useReplayList.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import {useCallback, useEffect, useState} from 'react';
  2. import {Location} from 'history';
  3. import type {Organization} from 'sentry/types';
  4. import type EventView from 'sentry/utils/discover/eventView';
  5. import fetchReplayList from 'sentry/utils/replays/fetchReplayList';
  6. import useApi from 'sentry/utils/useApi';
  7. import type {ReplayListLocationQuery} from 'sentry/views/replays/types';
  8. type Options = {
  9. eventView: EventView;
  10. location: Location<ReplayListLocationQuery>;
  11. organization: Organization;
  12. queryReferrer?: 'issueReplays';
  13. };
  14. type State = Awaited<ReturnType<typeof fetchReplayList>> & {isFetching: boolean};
  15. type Result = State;
  16. function useReplayList({
  17. eventView,
  18. location,
  19. organization,
  20. queryReferrer,
  21. }: Options): Result {
  22. const api = useApi();
  23. const [data, setData] = useState<State>({
  24. fetchError: undefined,
  25. isFetching: true,
  26. pageLinks: null,
  27. replays: [],
  28. });
  29. const loadReplays = useCallback(async () => {
  30. api.clear();
  31. setData(prev => ({
  32. ...prev,
  33. isFetching: true,
  34. }));
  35. const response = await fetchReplayList({
  36. api,
  37. organization,
  38. location,
  39. eventView,
  40. queryReferrer,
  41. });
  42. setData({...response, isFetching: false});
  43. }, [api, organization, location, eventView, queryReferrer]);
  44. useEffect(() => {
  45. loadReplays();
  46. }, [loadReplays]);
  47. return data;
  48. }
  49. export default useReplayList;