groupEventAttachments.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import pick from 'lodash/pick';
  4. import xor from 'lodash/xor';
  5. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  6. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  7. import * as Layout from 'sentry/components/layouts/thirds';
  8. import LoadingError from 'sentry/components/loadingError';
  9. import LoadingIndicator from 'sentry/components/loadingIndicator';
  10. import Pagination from 'sentry/components/pagination';
  11. import Panel from 'sentry/components/panels/panel';
  12. import PanelBody from 'sentry/components/panels/panelBody';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import type {IssueAttachment} from 'sentry/types/group';
  16. import type {Project} from 'sentry/types/project';
  17. import {useApiQuery, useMutation} from 'sentry/utils/queryClient';
  18. import {decodeList} from 'sentry/utils/queryString';
  19. import useApi from 'sentry/utils/useApi';
  20. import {useLocation} from 'sentry/utils/useLocation';
  21. import {useParams} from 'sentry/utils/useParams';
  22. import GroupEventAttachmentsFilter, {
  23. crashReportTypes,
  24. SCREENSHOT_TYPE,
  25. } from './groupEventAttachmentsFilter';
  26. import GroupEventAttachmentsTable from './groupEventAttachmentsTable';
  27. import {ScreenshotCard} from './screenshotCard';
  28. type GroupEventAttachmentsProps = {
  29. project: Project;
  30. };
  31. enum EventAttachmentFilter {
  32. ALL = 'all',
  33. CRASH_REPORTS = 'onlyCrash',
  34. SCREENSHOTS = 'screenshot',
  35. }
  36. export const MAX_SCREENSHOTS_PER_PAGE = 12;
  37. function useActiveAttachmentsTab() {
  38. const location = useLocation();
  39. const types = decodeList(location.query.types);
  40. if (types.length === 0) {
  41. return EventAttachmentFilter.ALL;
  42. }
  43. if (types[0] === SCREENSHOT_TYPE) {
  44. return EventAttachmentFilter.SCREENSHOTS;
  45. }
  46. if (xor(crashReportTypes, types).length === 0) {
  47. return EventAttachmentFilter.CRASH_REPORTS;
  48. }
  49. return EventAttachmentFilter.ALL;
  50. }
  51. function GroupEventAttachments({project}: GroupEventAttachmentsProps) {
  52. const location = useLocation();
  53. const {groupId, orgId} = useParams<{groupId: string; orgId: string}>();
  54. const activeAttachmentsTab = useActiveAttachmentsTab();
  55. const [deletedAttachments, setDeletedAttachments] = useState<string[]>([]);
  56. const api = useApi();
  57. const {
  58. data: eventAttachments,
  59. isPending,
  60. isError,
  61. getResponseHeader,
  62. refetch,
  63. } = useApiQuery<IssueAttachment[]>(
  64. [
  65. `/organizations/${orgId}/issues/${groupId}/attachments/`,
  66. {
  67. query:
  68. activeAttachmentsTab === EventAttachmentFilter.SCREENSHOTS
  69. ? {
  70. ...location.query,
  71. types: undefined, // need to explicitly set this to undefined because AsyncComponent adds location query back into the params
  72. screenshot: 1,
  73. per_page: MAX_SCREENSHOTS_PER_PAGE,
  74. }
  75. : {
  76. ...pick(location.query, ['cursor', 'environment', 'types']),
  77. per_page: 50,
  78. },
  79. },
  80. ],
  81. {staleTime: 0}
  82. );
  83. const {mutate: deleteAttachment} = useMutation({
  84. mutationFn: ({attachmentId, eventId}: {attachmentId: string; eventId: string}) =>
  85. api.requestPromise(
  86. `/projects/${orgId}/${project.slug}/events/${eventId}/attachments/${attachmentId}/`,
  87. {
  88. method: 'DELETE',
  89. }
  90. ),
  91. onError: () => {
  92. addErrorMessage('An error occurred while deleteting the attachment');
  93. },
  94. });
  95. const handleDelete = (deletedAttachmentId: string) => {
  96. const attachment = eventAttachments?.find(item => item.id === deletedAttachmentId);
  97. if (!attachment) {
  98. return;
  99. }
  100. setDeletedAttachments(prevState => [...prevState, deletedAttachmentId]);
  101. deleteAttachment({attachmentId: attachment.id, eventId: attachment.event_id});
  102. };
  103. const renderInnerBody = () => {
  104. if (isPending) {
  105. return <LoadingIndicator />;
  106. }
  107. if (eventAttachments && eventAttachments.length > 0) {
  108. return (
  109. <GroupEventAttachmentsTable
  110. attachments={eventAttachments}
  111. orgId={orgId}
  112. projectSlug={project.slug}
  113. groupId={groupId}
  114. onDelete={handleDelete}
  115. deletedAttachments={deletedAttachments}
  116. />
  117. );
  118. }
  119. if (activeAttachmentsTab === EventAttachmentFilter.CRASH_REPORTS) {
  120. return (
  121. <EmptyStateWarning>
  122. <p>{t('No crash reports found')}</p>
  123. </EmptyStateWarning>
  124. );
  125. }
  126. return (
  127. <EmptyStateWarning>
  128. <p>{t('No attachments found')}</p>
  129. </EmptyStateWarning>
  130. );
  131. };
  132. const renderAttachmentsTable = () => {
  133. if (isError) {
  134. return <LoadingError onRetry={refetch} message={t('Error loading attachments')} />;
  135. }
  136. return (
  137. <Panel className="event-list">
  138. <PanelBody>{renderInnerBody()}</PanelBody>
  139. </Panel>
  140. );
  141. };
  142. const renderScreenshotGallery = () => {
  143. if (isError) {
  144. return <LoadingError onRetry={refetch} message={t('Error loading screenshots')} />;
  145. }
  146. if (isPending) {
  147. return <LoadingIndicator />;
  148. }
  149. if (eventAttachments && eventAttachments.length > 0) {
  150. return (
  151. <ScreenshotGrid>
  152. {eventAttachments?.map((screenshot, index) => {
  153. return (
  154. <ScreenshotCard
  155. key={`${index}-${screenshot.id}`}
  156. eventAttachment={screenshot}
  157. eventId={screenshot.event_id}
  158. projectSlug={project.slug}
  159. groupId={groupId}
  160. onDelete={handleDelete}
  161. pageLinks={getResponseHeader?.('Link')}
  162. attachments={eventAttachments}
  163. attachmentIndex={index}
  164. />
  165. );
  166. })}
  167. </ScreenshotGrid>
  168. );
  169. }
  170. return (
  171. <EmptyStateWarning>
  172. <p>{t('No screenshots found')}</p>
  173. </EmptyStateWarning>
  174. );
  175. };
  176. return (
  177. <Layout.Body>
  178. <Layout.Main fullWidth>
  179. <GroupEventAttachmentsFilter project={project} />
  180. {activeAttachmentsTab === EventAttachmentFilter.SCREENSHOTS
  181. ? renderScreenshotGallery()
  182. : renderAttachmentsTable()}
  183. <Pagination pageLinks={getResponseHeader?.('Link')} />
  184. </Layout.Main>
  185. </Layout.Body>
  186. );
  187. }
  188. export default GroupEventAttachments;
  189. const ScreenshotGrid = styled('div')`
  190. display: grid;
  191. grid-template-columns: minmax(100px, 1fr);
  192. grid-template-rows: repeat(2, max-content);
  193. gap: ${space(2)};
  194. @media (min-width: ${p => p.theme.breakpoints.small}) {
  195. grid-template-columns: repeat(3, minmax(100px, 1fr));
  196. }
  197. @media (min-width: ${p => p.theme.breakpoints.large}) {
  198. grid-template-columns: repeat(4, minmax(100px, 1fr));
  199. }
  200. @media (min-width: ${p => p.theme.breakpoints.xxlarge}) {
  201. grid-template-columns: repeat(6, minmax(100px, 1fr));
  202. }
  203. `;