fetchReplayList.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. queryReferrer?: 'issueReplays';
  23. };
  24. async function fetchReplayList({
  25. api,
  26. organization,
  27. location,
  28. eventView,
  29. queryReferrer,
  30. }: Props): Promise<Result> {
  31. try {
  32. const path = `/organizations/${organization.slug}/replays/`;
  33. const payload = eventView.getEventsAPIPayload(location);
  34. // HACK!!! Because the sort field needs to be in the eventView, but I cannot
  35. // ask the server for compound fields like `os.name`.
  36. payload.field = payload.field.map(field => field.split('.')[0]);
  37. // unique list
  38. payload.field = Array.from(new Set(payload.field));
  39. const [{data}, _textStatus, resp] = await api.requestPromise(path, {
  40. includeAllArgs: true,
  41. query: {
  42. ...payload,
  43. cursor: location.query.cursor,
  44. // when queryReferrer === 'issueReplays' we override the global view check on the backend
  45. // we also require a project param otherwise we won't yield results
  46. queryReferrer,
  47. project: queryReferrer === 'issueReplays' ? ALL_ACCESS_PROJECTS : payload.project,
  48. },
  49. });
  50. const pageLinks = resp?.getResponseHeader('Link') ?? '';
  51. return {
  52. fetchError: undefined,
  53. pageLinks,
  54. replays: data.map(mapResponseToReplayRecord),
  55. };
  56. } catch (error) {
  57. if (error.responseJSON?.detail) {
  58. return {
  59. fetchError: error.responseJSON.detail,
  60. pageLinks: null,
  61. replays: [],
  62. };
  63. }
  64. Sentry.captureException(error);
  65. return {
  66. fetchError: error,
  67. pageLinks: null,
  68. replays: [],
  69. };
  70. }
  71. }
  72. export default fetchReplayList;