Invalidation after add to dashboard

This commit is contained in:
Nate Kelley 2025-07-25 16:49:45 -06:00
parent ac6b63bd6d
commit 2ba9044d13
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
2 changed files with 21 additions and 2 deletions

View File

@ -745,10 +745,26 @@ export const useRemoveMetricsFromDashboard = () => {
return useMutation({ return useMutation({
mutationFn: removeMetricFromDashboard, mutationFn: removeMetricFromDashboard,
onSuccess: (data) => { onMutate: (variables) => {
variables.metricIds.forEach((id) => {
queryClient.setQueryData(metricsQueryKeys.metricsGetMetric(id, null).queryKey, (old) => {
if (!old) return old;
return create(old, (draft) => {
draft.dashboards = old?.dashboards?.filter((d) => d.id !== variables.dashboardId) || [];
});
});
});
},
onSuccess: (data, variables) => {
if (data) { if (data) {
setLatestDashboardVersion(data.dashboard.id, data.dashboard.version_number); setLatestDashboardVersion(data.dashboard.id, data.dashboard.version_number);
} }
variables.metricIds.forEach((id) => {
queryClient.invalidateQueries({
queryKey: metricsQueryKeys.metricsGetMetric(id, null).queryKey
});
});
} }
}); });
}; };

View File

@ -152,6 +152,7 @@ const useDashboardSelectMenu = ({ metricId }: { metricId: string }) => {
const { mutateAsync: saveMetricsToDashboard } = useAddMetricsToDashboard(); const { mutateAsync: saveMetricsToDashboard } = useAddMetricsToDashboard();
const { mutateAsync: removeMetricsFromDashboard } = useRemoveMetricsFromDashboard(); const { mutateAsync: removeMetricsFromDashboard } = useRemoveMetricsFromDashboard();
const { data: dashboards } = useGetMetric({ id: metricId }, { select: (x) => x.dashboards }); const { data: dashboards } = useGetMetric({ id: metricId }, { select: (x) => x.dashboards });
const { openInfoMessage } = useBusterNotifications();
const onSaveToDashboard = useMemoizedFn(async (dashboardIds: string[]) => { const onSaveToDashboard = useMemoizedFn(async (dashboardIds: string[]) => {
await Promise.all( await Promise.all(
@ -159,14 +160,16 @@ const useDashboardSelectMenu = ({ metricId }: { metricId: string }) => {
saveMetricsToDashboard({ metricIds: [metricId], dashboardId }) saveMetricsToDashboard({ metricIds: [metricId], dashboardId })
) )
); );
openInfoMessage('Metric added to dashboard');
}); });
const onRemoveFromDashboard = useMemoizedFn(async (dashboardIds: string[]) => { const onRemoveFromDashboard = useMemoizedFn(async (dashboardIds: string[]) => {
await Promise.all( await Promise.all(
dashboardIds.map((dashboardId) => dashboardIds.map((dashboardId) =>
removeMetricsFromDashboard({ metricIds: [metricId], dashboardId }) removeMetricsFromDashboard({ metricIds: [metricId], dashboardId, useConfirmModal: false })
) )
); );
openInfoMessage('Metric removed from dashboard');
}); });
const { items, footerContent, selectType, menuHeader } = useSaveToDashboardDropdownContent({ const { items, footerContent, selectType, menuHeader } = useSaveToDashboardDropdownContent({