useModuleURL.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  2. import useOrganization from 'sentry/utils/useOrganization';
  3. import {BASE_URL as RESOURCES_BASE_URL} from 'sentry/views/insights/browser/resources/settings';
  4. import {BASE_URL as VITALS_BASE_URL} from 'sentry/views/insights/browser/webVitals/settings';
  5. import {BASE_URL as CACHE_BASE_URL} from 'sentry/views/insights/cache/settings';
  6. import {BASE_URL as DB_BASE_URL} from 'sentry/views/insights/database/settings';
  7. import {BASE_URL as HTTP_BASE_URL} from 'sentry/views/insights/http/settings';
  8. import {BASE_URL as AI_BASE_URL} from 'sentry/views/insights/llmMonitoring/settings';
  9. import {BASE_URL as APP_STARTS_BASE_URL} from 'sentry/views/insights/mobile/appStarts/settings';
  10. import {BASE_URL as SCREEN_LOADS_BASE_URL} from 'sentry/views/insights/mobile/screenload/settings';
  11. import {BASE_URL as MOBILE_SCREENS_BASE_URL} from 'sentry/views/insights/mobile/screens/settings';
  12. import {BASE_URL as MOBILE_UI_BASE_URL} from 'sentry/views/insights/mobile/ui/settings';
  13. import {BASE_URL as QUEUE_BASE_URL} from 'sentry/views/insights/queues/settings';
  14. import {INSIGHTS_BASE_URL} from 'sentry/views/insights/settings';
  15. import {ModuleName} from 'sentry/views/insights/types';
  16. export const MODULE_BASE_URLS: Record<ModuleName, string> = {
  17. [ModuleName.DB]: DB_BASE_URL,
  18. [ModuleName.HTTP]: HTTP_BASE_URL,
  19. [ModuleName.CACHE]: CACHE_BASE_URL,
  20. [ModuleName.QUEUE]: QUEUE_BASE_URL,
  21. [ModuleName.SCREEN_LOAD]: SCREEN_LOADS_BASE_URL,
  22. [ModuleName.APP_START]: APP_STARTS_BASE_URL,
  23. [ModuleName.VITAL]: VITALS_BASE_URL,
  24. [ModuleName.RESOURCE]: RESOURCES_BASE_URL,
  25. [ModuleName.AI]: AI_BASE_URL,
  26. [ModuleName.MOBILE_UI]: MOBILE_UI_BASE_URL,
  27. [ModuleName.MOBILE_SCREENS]: MOBILE_SCREENS_BASE_URL,
  28. [ModuleName.OTHER]: '',
  29. };
  30. type ModuleNameStrings = `${ModuleName}`;
  31. type RoutableModuleNames = Exclude<ModuleNameStrings, '' | 'other'>;
  32. export const useModuleURL = (
  33. moduleName: RoutableModuleNames,
  34. bare: boolean = false
  35. ): string => {
  36. const builder = useModuleURLBuilder(bare);
  37. return builder(moduleName);
  38. };
  39. type URLBuilder = (moduleName: RoutableModuleNames) => string;
  40. export function useModuleURLBuilder(bare: boolean = false): URLBuilder {
  41. const organization = useOrganization({allowNull: true}); // Some parts of the app, like the main sidebar, render even if the organization isn't available (during loading, or at all).
  42. if (!organization) {
  43. // If there isn't an organization, items that link to modules won't be visible, so this is a fallback just-in-case, and isn't trying too hard to be useful
  44. return () => '';
  45. }
  46. const {slug} = organization;
  47. return function (moduleName: RoutableModuleNames) {
  48. return bare
  49. ? `${INSIGHTS_BASE_URL}/${MODULE_BASE_URLS[moduleName]}`
  50. : normalizeUrl(
  51. `/organizations/${slug}/${INSIGHTS_BASE_URL}/${MODULE_BASE_URLS[moduleName]}`
  52. );
  53. };
  54. }