useReplaysFromIssue.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. project: -1,
  33. },
  34. }
  35. );
  36. setReplayIds(response[group.id] || []);
  37. } catch (error) {
  38. Sentry.captureException(error);
  39. setFetchError(error);
  40. }
  41. }, [api, organization.slug, group.id]);
  42. const eventView = useMemo(() => {
  43. if (!replayIds) {
  44. return null;
  45. }
  46. return EventView.fromSavedQuery({
  47. id: '',
  48. name: '',
  49. version: 2,
  50. fields: REPLAY_LIST_FIELDS,
  51. query: `id:[${String(replayIds)}]`,
  52. range: '14d',
  53. projects: [],
  54. orderby: decodeScalar(location.query.sort, DEFAULT_SORT),
  55. });
  56. }, [location.query.sort, replayIds]);
  57. useCleanQueryParamsOnRouteLeave({fieldsToClean: ['cursor']});
  58. useEffect(() => {
  59. fetchReplayIds();
  60. }, [fetchReplayIds]);
  61. return {
  62. eventView,
  63. fetchError,
  64. pageLinks: null,
  65. };
  66. }
  67. export default useReplayFromIssue;