useReplaysFromIssue.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import {useCallback, useEffect, useMemo, useState} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import {Location} from 'history';
  4. import type {Group, Organization} from 'sentry/types';
  5. import EventView from 'sentry/utils/discover/eventView';
  6. import {decodeScalar} from 'sentry/utils/queryString';
  7. import {DEFAULT_SORT} from 'sentry/utils/replays/fetchReplayList';
  8. import useApi from 'sentry/utils/useApi';
  9. import useCleanQueryParamsOnRouteLeave from 'sentry/utils/useCleanQueryParamsOnRouteLeave';
  10. import {REPLAY_LIST_FIELDS} from 'sentry/views/replays/types';
  11. function useReplayFromIssue({
  12. group,
  13. location,
  14. organization,
  15. }: {
  16. group: Group;
  17. location: Location;
  18. organization: Organization;
  19. }) {
  20. const api = useApi();
  21. const [replayIds, setReplayIds] = useState<string[]>();
  22. const [fetchError, setFetchError] = useState();
  23. const fetchReplayIds = useCallback(async () => {
  24. try {
  25. const response = await api.requestPromise(
  26. `/organizations/${organization.slug}/replay-count/`,
  27. {
  28. query: {
  29. returnIds: true,
  30. query: `issue.id:[${group.id}]`,
  31. statsPeriod: '14d',
  32. },
  33. }
  34. );
  35. setReplayIds(response[group.id] || []);
  36. } catch (error) {
  37. Sentry.captureException(error);
  38. setFetchError(error);
  39. }
  40. }, [api, organization.slug, group.id]);
  41. const eventView = useMemo(() => {
  42. if (!replayIds) {
  43. return null;
  44. }
  45. return EventView.fromSavedQuery({
  46. id: '',
  47. name: '',
  48. version: 2,
  49. fields: REPLAY_LIST_FIELDS,
  50. query: `id:[${String(replayIds)}]`,
  51. range: '14d',
  52. projects: [],
  53. orderby: decodeScalar(location.query.sort, DEFAULT_SORT),
  54. });
  55. }, [location.query.sort, replayIds]);
  56. useCleanQueryParamsOnRouteLeave({fieldsToClean: ['cursor']});
  57. useEffect(() => {
  58. fetchReplayIds();
  59. }, [fetchReplayIds]);
  60. return {
  61. eventView,
  62. fetchError,
  63. pageLinks: null,
  64. };
  65. }
  66. export default useReplayFromIssue;