useTransactions.tsx 1.4 KB

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