From 8a891dcdc4b68b00110e7877685f710b2bef417e Mon Sep 17 00:00:00 2001 From: Josh Hunt Date: Mon, 17 Jun 2024 11:15:37 +0100 Subject: [PATCH] RestoreDashboards: Clear cached parent folders after restoring a dashboard (#89162) * RestoreDashboards: Refresh parent folders after restoring a dashboard * actually, just clear the cache * less log * refactor --- .../api/browseDashboardsAPI.ts | 5 --- .../components/RecentlyDeletedActions.tsx | 32 ++++++++++++++++--- .../browse-dashboards/state/reducers.ts | 15 +++++++++ .../features/browse-dashboards/state/slice.ts | 3 +- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/public/app/features/browse-dashboards/api/browseDashboardsAPI.ts b/public/app/features/browse-dashboards/api/browseDashboardsAPI.ts index 9be53c2b10b..49f91d2bcef 100644 --- a/public/app/features/browse-dashboards/api/browseDashboardsAPI.ts +++ b/public/app/features/browse-dashboards/api/browseDashboardsAPI.ts @@ -381,11 +381,6 @@ export const browseDashboardsAPI = createApi({ url: `/dashboards/uid/${dashboardUID}/trash`, method: 'PATCH', }), - onQueryStarted: ({ dashboardUID }, { queryFulfilled, dispatch }) => { - queryFulfilled.then(() => { - dispatch(refreshParents([dashboardUID])); - }); - }, }), }), }); diff --git a/public/app/features/browse-dashboards/components/RecentlyDeletedActions.tsx b/public/app/features/browse-dashboards/components/RecentlyDeletedActions.tsx index 8657244d90c..c1ceaa07289 100644 --- a/public/app/features/browse-dashboards/components/RecentlyDeletedActions.tsx +++ b/public/app/features/browse-dashboards/components/RecentlyDeletedActions.tsx @@ -3,16 +3,16 @@ import React, { useMemo } from 'react'; import { GrafanaTheme2 } from '@grafana/data/'; import { Button, useStyles2 } from '@grafana/ui'; +import { GENERAL_FOLDER_UID } from 'app/features/search/constants'; import appEvents from '../../../core/app_events'; import { Trans } from '../../../core/internationalization'; import { useDispatch } from '../../../types'; import { ShowModalReactEvent } from '../../../types/events'; -import { useRestoreDashboardMutation } from '../api/browseDashboardsAPI'; +import { useRestoreDashboardMutation } from '../../browse-dashboards/api/browseDashboardsAPI'; +import { clearFolders, setAllSelection, useActionSelectionState } from '../../browse-dashboards/state'; import { useRecentlyDeletedStateManager } from '../api/useRecentlyDeletedStateManager'; -import { setAllSelection, useActionSelectionState } from '../state'; - -import { RestoreModal } from './RestoreModal'; +import { RestoreModal } from '../components/RestoreModal'; export function RecentlyDeletedActions() { const styles = useStyles2(getStyles); @@ -36,9 +36,31 @@ export function RecentlyDeletedActions() { }; const onRestore = async () => { - const promises = selectedDashboards.map((uid) => restoreDashboard({ dashboardUID: uid })); + const resultsView = stateManager.state.result?.view.toArray(); + if (!resultsView) { + return; + } + + const promises = selectedDashboards.map((uid) => { + return restoreDashboard({ dashboardUID: uid }); + }); await Promise.all(promises); + + const parentUIDs = new Set(); + for (const uid of selectedDashboards) { + const foundItem = resultsView.find((v) => v.uid === uid); + if (!foundItem) { + continue; + } + + // Search API returns items with no parent with a location of 'general', so we + // need to convert that back to undefined + const folderUID = foundItem.location === GENERAL_FOLDER_UID ? undefined : foundItem.location; + parentUIDs.add(folderUID); + } + dispatch(clearFolders(Array.from(parentUIDs))); + onActionComplete(); }; diff --git a/public/app/features/browse-dashboards/state/reducers.ts b/public/app/features/browse-dashboards/state/reducers.ts index e506748dcd6..045be88a490 100644 --- a/public/app/features/browse-dashboards/state/reducers.ts +++ b/public/app/features/browse-dashboards/state/reducers.ts @@ -200,3 +200,18 @@ export function setAllSelection( } } } + +export function clearFolders(state: BrowseDashboardsState, action: PayloadAction>) { + const folderUIDs = Array.isArray(action.payload) ? action.payload : [action.payload]; + + for (const folderUID of folderUIDs) { + if (!folderUID) { + state.rootItems = undefined; + } else { + state.childrenByParentUID[folderUID] = undefined; + + // close the folder to require it to be refetched next time its opened + state.openFolders[folderUID] = false; + } + } +} diff --git a/public/app/features/browse-dashboards/state/slice.ts b/public/app/features/browse-dashboards/state/slice.ts index 55254eb78d1..8c2172e442f 100644 --- a/public/app/features/browse-dashboards/state/slice.ts +++ b/public/app/features/browse-dashboards/state/slice.ts @@ -32,7 +32,8 @@ const browseDashboardsSlice = createSlice({ export const browseDashboardsReducer = browseDashboardsSlice.reducer; -export const { setFolderOpenState, setItemSelectionState, setAllSelection } = browseDashboardsSlice.actions; +export const { setFolderOpenState, setItemSelectionState, setAllSelection, clearFolders } = + browseDashboardsSlice.actions; export default { browseDashboards: browseDashboardsReducer,