useTraceQueryParams.tsx 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import {useMemo} from 'react';
  2. import * as qs from 'query-string';
  3. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  4. import {decodeScalar} from 'sentry/utils/queryString';
  5. export interface TraceViewQueryParams {
  6. end: string | undefined;
  7. start: string | undefined;
  8. statsPeriod: string | undefined;
  9. timestamp: number | undefined;
  10. useSpans: number;
  11. }
  12. export function useTraceQueryParams(
  13. params?: Partial<TraceViewQueryParams>
  14. ): TraceViewQueryParams {
  15. return useMemo(() => {
  16. const normalizedParams = normalizeDateTimeParams(qs.parse(location.search), {
  17. allowAbsolutePageDatetime: true,
  18. });
  19. const start = decodeScalar(normalizedParams.start);
  20. const timestamp: string | undefined = decodeScalar(normalizedParams.timestamp);
  21. const end = decodeScalar(normalizedParams.end);
  22. const statsPeriod = decodeScalar(normalizedParams.statsPeriod);
  23. const numberTimestamp = timestamp ? Number(timestamp) : undefined;
  24. return {
  25. start: start ?? params?.start,
  26. end: end ?? params?.end,
  27. statsPeriod: statsPeriod ?? params?.statsPeriod,
  28. timestamp: numberTimestamp ?? params?.timestamp,
  29. useSpans: 1,
  30. };
  31. }, [params]);
  32. }