ConfirmDeleteModal.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { css } from '@emotion/css';
  2. import React, { FC } from 'react';
  3. import { GrafanaTheme } from '@grafana/data';
  4. import { ConfirmModal, stylesFactory, useTheme } from '@grafana/ui';
  5. import { deleteFoldersAndDashboards } from 'app/features/manage-dashboards/state/actions';
  6. import { OnMoveOrDeleleSelectedItems } from '../../types';
  7. interface Props {
  8. onDeleteItems: OnMoveOrDeleleSelectedItems;
  9. results: Map<string, Set<string>>;
  10. isOpen: boolean;
  11. onDismiss: () => void;
  12. }
  13. export const ConfirmDeleteModal: FC<Props> = ({ results, onDeleteItems, isOpen, onDismiss }) => {
  14. const theme = useTheme();
  15. const styles = getStyles(theme);
  16. const dashboards = Array.from(results.get('dashboard') ?? []);
  17. const folders = Array.from(results.get('folder') ?? []);
  18. const folderCount = folders.length;
  19. const dashCount = dashboards.length;
  20. let text = 'Do you want to delete the ';
  21. let subtitle;
  22. const dashEnding = dashCount === 1 ? '' : 's';
  23. const folderEnding = folderCount === 1 ? '' : 's';
  24. if (folderCount > 0 && dashCount > 0) {
  25. text += `selected folder${folderEnding} and dashboard${dashEnding}?\n`;
  26. subtitle = `All dashboards and alerts of the selected folder${folderEnding} will also be deleted`;
  27. } else if (folderCount > 0) {
  28. text += `selected folder${folderEnding} and all ${folderCount === 1 ? 'its' : 'their'} dashboards and alerts?`;
  29. } else {
  30. text += `${dashCount} selected dashboard${dashEnding}?`;
  31. }
  32. const deleteItems = () => {
  33. deleteFoldersAndDashboards(folders, dashboards).then(() => {
  34. onDeleteItems();
  35. onDismiss();
  36. });
  37. };
  38. return isOpen ? (
  39. <ConfirmModal
  40. isOpen={isOpen}
  41. title="Delete"
  42. body={
  43. <>
  44. {text} {subtitle && <div className={styles.subtitle}>{subtitle}</div>}
  45. </>
  46. }
  47. confirmText="Delete"
  48. onConfirm={deleteItems}
  49. onDismiss={onDismiss}
  50. />
  51. ) : null;
  52. };
  53. const getStyles = stylesFactory((theme: GrafanaTheme) => {
  54. return {
  55. subtitle: css`
  56. font-size: ${theme.typography.size.base};
  57. padding-top: ${theme.spacing.md};
  58. `,
  59. };
  60. });