MoveToFolderModal.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { css } from '@emotion/css';
  2. import React, { FC, useState } from 'react';
  3. import { GrafanaTheme } from '@grafana/data';
  4. import { Button, HorizontalGroup, Modal, stylesFactory, useTheme } from '@grafana/ui';
  5. import { FolderPicker } from 'app/core/components/Select/FolderPicker';
  6. import { useAppNotification } from 'app/core/copy/appNotification';
  7. import { moveDashboards } from 'app/features/manage-dashboards/state/actions';
  8. import { FolderInfo } from 'app/types';
  9. import { DashboardSection, OnMoveItems } from '../types';
  10. import { getCheckedDashboards } from '../utils';
  11. interface Props {
  12. onMoveItems: OnMoveItems;
  13. results: DashboardSection[];
  14. isOpen: boolean;
  15. onDismiss: () => void;
  16. }
  17. export const MoveToFolderModal: FC<Props> = ({ results, onMoveItems, isOpen, onDismiss }) => {
  18. const [folder, setFolder] = useState<FolderInfo | null>(null);
  19. const theme = useTheme();
  20. const styles = getStyles(theme);
  21. const selectedDashboards = getCheckedDashboards(results);
  22. const notifyApp = useAppNotification();
  23. const moveTo = () => {
  24. if (folder && selectedDashboards.length) {
  25. const folderTitle = folder.title ?? 'General';
  26. moveDashboards(selectedDashboards.map((d) => d.uid) as string[], folder).then((result: any) => {
  27. if (result.successCount > 0) {
  28. const ending = result.successCount === 1 ? '' : 's';
  29. const header = `Dashboard${ending} Moved`;
  30. const msg = `${result.successCount} dashboard${ending} moved to ${folderTitle}`;
  31. notifyApp.success(header, msg);
  32. }
  33. if (result.totalCount === result.alreadyInFolderCount) {
  34. notifyApp.error('Error', `Dashboard already belongs to folder ${folderTitle}`);
  35. } else {
  36. onMoveItems(selectedDashboards, folder);
  37. }
  38. onDismiss();
  39. });
  40. }
  41. };
  42. return isOpen ? (
  43. <Modal
  44. className={styles.modal}
  45. title="Choose Dashboard Folder"
  46. icon="folder-plus"
  47. isOpen={isOpen}
  48. onDismiss={onDismiss}
  49. >
  50. <>
  51. <div className={styles.content}>
  52. <p>
  53. Move the {selectedDashboards.length} selected dashboard{selectedDashboards.length === 1 ? '' : 's'} to the
  54. following folder:
  55. </p>
  56. <FolderPicker onChange={(f) => setFolder(f as FolderInfo)} />
  57. </div>
  58. <HorizontalGroup justify="center">
  59. <Button variant="primary" onClick={moveTo}>
  60. Move
  61. </Button>
  62. <Button variant="secondary" onClick={onDismiss}>
  63. Cancel
  64. </Button>
  65. </HorizontalGroup>
  66. </>
  67. </Modal>
  68. ) : null;
  69. };
  70. const getStyles = stylesFactory((theme: GrafanaTheme) => {
  71. return {
  72. modal: css`
  73. width: 500px;
  74. `,
  75. content: css`
  76. margin-bottom: ${theme.spacing.lg};
  77. `,
  78. };
  79. });