useProfileTransactions.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import {useEffect, useState} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import {Client} from 'sentry/api';
  4. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  5. import {t} from 'sentry/locale';
  6. import {Organization, PageFilters, RequestState} from 'sentry/types';
  7. import {ProfileTransaction} from 'sentry/types/profiling/core';
  8. import {defined} from 'sentry/utils';
  9. import useApi from 'sentry/utils/useApi';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. type ProfileTransactionsResult = {
  12. pageLinks: string | null;
  13. transactions: ProfileTransaction[];
  14. };
  15. interface UseProfileTransactionsOptions {
  16. query: string;
  17. sort: string;
  18. cursor?: string;
  19. limit?: number;
  20. selection?: PageFilters;
  21. }
  22. function useProfileTransactions({
  23. cursor,
  24. sort,
  25. limit,
  26. query,
  27. selection,
  28. }: UseProfileTransactionsOptions): RequestState<ProfileTransactionsResult> {
  29. const api = useApi();
  30. const organization = useOrganization();
  31. const [requestState, setRequestState] = useState<
  32. RequestState<ProfileTransactionsResult>
  33. >({
  34. type: 'initial',
  35. });
  36. useEffect(() => {
  37. if (!defined(selection)) {
  38. return undefined;
  39. }
  40. setRequestState({type: 'loading'});
  41. fetchTransactions(api, organization, {cursor, limit, query, selection, sort})
  42. .then(([transactions, , response]) => {
  43. setRequestState({
  44. type: 'resolved',
  45. data: {
  46. transactions,
  47. pageLinks: response?.getResponseHeader('Link') ?? null,
  48. },
  49. });
  50. })
  51. .catch(err => {
  52. setRequestState({
  53. type: 'errored',
  54. error: t('Error: Unable to load transactions'),
  55. });
  56. Sentry.captureException(err);
  57. });
  58. return () => api.clear();
  59. }, [api, organization, cursor, limit, query, selection, sort]);
  60. return requestState;
  61. }
  62. function fetchTransactions(
  63. api: Client,
  64. organization: Organization,
  65. {
  66. cursor,
  67. limit,
  68. query,
  69. selection,
  70. sort,
  71. }: {
  72. cursor: string | undefined;
  73. limit: number | undefined;
  74. query: string;
  75. selection: PageFilters;
  76. sort: string;
  77. }
  78. ) {
  79. return api.requestPromise(
  80. `/organizations/${organization.slug}/profiling/transactions/`,
  81. {
  82. method: 'GET',
  83. includeAllArgs: true,
  84. query: {
  85. cursor,
  86. sort,
  87. query,
  88. per_page: limit,
  89. project: selection.projects,
  90. environment: selection.environments,
  91. ...normalizeDateTimeParams(selection.datetime),
  92. },
  93. }
  94. );
  95. }
  96. export {useProfileTransactions};