eventAttachmentActions.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {useRole} from 'sentry/components/acl/useRole';
  2. import {Button, LinkButton} from 'sentry/components/button';
  3. import ButtonBar from 'sentry/components/buttonBar';
  4. import Confirm from 'sentry/components/confirm';
  5. import {hasInlineAttachmentRenderer} from 'sentry/components/events/attachmentViewers/previewAttachmentTypes';
  6. import {IconDelete, IconDownload, IconShow} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import type {IssueAttachment} from 'sentry/types/group';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. type Props = {
  11. attachment: IssueAttachment;
  12. onDelete: () => void;
  13. projectSlug: string;
  14. onPreviewClick?: () => void;
  15. previewIsOpen?: boolean;
  16. withPreviewButton?: boolean;
  17. };
  18. function EventAttachmentActions({
  19. attachment,
  20. projectSlug,
  21. withPreviewButton,
  22. previewIsOpen,
  23. onPreviewClick,
  24. onDelete,
  25. }: Props) {
  26. const organization = useOrganization();
  27. const {hasRole: hasAttachmentRole} = useRole({role: 'attachmentsRole'});
  28. const url = `/api/0/projects/${organization.slug}/${projectSlug}/events/${attachment.event_id}/attachments/${attachment.id}/`;
  29. const hasPreview = hasInlineAttachmentRenderer(attachment);
  30. return (
  31. <ButtonBar gap={1}>
  32. {withPreviewButton && (
  33. <Button
  34. size="xs"
  35. disabled={!hasAttachmentRole || !hasPreview}
  36. priority={previewIsOpen ? 'primary' : 'default'}
  37. icon={<IconShow />}
  38. onClick={onPreviewClick}
  39. title={
  40. !hasAttachmentRole
  41. ? t('Insufficient permissions to preview attachments')
  42. : !hasPreview
  43. ? t('This attachment cannot be previewed')
  44. : undefined
  45. }
  46. >
  47. {t('Preview')}
  48. </Button>
  49. )}
  50. <LinkButton
  51. size="xs"
  52. icon={<IconDownload />}
  53. href={hasAttachmentRole ? `${url}?download=1` : ''}
  54. disabled={!hasAttachmentRole}
  55. title={
  56. hasAttachmentRole
  57. ? t('Download')
  58. : t('Insufficient permissions to download attachments')
  59. }
  60. aria-label={t('Download')}
  61. />
  62. <Confirm
  63. confirmText={t('Delete')}
  64. message={t('Are you sure you wish to delete this file?')}
  65. priority="danger"
  66. onConfirm={onDelete}
  67. disabled={!hasAttachmentRole}
  68. >
  69. <Button
  70. size="xs"
  71. icon={<IconDelete />}
  72. aria-label={t('Delete')}
  73. disabled={!hasAttachmentRole}
  74. title={
  75. hasAttachmentRole
  76. ? t('Delete')
  77. : t('Insufficient permissions to delete attachments')
  78. }
  79. />
  80. </Confirm>
  81. </ButtonBar>
  82. );
  83. }
  84. export default EventAttachmentActions;