useReplaysFromIssue.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import {useCallback, useEffect, useMemo, useState} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import type {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. export default 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. environment: location.query.environment,
  37. project: ALL_ACCESS_PROJECTS,
  38. },
  39. }
  40. );
  41. setReplayIds(response[group.id] || []);
  42. } catch (error) {
  43. Sentry.captureException(error);
  44. setFetchError(error);
  45. }
  46. }, [api, organization.slug, group.id, dataSource, location.query.environment]);
  47. const eventView = useMemo(() => {
  48. if (!replayIds || !replayIds.length) {
  49. return null;
  50. }
  51. return EventView.fromSavedQuery({
  52. id: '',
  53. name: '',
  54. version: 2,
  55. fields: REPLAY_LIST_FIELDS,
  56. query: replayIds.length ? `id:[${String(replayIds)}]` : `id:1`,
  57. range: '14d',
  58. projects: [],
  59. orderby: decodeScalar(location.query.sort, DEFAULT_SORT),
  60. });
  61. }, [location.query.sort, replayIds]);
  62. useCleanQueryParamsOnRouteLeave({
  63. fieldsToClean: ['cursor'],
  64. shouldClean: newLocation => newLocation.pathname.includes(`/issues/${group.id}/`),
  65. });
  66. useEffect(() => {
  67. fetchReplayIds();
  68. }, [fetchReplayIds]);
  69. return {
  70. eventView,
  71. fetchError,
  72. isFetching: replayIds === undefined,
  73. pageLinks: null,
  74. };
  75. }