useReplayList.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import {useCallback, useEffect, useState} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import type {Organization} from 'sentry/types';
  4. import EventView from 'sentry/utils/discover/eventView';
  5. import {mapResponseToReplayRecord} from 'sentry/utils/replays/replayDataUtils';
  6. import type RequestError from 'sentry/utils/requestError/requestError';
  7. import useApi from 'sentry/utils/useApi';
  8. import {useLocation} from 'sentry/utils/useLocation';
  9. import type {ReplayListLocationQuery, ReplayListRecord} from 'sentry/views/replays/types';
  10. export const DEFAULT_SORT = '-startedAt';
  11. export const REPLAY_LIST_FIELDS = [
  12. 'countErrors',
  13. 'duration',
  14. 'finishedAt',
  15. 'id',
  16. 'projectId',
  17. 'startedAt',
  18. 'urls',
  19. 'user',
  20. ];
  21. type Options = {
  22. eventView: EventView;
  23. organization: Organization;
  24. };
  25. type State = {
  26. fetchError: undefined | RequestError;
  27. isFetching: boolean;
  28. pageLinks: null | string;
  29. replays: undefined | ReplayListRecord[];
  30. };
  31. type Result = State;
  32. function useReplayList({eventView, organization}: Options): Result {
  33. const api = useApi();
  34. const location = useLocation<ReplayListLocationQuery>();
  35. const [data, setData] = useState<State>({
  36. fetchError: undefined,
  37. isFetching: true,
  38. pageLinks: null,
  39. replays: [],
  40. });
  41. const init = useCallback(async () => {
  42. try {
  43. setData(prev => ({
  44. ...prev,
  45. isFetching: true,
  46. }));
  47. const path = `/organizations/${organization.slug}/replays/`;
  48. const [{data: records}, _textStatus, resp] = await api.requestPromise(path, {
  49. includeAllArgs: true,
  50. query: {
  51. ...eventView.getEventsAPIPayload(location),
  52. cursor: location.query.cursor,
  53. },
  54. });
  55. const pageLinks = resp?.getResponseHeader('Link') ?? '';
  56. setData({
  57. fetchError: undefined,
  58. isFetching: false,
  59. pageLinks,
  60. replays: records.map(mapResponseToReplayRecord),
  61. });
  62. } catch (error) {
  63. Sentry.captureException(error);
  64. setData({
  65. fetchError: error,
  66. isFetching: false,
  67. pageLinks: null,
  68. replays: [],
  69. });
  70. }
  71. }, [api, organization, location, eventView]);
  72. useEffect(() => {
  73. init();
  74. }, [init]);
  75. return data;
  76. }
  77. export default useReplayList;