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
This commit is contained in:
Josh Hunt
2024-06-17 11:15:37 +01:00
committed by GitHub
parent 67f2d93281
commit 8a891dcdc4
4 changed files with 44 additions and 11 deletions
@@ -381,11 +381,6 @@ export const browseDashboardsAPI = createApi({
url: `/dashboards/uid/${dashboardUID}/trash`,
method: 'PATCH',
}),
onQueryStarted: ({ dashboardUID }, { queryFulfilled, dispatch }) => {
queryFulfilled.then(() => {
dispatch(refreshParents([dashboardUID]));
});
},
}),
}),
});
@@ -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<string | undefined>();
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();
};
@@ -200,3 +200,18 @@ export function setAllSelection(
}
}
}
export function clearFolders(state: BrowseDashboardsState, action: PayloadAction<Array<string | undefined>>) {
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;
}
}
}
@@ -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,