groupEventAttachments.tsx 6.7 KB

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