RestoreDashboards: Fix counting bug (#89201)

Fix restore modal showing incorrect counts

Co-authored-by: joshhunt <josh@trtr.co>
This commit is contained in:
Laura Benz 2024-06-14 14:30:05 +02:00 committed by GitHub
parent 4e3294cd8d
commit d44ddba996
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 10 deletions

View File

@ -3,22 +3,23 @@ import React from 'react';
import { ConfirmModal, Text } from '@grafana/ui'; import { ConfirmModal, Text } from '@grafana/ui';
import { Trans, t } from '../../../core/internationalization'; import { Trans, t } from '../../../core/internationalization';
import { DashboardTreeSelection } from '../../browse-dashboards/types';
interface Props { interface Props {
isOpen: boolean; isOpen: boolean;
onConfirm: () => Promise<void>; onConfirm: () => Promise<void>;
onDismiss: () => void; onDismiss: () => void;
selectedItems: DashboardTreeSelection; selectedDashboards: string[];
isLoading: boolean; isLoading: boolean;
} }
export const RestoreModal = ({ onConfirm, onDismiss, selectedItems, isLoading, ...props }: Props) => { export const RestoreModal = ({ onConfirm, onDismiss, selectedDashboards, isLoading, ...props }: Props) => {
const numberOfDashboards = selectedItems ? Object.keys(selectedItems.dashboard).length : 0; const numberOfDashboards = selectedDashboards.length;
const onRestore = async () => { const onRestore = async () => {
await onConfirm(); await onConfirm();
onDismiss(); onDismiss();
}; };
return ( return (
<ConfirmModal <ConfirmModal
body={ body={

View File

@ -1,5 +1,5 @@
import { css } from '@emotion/css'; import { css } from '@emotion/css';
import React from 'react'; import React, { useMemo } from 'react';
import { GrafanaTheme2 } from '@grafana/data/'; import { GrafanaTheme2 } from '@grafana/data/';
import { Button, useStyles2 } from '@grafana/ui'; import { Button, useStyles2 } from '@grafana/ui';
@ -17,11 +17,17 @@ export function RecentlyDeletedActions() {
const styles = useStyles2(getStyles); const styles = useStyles2(getStyles);
const dispatch = useDispatch(); const dispatch = useDispatch();
const selectedItems = useActionSelectionState(); const selectedItemsState = useActionSelectionState();
const [, stateManager] = useRecentlyDeletedStateManager(); const [, stateManager] = useRecentlyDeletedStateManager();
const [restoreDashboard, { isLoading: isRestoreLoading }] = useRestoreDashboardMutation(); const [restoreDashboard, { isLoading: isRestoreLoading }] = useRestoreDashboardMutation();
const selectedDashboards = useMemo(() => {
return Object.entries(selectedItemsState.dashboard)
.filter(([_, selected]) => selected)
.map(([uid]) => uid);
}, [selectedItemsState.dashboard]);
const onActionComplete = () => { const onActionComplete = () => {
dispatch(setAllSelection({ isSelected: false, folderUID: undefined })); dispatch(setAllSelection({ isSelected: false, folderUID: undefined }));
@ -29,9 +35,8 @@ export function RecentlyDeletedActions() {
}; };
const onRestore = async () => { const onRestore = async () => {
const promises = Object.entries(selectedItems.dashboard) const promises = selectedDashboards.map((uid) => restoreDashboard({ dashboardUID: uid }));
.filter(([_, selected]) => selected)
.map(([uid]) => restoreDashboard({ dashboardUID: uid }));
await Promise.all(promises); await Promise.all(promises);
onActionComplete(); onActionComplete();
}; };
@ -41,7 +46,7 @@ export function RecentlyDeletedActions() {
new ShowModalReactEvent({ new ShowModalReactEvent({
component: RestoreModal, component: RestoreModal,
props: { props: {
selectedItems, selectedDashboards,
onConfirm: onRestore, onConfirm: onRestore,
isLoading: isRestoreLoading, isLoading: isRestoreLoading,
}, },