groupEventAttachments.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import {useEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Flex} from 'sentry/components/container/flex';
  4. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  5. import LoadingError from 'sentry/components/loadingError';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import Pagination from 'sentry/components/pagination';
  8. import {IconFilter} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import {space} from 'sentry/styles/space';
  11. import type {Group, IssueAttachment} from 'sentry/types/group';
  12. import type {Project} from 'sentry/types/project';
  13. import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
  14. import {useLocation} from 'sentry/utils/useLocation';
  15. import {useNavigate} from 'sentry/utils/useNavigate';
  16. import useOrganization from 'sentry/utils/useOrganization';
  17. import {useEventQuery} from 'sentry/views/issueDetails/streamline/eventSearch';
  18. import {useIssueDetailsEventView} from 'sentry/views/issueDetails/streamline/hooks/useIssueDetailsDiscoverQuery';
  19. import {useHasStreamlinedUI} from 'sentry/views/issueDetails/utils';
  20. import GroupEventAttachmentsFilter, {
  21. EventAttachmentFilter,
  22. } from './groupEventAttachmentsFilter';
  23. import GroupEventAttachmentsTable from './groupEventAttachmentsTable';
  24. import {ScreenshotCard} from './screenshotCard';
  25. import {useDeleteGroupEventAttachment} from './useDeleteGroupEventAttachment';
  26. import {useGroupEventAttachments} from './useGroupEventAttachments';
  27. type GroupEventAttachmentsProps = {
  28. group: Group;
  29. project: Project;
  30. };
  31. const DEFAULT_ATTACHMENTS_TAB = EventAttachmentFilter.ALL;
  32. function GroupEventAttachments({project, group}: GroupEventAttachmentsProps) {
  33. const location = useLocation();
  34. const organization = useOrganization();
  35. const hasStreamlinedUI = useHasStreamlinedUI();
  36. const eventQuery = useEventQuery({groupId: group.id});
  37. const eventView = useIssueDetailsEventView({group});
  38. const navigate = useNavigate();
  39. const [previouslyUsedAttachmentsTab, setPreviouslyUsedAttachmentsTab] =
  40. useLocalStorageState(
  41. `issue-details-attachments-default-tab-${project.id}`,
  42. DEFAULT_ATTACHMENTS_TAB
  43. );
  44. const activeAttachmentsTab =
  45. (location.query.attachmentFilter as EventAttachmentFilter | undefined) ??
  46. previouslyUsedAttachmentsTab ??
  47. DEFAULT_ATTACHMENTS_TAB;
  48. // Persist the previously used attachments tab in the url if it's not already set
  49. useEffect(() => {
  50. if (
  51. !location.query.attachmentFilter &&
  52. previouslyUsedAttachmentsTab !== DEFAULT_ATTACHMENTS_TAB
  53. ) {
  54. navigate(
  55. {
  56. pathname: location.pathname,
  57. query: {...location.query, attachmentFilter: previouslyUsedAttachmentsTab},
  58. },
  59. {replace: true}
  60. );
  61. }
  62. }, [previouslyUsedAttachmentsTab, location, navigate]);
  63. const {attachments, isPending, isError, getResponseHeader, refetch} =
  64. useGroupEventAttachments({
  65. group,
  66. activeAttachmentsTab,
  67. });
  68. const {mutate: deleteAttachment} = useDeleteGroupEventAttachment();
  69. const handleDelete = (attachment: IssueAttachment) => {
  70. deleteAttachment({
  71. attachment,
  72. projectSlug: project.slug,
  73. activeAttachmentsTab,
  74. group,
  75. orgSlug: organization.slug,
  76. cursor: location.query.cursor as string | undefined,
  77. // We only want to filter by date/query/environment if we're using the Streamlined UI
  78. environment: hasStreamlinedUI ? (eventView.environment as string[]) : undefined,
  79. start: hasStreamlinedUI ? eventView.start : undefined,
  80. end: hasStreamlinedUI ? eventView.end : undefined,
  81. statsPeriod: hasStreamlinedUI ? eventView.statsPeriod : undefined,
  82. eventQuery: hasStreamlinedUI ? eventQuery : undefined,
  83. });
  84. };
  85. const renderAttachmentsTable = () => {
  86. if (isError) {
  87. return <LoadingError onRetry={refetch} message={t('Error loading attachments')} />;
  88. }
  89. return (
  90. <GroupEventAttachmentsTable
  91. isLoading={isPending}
  92. attachments={attachments}
  93. projectSlug={project.slug}
  94. groupId={group.id}
  95. onDelete={handleDelete}
  96. emptyMessage={
  97. activeAttachmentsTab === EventAttachmentFilter.CRASH_REPORTS
  98. ? t('No matching crash reports found')
  99. : t('No matching attachments found')
  100. }
  101. />
  102. );
  103. };
  104. const renderScreenshotGallery = () => {
  105. if (isError) {
  106. return <LoadingError onRetry={refetch} message={t('Error loading screenshots')} />;
  107. }
  108. if (isPending) {
  109. return <LoadingIndicator />;
  110. }
  111. if (attachments.length > 0) {
  112. return (
  113. <ScreenshotGrid>
  114. {attachments.map(screenshot => {
  115. return (
  116. <ScreenshotCard
  117. key={screenshot.id}
  118. eventAttachment={screenshot}
  119. eventId={screenshot.event_id}
  120. projectSlug={project.slug}
  121. groupId={group.id}
  122. onDelete={handleDelete}
  123. attachments={attachments}
  124. />
  125. );
  126. })}
  127. </ScreenshotGrid>
  128. );
  129. }
  130. return (
  131. <EmptyStateWarning>
  132. <p>{t('No screenshots found')}</p>
  133. </EmptyStateWarning>
  134. );
  135. };
  136. return (
  137. <Wrapper>
  138. {hasStreamlinedUI ? (
  139. <Flex justify="space-between">
  140. <FilterMessage align="center" gap={space(1)}>
  141. <IconFilter size="xs" />
  142. {t('Results are filtered by the selections above.')}
  143. </FilterMessage>
  144. <GroupEventAttachmentsFilter
  145. onChange={key => setPreviouslyUsedAttachmentsTab(key)}
  146. />
  147. </Flex>
  148. ) : (
  149. <GroupEventAttachmentsFilter />
  150. )}
  151. {activeAttachmentsTab === EventAttachmentFilter.SCREENSHOT
  152. ? renderScreenshotGallery()
  153. : renderAttachmentsTable()}
  154. <NoMarginPagination pageLinks={getResponseHeader?.('Link')} />
  155. </Wrapper>
  156. );
  157. }
  158. export default GroupEventAttachments;
  159. const ScreenshotGrid = styled('div')`
  160. display: grid;
  161. grid-template-columns: minmax(100px, 1fr);
  162. grid-template-rows: repeat(2, max-content);
  163. gap: ${space(2)};
  164. @media (min-width: ${p => p.theme.breakpoints.small}) {
  165. grid-template-columns: repeat(3, minmax(100px, 1fr));
  166. }
  167. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  168. grid-template-columns: repeat(4, minmax(100px, 1fr));
  169. }
  170. @media (min-width: ${p => p.theme.breakpoints.xxlarge}) {
  171. grid-template-columns: repeat(6, minmax(100px, 1fr));
  172. }
  173. `;
  174. const NoMarginPagination = styled(Pagination)`
  175. margin: 0;
  176. `;
  177. const Wrapper = styled('div')`
  178. display: flex;
  179. flex-direction: column;
  180. gap: ${space(2)};
  181. `;
  182. const FilterMessage = styled(Flex)``;