Skip to content

Commit 2afac17

Browse files
authored
feat(dashboards): populate prebuilt dashboard with actual id (#108032)
1. Update `useGetPrebuiltDashboard` to populate `dashboard.id` with the actual id from the db. This provides a means to lookup the dashboard id as well. 2. Set `staleTime` in the query to `Infinity` which will cache the value for 5 minutes, this means if `useGetPrebuiltDashboard` is called with the same prebuiltId, the api call will be cached.
1 parent 93ebcb7 commit 2afac17

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

static/app/views/dashboards/orgDashboards.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ function OrgDashboards({children, initialDashboard}: OrgDashboardsProps) {
8989
selectedDashboard = {
9090
...selectedDashboard,
9191
...prebuiltDashboard,
92+
id: selectedDashboard.id,
9293
};
9394
}
9495

static/app/views/dashboards/utils/usePopulateLinkedDashboards.tsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ import {
1313

1414
export const useGetPrebuiltDashboard = (prebuiltId?: PrebuiltDashboardId) => {
1515
const prebuiltDashboard = prebuiltId ? PREBUILT_DASHBOARDS[prebuiltId] : undefined;
16-
return usePopulateLinkedDashboards(prebuiltDashboard);
16+
return usePopulatePrebuiltIdsWithActualIds(prebuiltDashboard, prebuiltId);
1717
};
1818

19-
const usePopulateLinkedDashboards = (dashboard?: PrebuiltDashboard) => {
19+
const usePopulatePrebuiltIdsWithActualIds = (
20+
dashboard?: PrebuiltDashboard,
21+
prebuiltId?: PrebuiltDashboardId
22+
) => {
2023
const organization = useOrganization();
2124

2225
const widgets = useMemo(() => dashboard?.widgets ?? [], [dashboard]);
@@ -42,27 +45,32 @@ const usePopulateLinkedDashboards = (dashboard?: PrebuiltDashboard) => {
4245
path: {organizationIdOrSlug: organization.slug},
4346
}),
4447
{
45-
query: {prebuiltId: prebuiltIds.sort()},
48+
query: {prebuiltId: [...prebuiltIds.sort(), prebuiltId].filter(defined)},
4649
},
4750
],
4851
{
49-
enabled: hasLinkedDashboards,
50-
staleTime: 0,
52+
enabled: hasLinkedDashboards || Boolean(prebuiltId),
53+
staleTime: Infinity,
5154
retry: false,
5255
}
5356
);
5457

5558
return useMemo(() => {
56-
if (!hasLinkedDashboards) {
57-
return {dashboard, isLoading: false};
59+
const populatedDashboard = {
60+
...dashboard,
61+
id: data?.find(d => d.prebuiltId === prebuiltId)?.id || undefined,
62+
};
63+
64+
if (!hasLinkedDashboards && !prebuiltId) {
65+
return {dashboard: populatedDashboard, isLoading: false};
5866
}
5967

6068
if (!data) {
61-
return {dashboard, isLoading};
69+
return {dashboard: populatedDashboard, isLoading};
6270
}
6371

64-
const populatedDashboard = {
65-
...dashboard,
72+
const populatedDashboardWithLinkedDashboards = {
73+
...populatedDashboard,
6674
widgets: widgets.map(widget => ({
6775
...widget,
6876
queries: widget.queries.map(query => ({
@@ -80,6 +88,6 @@ const usePopulateLinkedDashboards = (dashboard?: PrebuiltDashboard) => {
8088
})),
8189
};
8290

83-
return {dashboard: populatedDashboard, isLoading};
84-
}, [dashboard, widgets, data, hasLinkedDashboards, isLoading]);
91+
return {dashboard: populatedDashboardWithLinkedDashboards, isLoading};
92+
}, [dashboard, widgets, data, hasLinkedDashboards, isLoading, prebuiltId]);
8593
};

0 commit comments

Comments
 (0)