useReplaysForRegressionIssue.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import type {Location} from 'history';
  4. import {getSampleEventQuery} from 'sentry/components/events/eventStatisticalDetector/eventComparison/eventDisplay';
  5. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  6. import type {Event, Group, Organization} from 'sentry/types';
  7. import EventView from 'sentry/utils/discover/eventView';
  8. import {decodeScalar} from 'sentry/utils/queryString';
  9. import {DEFAULT_SORT} from 'sentry/utils/replays/fetchReplayList';
  10. import useApi from 'sentry/utils/useApi';
  11. import useCleanQueryParamsOnRouteLeave from 'sentry/utils/useCleanQueryParamsOnRouteLeave';
  12. import {REPLAY_LIST_FIELDS} from 'sentry/views/replays/types';
  13. function useReplaysForRegressionIssue({
  14. group,
  15. location,
  16. organization,
  17. event,
  18. }: {
  19. event: Event;
  20. group: Group;
  21. location: Location;
  22. organization: Organization;
  23. }) {
  24. const now = useRef(new Date().toISOString());
  25. const api = useApi();
  26. const [replayIds, setReplayIds] = useState<string[]>();
  27. const [fetchError, setFetchError] = useState();
  28. const {transaction, aggregateRange2, breakpoint} = event.occurrence?.evidenceData ?? {};
  29. const datetime = useMemo(
  30. () => ({
  31. start: new Date(breakpoint * 1000).toISOString(),
  32. end: now.current,
  33. }),
  34. [breakpoint]
  35. );
  36. const fetchReplayIds = useCallback(async () => {
  37. try {
  38. const response = await api.requestPromise(
  39. `/organizations/${organization.slug}/replay-count/`,
  40. {
  41. query: {
  42. returnIds: true,
  43. query: getSampleEventQuery({
  44. transaction,
  45. durationBaseline: aggregateRange2,
  46. addUpperBound: false,
  47. }),
  48. data_source: 'search_issues',
  49. project: ALL_ACCESS_PROJECTS,
  50. ...datetime,
  51. },
  52. }
  53. );
  54. setReplayIds(response[transaction] || []);
  55. } catch (error) {
  56. Sentry.captureException(error);
  57. setFetchError(error);
  58. }
  59. }, [api, organization.slug, transaction, aggregateRange2, datetime]);
  60. const eventView = useMemo(() => {
  61. if (!replayIds) {
  62. return null;
  63. }
  64. return EventView.fromSavedQuery({
  65. id: '',
  66. name: '',
  67. version: 2,
  68. fields: REPLAY_LIST_FIELDS,
  69. query: `id:[${String(replayIds)}]`,
  70. projects: [],
  71. orderby: decodeScalar(location.query.sort, DEFAULT_SORT),
  72. ...datetime,
  73. });
  74. }, [datetime, location.query.sort, replayIds]);
  75. useCleanQueryParamsOnRouteLeave({
  76. fieldsToClean: ['cursor'],
  77. shouldClean: newLocation => newLocation.pathname.includes(`/issues/${group.id}/`),
  78. });
  79. useEffect(() => {
  80. fetchReplayIds();
  81. }, [fetchReplayIds]);
  82. return {
  83. eventView,
  84. fetchError,
  85. pageLinks: null,
  86. };
  87. }
  88. export default useReplaysForRegressionIssue;