useReplayList.tsx 1.4 KB

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