useTraceMeta.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import {useMemo} from 'react';
  2. import type {Location} from 'history';
  3. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  4. import {ALL_ACCESS_PROJECTS} from 'sentry/constants/pageFilters';
  5. import type {PageFilters} from 'sentry/types';
  6. import type {TraceMeta} from 'sentry/utils/performance/quickTrace/types';
  7. import {useApiQuery, type UseApiQueryResult} from 'sentry/utils/queryClient';
  8. import {decodeScalar} from 'sentry/utils/queryString';
  9. import {useLocation} from 'sentry/utils/useLocation';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import usePageFilters from 'sentry/utils/usePageFilters';
  12. import {useParams} from 'sentry/utils/useParams';
  13. function getMetaQueryParams(
  14. query: Location['query'],
  15. filters: Partial<PageFilters> = {}
  16. ): {
  17. project: string;
  18. statsPeriod: string | undefined;
  19. timestamp: string | undefined;
  20. } {
  21. const normalizedParams = normalizeDateTimeParams(query, {
  22. allowAbsolutePageDatetime: true,
  23. });
  24. let statsPeriod = decodeScalar(normalizedParams.statsPeriod);
  25. const project = decodeScalar(normalizedParams.project, ALL_ACCESS_PROJECTS + '');
  26. const timestamp = decodeScalar(normalizedParams.timestamp);
  27. if (statsPeriod === undefined && !timestamp && filters.datetime?.period) {
  28. statsPeriod = filters.datetime.period;
  29. }
  30. return {statsPeriod, project, timestamp};
  31. }
  32. export function useTraceMeta(): UseApiQueryResult<TraceMeta | null, any> {
  33. const filters = usePageFilters();
  34. const location = useLocation();
  35. const organization = useOrganization();
  36. const params = useParams<{traceSlug?: string}>();
  37. const queryParams = useMemo(() => {
  38. return getMetaQueryParams(location.query, filters.selection);
  39. // eslint-disable-next-line react-hooks/exhaustive-deps
  40. }, []);
  41. return useApiQuery(
  42. [
  43. `/organizations/${organization.slug}/events-trace-meta/${params.traceSlug ?? ''}/`,
  44. {query: queryParams},
  45. ],
  46. {
  47. staleTime: Infinity,
  48. enabled: !!params.traceSlug && !!organization.slug,
  49. }
  50. );
  51. }