useTraceMeta.tsx 1.9 KB

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