Browse Dashboards: Reset cache when folder is changed during import (#107116)

* Browse Dashboards: Reset cache when folder is changed during import

* Fetch dashboard data

* Do not invalidate tags

* Handle fetch error
This commit is contained in:
Alex Khomenko 2025-06-25 09:27:31 +02:00 committed by GitHub
parent e7af803906
commit e0eff97d94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 35 additions and 2 deletions

View File

@ -2,7 +2,7 @@ import { createApi } from '@reduxjs/toolkit/query/react';
import { AppEvents, isTruthy, locationUtil } from '@grafana/data';
import { t } from '@grafana/i18n';
import { config, getBackendSrv, locationService } from '@grafana/runtime';
import { config, getBackendSrv, isFetchError, locationService } from '@grafana/runtime';
import { Dashboard } from '@grafana/schema';
import { Spec as DashboardV2Spec } from '@grafana/schema/dist/esm/schema/dashboard/v2alpha1/types.spec.gen';
import { folderAPIv1beta1 as folderAPI } from 'app/api/clients/folder/v1beta1';
@ -401,14 +401,47 @@ export const browseDashboardsAPI = createApi({
folderUid,
},
}),
onQueryStarted: ({ folderUid }, { queryFulfilled, dispatch }) => {
onQueryStarted: async ({ dashboard, folderUid }, { queryFulfilled, dispatch }) => {
// Check if a dashboard with this UID already exists to find its current folder
let currentFolderUid: string | undefined;
if (dashboard.uid) {
try {
const existingDashboard = await getDashboardAPI().getDashboardDTO(dashboard.uid);
currentFolderUid = isDashboardV2Resource(existingDashboard)
? existingDashboard.metadata?.name
: existingDashboard.meta?.folderUid;
} catch (error) {
if (isFetchError(error)) {
if (error.status !== 404) {
console.error('Error fetching dashboard', error);
} else {
// Do not show the error alert if the dashboard does not exist
// this is expected when importing a new dashboard
error.isHandled = true;
}
}
}
}
queryFulfilled.then(async (response) => {
// Refresh destination folder
dispatch(
refetchChildren({
parentUID: folderUid,
pageSize: PAGE_SIZE,
})
);
// If the dashboard was moved from a different folder, refresh the source folder too
if (currentFolderUid && currentFolderUid !== folderUid) {
dispatch(
refetchChildren({
parentUID: currentFolderUid,
pageSize: PAGE_SIZE,
})
);
}
const dashboardUrl = locationUtil.stripBaseFromUrl(response.data.importedUrl);
locationService.push(dashboardUrl);
});