ManageActions.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import React, { useState } from 'react';
  2. import { Button, HorizontalGroup, IconButton, IconName, useStyles2 } from '@grafana/ui';
  3. import { contextSrv } from 'app/core/services/context_srv';
  4. import { FolderDTO } from 'app/types';
  5. import { GENERAL_FOLDER_UID } from '../../constants';
  6. import { OnMoveOrDeleleSelectedItems } from '../../types';
  7. import { getStyles } from './ActionRow';
  8. import { ConfirmDeleteModal } from './ConfirmDeleteModal';
  9. import { MoveToFolderModal } from './MoveToFolderModal';
  10. type Props = {
  11. items: Map<string, Set<string>>;
  12. folder?: FolderDTO; // when we are loading in folder page
  13. onChange: OnMoveOrDeleleSelectedItems;
  14. clearSelection: () => void;
  15. };
  16. export function ManageActions({ items, folder, onChange, clearSelection }: Props) {
  17. const styles = useStyles2(getStyles);
  18. const canSave = folder?.canSave;
  19. const hasEditPermissionInFolders = folder ? canSave : contextSrv.hasEditPermissionInFolders;
  20. const canMove = hasEditPermissionInFolders;
  21. const selectedFolders = Array.from(items.get('folder') ?? []);
  22. const includesGeneralFolder = selectedFolders.find((result) => result === GENERAL_FOLDER_UID);
  23. const canDelete = hasEditPermissionInFolders && !includesGeneralFolder;
  24. const [isMoveModalOpen, setIsMoveModalOpen] = useState(false);
  25. const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
  26. const onMove = () => {
  27. setIsMoveModalOpen(true);
  28. };
  29. const onDelete = () => {
  30. setIsDeleteModalOpen(true);
  31. };
  32. return (
  33. <div className={styles.actionRow} data-testid="manage-actions">
  34. <div className={styles.rowContainer}>
  35. <HorizontalGroup spacing="md" width="auto">
  36. <IconButton name={'check-square' as IconName} onClick={clearSelection} title="Uncheck everything" />
  37. <Button disabled={!canMove} onClick={onMove} icon="exchange-alt" variant="secondary">
  38. Move
  39. </Button>
  40. <Button disabled={!canDelete} onClick={onDelete} icon="trash-alt" variant="destructive">
  41. Delete
  42. </Button>
  43. </HorizontalGroup>
  44. </div>
  45. <ConfirmDeleteModal
  46. onDeleteItems={onChange}
  47. results={items}
  48. isOpen={isDeleteModalOpen}
  49. onDismiss={() => setIsDeleteModalOpen(false)}
  50. />
  51. <MoveToFolderModal
  52. onMoveItems={onChange}
  53. results={items}
  54. isOpen={isMoveModalOpen}
  55. onDismiss={() => setIsMoveModalOpen(false)}
  56. />
  57. </div>
  58. );
  59. }