useReplaysFromIssue.tsx 2.3 KB

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