fetchReplayList.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import * as Sentry from '@sentry/react';
  2. import type {Location} from 'history';
  3. import type {Client} from 'sentry/api';
  4. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  5. import type {Organization} from 'sentry/types';
  6. import type EventView from 'sentry/utils/discover/eventView';
  7. import {mapResponseToReplayRecord} from 'sentry/utils/replays/replayDataUtils';
  8. import type RequestError from 'sentry/utils/requestError/requestError';
  9. import type {ReplayListRecord} from 'sentry/views/replays/types';
  10. export const DEFAULT_SORT = '-started_at';
  11. type State = {
  12. fetchError: undefined | RequestError;
  13. pageLinks: null | string;
  14. replays: undefined | ReplayListRecord[];
  15. };
  16. type Result = State;
  17. type Props = {
  18. api: Client;
  19. eventView: EventView;
  20. location: Location;
  21. organization: Organization;
  22. perPage?: number;
  23. queryReferrer?: 'issueReplays';
  24. };
  25. async function fetchReplayList({
  26. api,
  27. organization,
  28. location,
  29. eventView,
  30. queryReferrer,
  31. perPage,
  32. }: Props): Promise<Result> {
  33. try {
  34. const path = `/organizations/${organization.slug}/replays/`;
  35. const payload = eventView.getEventsAPIPayload(location);
  36. // HACK!!! Because the sort field needs to be in the eventView, but I cannot
  37. // ask the server for compound fields like `os.name`.
  38. payload.field = payload.field.map(field => field.split('.')[0]);
  39. if (perPage) {
  40. payload.per_page = perPage;
  41. }
  42. // unique list
  43. payload.field = Array.from(new Set(payload.field));
  44. const [{data}, _textStatus, resp] = await api.requestPromise(path, {
  45. includeAllArgs: true,
  46. query: {
  47. ...payload,
  48. cursor: location.query.cursor,
  49. // when queryReferrer === 'issueReplays' we override the global view check on the backend
  50. // we also require a project param otherwise we won't yield results
  51. queryReferrer,
  52. project: queryReferrer === 'issueReplays' ? ALL_ACCESS_PROJECTS : payload.project,
  53. },
  54. });
  55. const pageLinks = resp?.getResponseHeader('Link') ?? '';
  56. return {
  57. fetchError: undefined,
  58. pageLinks,
  59. replays: data.map(mapResponseToReplayRecord),
  60. };
  61. } catch (error) {
  62. if (error.responseJSON?.detail) {
  63. return {
  64. fetchError: error.responseJSON.detail,
  65. pageLinks: null,
  66. replays: [],
  67. };
  68. }
  69. Sentry.captureException(error);
  70. return {
  71. fetchError: error,
  72. pageLinks: null,
  73. replays: [],
  74. };
  75. }
  76. }
  77. export default fetchReplayList;