eventAttachmentActions.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import {Component} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Client} from 'sentry/api';
  4. import Button from 'sentry/components/button';
  5. import ButtonBar from 'sentry/components/buttonBar';
  6. import Confirm from 'sentry/components/confirm';
  7. import {IconDelete, IconDownload, IconShow} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import space from 'sentry/styles/space';
  10. import withApi from 'sentry/utils/withApi';
  11. type Props = {
  12. api: Client;
  13. attachmentId: string;
  14. onDelete: (attachmentId: string) => void;
  15. url: string | null;
  16. hasPreview?: boolean;
  17. onPreview?: (attachmentId: string) => void;
  18. previewIsOpen?: boolean;
  19. withPreviewButton?: boolean;
  20. };
  21. class EventAttachmentActions extends Component<Props> {
  22. handlePreview() {
  23. const {onPreview, attachmentId} = this.props;
  24. if (onPreview) {
  25. onPreview(attachmentId);
  26. }
  27. }
  28. render() {
  29. const {url, withPreviewButton, hasPreview, previewIsOpen, onDelete, attachmentId} =
  30. this.props;
  31. return (
  32. <ButtonBar gap={1}>
  33. <Confirm
  34. confirmText={t('Delete')}
  35. message={t('Are you sure you wish to delete this file?')}
  36. priority="danger"
  37. onConfirm={() => onDelete(attachmentId)}
  38. disabled={!url}
  39. >
  40. <Button
  41. size="xs"
  42. icon={<IconDelete size="xs" />}
  43. aria-label={t('Delete')}
  44. disabled={!url}
  45. title={!url ? t('Insufficient permissions to delete attachments') : undefined}
  46. />
  47. </Confirm>
  48. <DownloadButton
  49. size="xs"
  50. icon={<IconDownload size="xs" />}
  51. href={url ? `${url}?download=1` : ''}
  52. disabled={!url}
  53. title={!url ? t('Insufficient permissions to download attachments') : undefined}
  54. aria-label={t('Download')}
  55. />
  56. {withPreviewButton && (
  57. <DownloadButton
  58. size="xs"
  59. disabled={!url || !hasPreview}
  60. priority={previewIsOpen ? 'primary' : 'default'}
  61. icon={<IconShow size="xs" />}
  62. onClick={() => this.handlePreview()}
  63. title={
  64. !url
  65. ? t('Insufficient permissions to preview attachments')
  66. : !hasPreview
  67. ? t('This attachment cannot be previewed')
  68. : undefined
  69. }
  70. >
  71. {t('Preview')}
  72. </DownloadButton>
  73. )}
  74. </ButtonBar>
  75. );
  76. }
  77. }
  78. const DownloadButton = styled(Button)`
  79. margin-right: ${space(0.5)};
  80. `;
  81. export default withApi(EventAttachmentActions);