useTransactions.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. type Transaction = {
  6. id: string;
  7. 'project.name': string;
  8. timestamp: string;
  9. 'transaction.duration': number;
  10. };
  11. export function useTransactions(eventIDs: string[], referrer = 'use-transactions') {
  12. const location = useLocation();
  13. const {slug} = useOrganization();
  14. const eventView = EventView.fromNewQueryWithLocation(
  15. {
  16. fields: ['id', 'timestamp', 'project.name', 'transaction.duration'],
  17. name: 'Transactions',
  18. projects: [1],
  19. version: 2,
  20. query: `id:[${eventIDs.join(',')}]`,
  21. },
  22. location
  23. );
  24. const enabled = Boolean(eventIDs.length);
  25. const response = useDiscoverQuery({
  26. eventView,
  27. location,
  28. orgSlug: slug,
  29. referrer,
  30. options: {
  31. enabled,
  32. },
  33. });
  34. const data = (response.data?.data ?? []) as unknown as Transaction[];
  35. if (!enabled) {
  36. return {
  37. isFetching: false,
  38. isLoading: false,
  39. data: [],
  40. };
  41. }
  42. return {
  43. ...response,
  44. data,
  45. };
  46. }