useGroupEventAttachments.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import type {IssueAttachment} from 'sentry/types/group';
  2. import {
  3. type ApiQueryKey,
  4. useApiQuery,
  5. type UseApiQueryOptions,
  6. } from 'sentry/utils/queryClient';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import useOrganization from 'sentry/utils/useOrganization';
  9. interface UseGroupEventAttachmentsOptions {
  10. activeAttachmentsTab: 'all' | 'onlyCrash' | 'screenshot';
  11. groupId: string;
  12. options?: Pick<UseApiQueryOptions<IssueAttachment[]>, 'placeholderData'>;
  13. }
  14. interface MakeFetchGroupEventAttachmentsQueryKeyOptions
  15. extends UseGroupEventAttachmentsOptions {
  16. cursor: string | undefined;
  17. environment: string[] | string | undefined;
  18. orgSlug: string;
  19. }
  20. type GroupEventAttachmentsTypeFilter =
  21. | 'event.minidump'
  22. | 'event.applecrashreport'
  23. | 'event.screenshot';
  24. interface GroupEventAttachmentsQuery {
  25. cursor?: string;
  26. environment?: string[] | string;
  27. per_page?: string;
  28. screenshot?: '1';
  29. types?: `${GroupEventAttachmentsTypeFilter}` | `${GroupEventAttachmentsTypeFilter}`[];
  30. }
  31. export const makeFetchGroupEventAttachmentsQueryKey = ({
  32. activeAttachmentsTab,
  33. groupId,
  34. orgSlug,
  35. cursor,
  36. environment,
  37. }: MakeFetchGroupEventAttachmentsQueryKeyOptions): ApiQueryKey => {
  38. const query: GroupEventAttachmentsQuery = {};
  39. if (environment) {
  40. query.environment = environment;
  41. }
  42. if (cursor) {
  43. query.cursor = cursor;
  44. }
  45. if (activeAttachmentsTab === 'screenshot') {
  46. query.screenshot = '1';
  47. } else if (activeAttachmentsTab === 'onlyCrash') {
  48. query.types = ['event.minidump', 'event.applecrashreport'];
  49. }
  50. return [`/organizations/${orgSlug}/issues/${groupId}/attachments/`, {query}];
  51. };
  52. export function useGroupEventAttachments({
  53. groupId,
  54. activeAttachmentsTab,
  55. options,
  56. }: UseGroupEventAttachmentsOptions) {
  57. const organization = useOrganization();
  58. const location = useLocation();
  59. const {
  60. data: attachments = [],
  61. isPending,
  62. isError,
  63. getResponseHeader,
  64. refetch,
  65. } = useApiQuery<IssueAttachment[]>(
  66. makeFetchGroupEventAttachmentsQueryKey({
  67. activeAttachmentsTab,
  68. groupId,
  69. orgSlug: organization.slug,
  70. cursor: location.query.cursor as string | undefined,
  71. environment: location.query.environment as string[] | string | undefined,
  72. }),
  73. {...options, staleTime: 60_000}
  74. );
  75. return {
  76. attachments,
  77. isPending,
  78. isError,
  79. getResponseHeader,
  80. refetch,
  81. };
  82. }