useReplaysForRegressionIssue.tsx 2.9 KB

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