MoveToFolderModal.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 { OnMoveOrDeleleSelectedItems } from '../../types';
  10. interface Props {
  11. onMoveItems: OnMoveOrDeleleSelectedItems;
  12. results: Map<string, Set<string>>;
  13. isOpen: boolean;
  14. onDismiss: () => void;
  15. }
  16. export const MoveToFolderModal: FC<Props> = ({ results, onMoveItems, isOpen, onDismiss }) => {
  17. const [folder, setFolder] = useState<FolderInfo | null>(null);
  18. const theme = useTheme();
  19. const styles = getStyles(theme);
  20. const notifyApp = useAppNotification();
  21. const selectedDashboards = Array.from(results.get('dashboard') ?? []);
  22. const [moving, setMoving] = useState(false);
  23. const moveTo = () => {
  24. if (folder && selectedDashboards.length) {
  25. const folderTitle = folder.title ?? 'General';
  26. setMoving(true);
  27. moveDashboards(selectedDashboards, folder).then((result: any) => {
  28. if (result.successCount > 0) {
  29. const ending = result.successCount === 1 ? '' : 's';
  30. const header = `Dashboard${ending} Moved`;
  31. const msg = `${result.successCount} dashboard${ending} moved to ${folderTitle}`;
  32. notifyApp.success(header, msg);
  33. }
  34. if (result.totalCount === result.alreadyInFolderCount) {
  35. notifyApp.error('Error', `Dashboard already belongs to folder ${folderTitle}`);
  36. } else {
  37. //update the list
  38. onMoveItems();
  39. }
  40. setMoving(false);
  41. onDismiss();
  42. });
  43. }
  44. };
  45. return isOpen ? (
  46. <Modal
  47. className={styles.modal}
  48. title="Choose Dashboard Folder"
  49. icon="folder-plus"
  50. isOpen={isOpen}
  51. onDismiss={onDismiss}
  52. >
  53. <>
  54. <div className={styles.content}>
  55. <p>
  56. Move the {selectedDashboards.length} selected dashboard{selectedDashboards.length === 1 ? '' : 's'} to the
  57. following folder:
  58. </p>
  59. <FolderPicker onChange={(f) => setFolder(f as FolderInfo)} />
  60. </div>
  61. <HorizontalGroup justify="center">
  62. <Button icon={moving ? 'fa fa-spinner' : undefined} variant="primary" onClick={moveTo}>
  63. Move
  64. </Button>
  65. <Button variant="secondary" onClick={onDismiss}>
  66. Cancel
  67. </Button>
  68. </HorizontalGroup>
  69. </>
  70. </Modal>
  71. ) : null;
  72. };
  73. const getStyles = stylesFactory((theme: GrafanaTheme) => {
  74. return {
  75. modal: css`
  76. width: 500px;
  77. `,
  78. content: css`
  79. margin-bottom: ${theme.spacing.lg};
  80. `,
  81. };
  82. });