1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import {useEffect, useState} from 'react';
- import * as Sentry from '@sentry/react';
- import {Client} from 'sentry/api';
- import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
- import {t} from 'sentry/locale';
- import {Organization, PageFilters, RequestState} from 'sentry/types';
- import {Trace} from 'sentry/types/profiling/core';
- import {defined} from 'sentry/utils';
- import useApi from 'sentry/utils/useApi';
- import useOrganization from 'sentry/utils/useOrganization';
- type ProfilesResult = {
- pageLinks: string | null;
- traces: Trace[];
- };
- interface UseProfilesOptions {
- query: string;
- cursor?: string;
- limit?: number;
- selection?: PageFilters;
- }
- function useProfiles({
- cursor,
- limit,
- query,
- selection,
- }: UseProfilesOptions): RequestState<ProfilesResult> {
- const api = useApi();
- const organization = useOrganization();
- const [requestState, setRequestState] = useState<RequestState<ProfilesResult>>({
- type: 'initial',
- });
- useEffect(() => {
- if (!defined(selection)) {
- return undefined;
- }
- setRequestState({type: 'loading'});
- fetchTraces(api, organization, {cursor, limit, query, selection})
- .then(([traces, , response]) => {
- setRequestState({
- type: 'resolved',
- data: {
- traces,
- pageLinks: response?.getResponseHeader('Link') ?? null,
- },
- });
- })
- .catch(err => {
- setRequestState({type: 'errored', error: t('Error: Unable to load profiles')});
- Sentry.captureException(err);
- });
- return () => api.clear();
- }, [api, organization, cursor, limit, query, selection]);
- return requestState;
- }
- function fetchTraces(
- api: Client,
- organization: Organization,
- {
- cursor,
- limit,
- query,
- selection,
- }: {
- cursor: string | undefined;
- limit: number | undefined;
- query: string;
- selection: PageFilters;
- }
- ) {
- return api.requestPromise(`/organizations/${organization.slug}/profiling/profiles/`, {
- method: 'GET',
- includeAllArgs: true,
- query: {
- cursor,
- query,
- per_page: limit,
- project: selection.projects,
- environment: selection.environments,
- ...normalizeDateTimeParams(selection.datetime),
- },
- });
- }
- export {useProfiles};
|