useModuleNameFromUrl.tsx 811 B

1234567891011121314151617181920212223
  1. import {useLocation} from 'sentry/utils/useLocation';
  2. import {MODULE_BASE_URLS} from 'sentry/views/insights/common/utils/useModuleURL';
  3. import {INSIGHTS_BASE_URL} from 'sentry/views/insights/settings';
  4. import type {ModuleName} from 'sentry/views/insights/types';
  5. export function useModuleNameFromUrl(): ModuleName | null {
  6. const {pathname} = useLocation();
  7. // Reverse MODULE_BASE_URLS
  8. const urlToModuleNameMap: Record<string, ModuleName> = Object.fromEntries(
  9. Object.entries(MODULE_BASE_URLS)
  10. .map(([key, value]) => [value, key])
  11. .filter(([key]) => Boolean(key))
  12. );
  13. const moduleKey = Object.keys(urlToModuleNameMap).find(key => {
  14. return pathname.startsWith(`/${INSIGHTS_BASE_URL}/${key}`);
  15. });
  16. if (moduleKey) {
  17. return urlToModuleNameMap[moduleKey];
  18. }
  19. return null;
  20. }