useTraceMeta.tsx 2.1 KB

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