useReplayList.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. };
  13. type State = Awaited<ReturnType<typeof fetchReplayList>>;
  14. type Result = State;
  15. function useReplayList({eventView, location, organization}: Options): Result {
  16. const api = useApi();
  17. const [data, setData] = useState<State>({
  18. fetchError: undefined,
  19. isFetching: true,
  20. pageLinks: null,
  21. replays: [],
  22. });
  23. const loadReplays = useCallback(async () => {
  24. setData(prev => ({
  25. ...prev,
  26. isFetching: true,
  27. }));
  28. const response = await fetchReplayList({
  29. api,
  30. organization,
  31. location,
  32. eventView,
  33. });
  34. setData(response);
  35. }, [api, organization, location, eventView]);
  36. useEffect(() => {
  37. loadReplays();
  38. }, [loadReplays]);
  39. return data;
  40. }
  41. export default useReplayList;