useLocation.tsx 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import {useMemo} from 'react';
  2. import {useLocation as useReactRouter6Location} from 'react-router-dom';
  3. import type {Location, Query} from 'history';
  4. import {useRouteContext} from 'sentry/utils/useRouteContext';
  5. import {location6ToLocation3} from './reactRouter6Compat/location';
  6. type DefaultQuery<T = string> = {
  7. [key: string]: T | T[] | null | undefined;
  8. };
  9. export function useLocation<Q extends Query = DefaultQuery>(): Location<Q> {
  10. // When running in test mode we still read from the legacy route context to
  11. // keep test compatability while we fully migrate to react router 6
  12. const legacyRouterContext = useRouteContext();
  13. if (legacyRouterContext) {
  14. return legacyRouterContext.location;
  15. }
  16. // biome-ignore lint/correctness/useHookAtTopLevel: react-router 6 migration
  17. const router6Location = useReactRouter6Location();
  18. // biome-ignore lint/correctness/useHookAtTopLevel: react-router 6 migration
  19. const location = useMemo(
  20. () => location6ToLocation3<Q>(router6Location),
  21. [router6Location]
  22. );
  23. return location;
  24. }