useProfiles.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 {Trace} 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 ProfilesResult = {
  12. pageLinks: string | null;
  13. traces: Trace[];
  14. };
  15. interface UseProfilesOptions {
  16. query: string;
  17. cursor?: string;
  18. limit?: number;
  19. selection?: PageFilters;
  20. }
  21. function useProfiles({
  22. cursor,
  23. limit,
  24. query,
  25. selection,
  26. }: UseProfilesOptions): RequestState<ProfilesResult> {
  27. const api = useApi();
  28. const organization = useOrganization();
  29. const [requestState, setRequestState] = useState<RequestState<ProfilesResult>>({
  30. type: 'initial',
  31. });
  32. useEffect(() => {
  33. if (!defined(selection)) {
  34. return undefined;
  35. }
  36. setRequestState({type: 'loading'});
  37. fetchTraces(api, organization, {cursor, limit, query, selection})
  38. .then(([traces, , response]) => {
  39. setRequestState({
  40. type: 'resolved',
  41. data: {
  42. traces,
  43. pageLinks: response?.getResponseHeader('Link') ?? null,
  44. },
  45. });
  46. })
  47. .catch(err => {
  48. setRequestState({type: 'errored', error: t('Error: Unable to load profiles')});
  49. Sentry.captureException(err);
  50. });
  51. return () => api.clear();
  52. }, [api, organization, cursor, limit, query, selection]);
  53. return requestState;
  54. }
  55. function fetchTraces(
  56. api: Client,
  57. organization: Organization,
  58. {
  59. cursor,
  60. limit,
  61. query,
  62. selection,
  63. }: {
  64. cursor: string | undefined;
  65. limit: number | undefined;
  66. query: string;
  67. selection: PageFilters;
  68. }
  69. ) {
  70. return api.requestPromise(`/organizations/${organization.slug}/profiling/profiles/`, {
  71. method: 'GET',
  72. includeAllArgs: true,
  73. query: {
  74. cursor,
  75. query,
  76. per_page: limit,
  77. project: selection.projects,
  78. environment: selection.environments,
  79. ...normalizeDateTimeParams(selection.datetime),
  80. },
  81. });
  82. }
  83. export {useProfiles};