useTransactions.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import {useDiscoverQuery} from 'sentry/utils/discover/discoverQuery';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import {useLocation} from 'sentry/utils/useLocation';
  4. import useOrganization from 'sentry/utils/useOrganization';
  5. import {
  6. getRetryDelay,
  7. shouldRetryHandler,
  8. } from 'sentry/views/insights/common/utils/retryHandlers';
  9. type Transaction = {
  10. id: string;
  11. 'project.name': string;
  12. timestamp: string;
  13. trace: string;
  14. 'transaction.duration': number;
  15. };
  16. export function useTransactions(eventIDs: string[], referrer = 'use-transactions') {
  17. const location = useLocation();
  18. const {slug} = useOrganization();
  19. const eventView = EventView.fromNewQueryWithLocation(
  20. {
  21. fields: ['id', 'timestamp', 'project.name', 'transaction.duration', 'trace'],
  22. name: 'Transactions',
  23. version: 2,
  24. query: `id:[${eventIDs.join(',')}]`,
  25. },
  26. location
  27. );
  28. const enabled = Boolean(eventIDs.length);
  29. const response = useDiscoverQuery({
  30. eventView,
  31. location,
  32. orgSlug: slug,
  33. referrer,
  34. options: {
  35. enabled,
  36. refetchOnWindowFocus: false,
  37. retry: shouldRetryHandler,
  38. retryDelay: getRetryDelay,
  39. staleTime: Infinity,
  40. },
  41. });
  42. const data = (response.data?.data ?? []) as unknown as Transaction[];
  43. if (!enabled) {
  44. return {
  45. isFetching: false,
  46. isLoading: false,
  47. error: null,
  48. data: [],
  49. isEnabled: enabled,
  50. };
  51. }
  52. return {
  53. ...response,
  54. isEnabled: enabled,
  55. data,
  56. };
  57. }