RestoreDashboards: Fix restoring to 'Dashboards' (#94086)

* pairing WIP

* refactor: clean up

---------

Co-authored-by: joshhunt <josh@trtr.co>
This commit is contained in:
Laura Benz
2024-10-02 17:15:31 +02:00
committed by GitHub
co-authored by joshhunt
parent 9fff736549
commit a0542e7307
3 changed files with 17 additions and 16 deletions
@@ -33,7 +33,13 @@ export function RecentlyDeletedActions() {
if (searchState.result) {
for (const selectedDashboard of selectedDashboards) {
const index = searchState.result.view.fields.uid.values.findIndex((e) => e === selectedDashboard);
selectedDashboardOrigin.push(searchState.result.view.fields.location.values[index]);
// SQLSearcher changes the location from empty string to 'general' for items with no parent
// but the restore API doesn't work with 'general' folder UID, so we need to convert it back
// to an empty string
const location = searchState.result.view.fields.location.values[index];
const fixedLocation = location === GENERAL_FOLDER_UID ? '' : location;
selectedDashboardOrigin.push(fixedLocation);
}
}
@@ -1,4 +1,4 @@
import { useState, useEffect } from 'react';
import { useState } from 'react';
import { reportInteraction } from '@grafana/runtime';
import { ConfirmModal, Space, Text } from '@grafana/ui';
@@ -23,19 +23,14 @@ export const RestoreModal = ({
isLoading,
...props
}: RestoreModalProps) => {
const [restoreTarget, setRestoreTarget] = useState<string>();
const numberOfDashboards = selectedDashboards.length;
useEffect(() => {
// restoreTarget is used by the folder picker to preselect a folder and therefore enable the confirm button
// if there is only one dashboard selected or all selected dashboards come from the same folder
if (
dashboardOrigin.length > 0 &&
const [restoreTarget, setRestoreTarget] = useState<string | undefined>(() => {
// Preselect the restore target and therefore enable the confirm button if all selected dashboards come from the same folder
return dashboardOrigin.length > 0 &&
dashboardOrigin.every((originalLocation) => originalLocation === dashboardOrigin[0])
) {
setRestoreTarget(dashboardOrigin[0]);
}
}, [dashboardOrigin, selectedDashboards]);
? dashboardOrigin[0]
: undefined;
});
const numberOfDashboards = selectedDashboards.length;
const onRestore = async () => {
reportInteraction('grafana_restore_confirm_clicked', {
+2 -2
View File
@@ -4,7 +4,7 @@ import { TermCount } from 'app/core/components/TagFilter/TagFilter';
import { backendSrv } from 'app/core/services/backend_srv';
import { PermissionLevelString } from 'app/types';
import { DEFAULT_MAX_VALUES, TYPE_KIND_MAP } from '../constants';
import { DEFAULT_MAX_VALUES, GENERAL_FOLDER_UID, TYPE_KIND_MAP } from '../constants';
import { DashboardSearchHit, DashboardSearchItemType } from '../types';
import { LocationInfo } from './types';
@@ -175,7 +175,7 @@ export class SQLSearcher implements GrafanaSearcher {
let v = hit.folderUid;
if (!v && k === 'dashboard') {
v = 'general';
v = GENERAL_FOLDER_UID;
}
location.push(v!);