useReplaysFromTransaction.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {useCallback, useEffect, useState} from 'react';
  2. import {Location} from 'history';
  3. import type {Client} from 'sentry/api';
  4. import type {Organization} from 'sentry/types';
  5. import {TableData} from 'sentry/utils/discover/discoverQuery';
  6. import EventView, {fromSorts} from 'sentry/utils/discover/eventView';
  7. import {doDiscoverQuery} from 'sentry/utils/discover/genericDiscoverQuery';
  8. import {decodeScalar} from 'sentry/utils/queryString';
  9. import fetchReplayList, {
  10. DEFAULT_SORT,
  11. REPLAY_LIST_FIELDS,
  12. } from 'sentry/utils/replays/fetchReplayList';
  13. import useApi from 'sentry/utils/useApi';
  14. type State = Awaited<ReturnType<typeof fetchReplayList>> & {
  15. eventView: undefined | EventView;
  16. };
  17. type Options = {
  18. eventsWithReplaysView: EventView;
  19. location: Location;
  20. organization: Organization;
  21. };
  22. function useReplaysFromTransaction({
  23. eventsWithReplaysView,
  24. location,
  25. organization,
  26. }: Options) {
  27. const api = useApi();
  28. const [data, setData] = useState<State>({
  29. fetchError: undefined,
  30. isFetching: true,
  31. pageLinks: null,
  32. replays: [],
  33. eventView: undefined,
  34. });
  35. const load = useCallback(async () => {
  36. const replayIds = await fetchReplayIds({
  37. api,
  38. eventView: eventsWithReplaysView,
  39. location,
  40. organization,
  41. });
  42. const eventView = EventView.fromNewQueryWithLocation(
  43. {
  44. id: '',
  45. name: 'Replays within a transaction',
  46. version: 2,
  47. fields: REPLAY_LIST_FIELDS,
  48. projects: [],
  49. query: `id:[${String(replayIds)}]`,
  50. },
  51. location
  52. );
  53. eventView.sorts = fromSorts(decodeScalar(location.query.sort, DEFAULT_SORT));
  54. const listData = await fetchReplayList({
  55. api,
  56. eventView,
  57. location,
  58. organization,
  59. });
  60. setData({
  61. ...listData,
  62. eventView,
  63. });
  64. }, [api, eventsWithReplaysView, location, organization]);
  65. useEffect(() => {
  66. load();
  67. }, [load]);
  68. return data;
  69. }
  70. async function fetchReplayIds({
  71. api,
  72. eventView,
  73. location,
  74. organization,
  75. }: {
  76. api: Client;
  77. eventView: EventView;
  78. location: Location;
  79. organization: Organization;
  80. }) {
  81. try {
  82. const [data] = await doDiscoverQuery<TableData>(
  83. api,
  84. `/organizations/${organization.slug}/events/`,
  85. eventView.getEventsAPIPayload(location)
  86. );
  87. return data.data.map(record => String(record.replayId));
  88. } catch (err) {
  89. return null;
  90. }
  91. }
  92. export default useReplaysFromTransaction;