useReplaysFromIssue.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, 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 {getReplayListFields} 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 fetchReplayIds = useCallback(async () => {
  25. try {
  26. const response = await api.requestPromise(
  27. `/organizations/${organization.slug}/replay-count/`,
  28. {
  29. query: {
  30. returnIds: true,
  31. query: `issue.id:[${group.id}]`,
  32. statsPeriod: '14d',
  33. project: ALL_ACCESS_PROJECTS,
  34. },
  35. }
  36. );
  37. setReplayIds(response[group.id] || []);
  38. } catch (error) {
  39. Sentry.captureException(error);
  40. setFetchError(error);
  41. }
  42. }, [api, organization.slug, group.id]);
  43. const eventView = useMemo(() => {
  44. if (!replayIds) {
  45. return null;
  46. }
  47. return EventView.fromSavedQuery({
  48. id: '',
  49. name: '',
  50. version: 2,
  51. fields: getReplayListFields(organization),
  52. query: `id:[${String(replayIds)}]`,
  53. range: '14d',
  54. projects: [],
  55. orderby: decodeScalar(location.query.sort, DEFAULT_SORT),
  56. });
  57. }, [location.query.sort, replayIds, organization]);
  58. useCleanQueryParamsOnRouteLeave({fieldsToClean: ['cursor']});
  59. useEffect(() => {
  60. fetchReplayIds();
  61. }, [fetchReplayIds]);
  62. return {
  63. eventView,
  64. fetchError,
  65. pageLinks: null,
  66. };
  67. }
  68. export default useReplayFromIssue;