useModuleURL.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import useOrganization from 'sentry/utils/useOrganization';
  2. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  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_UI_BASE_URL} from 'sentry/views/insights/mobile/ui/settings';
  12. import {BASE_URL as QUEUE_BASE_URL} from 'sentry/views/insights/queues/settings';
  13. import {INSIGHTS_BASE_URL} from 'sentry/views/insights/settings';
  14. import {ModuleName} from 'sentry/views/insights/types';
  15. export const MODULE_BASE_URLS: Record<ModuleName, string> = {
  16. [ModuleName.DB]: DB_BASE_URL,
  17. [ModuleName.HTTP]: HTTP_BASE_URL,
  18. [ModuleName.CACHE]: CACHE_BASE_URL,
  19. [ModuleName.QUEUE]: QUEUE_BASE_URL,
  20. [ModuleName.SCREEN_LOAD]: SCREEN_LOADS_BASE_URL,
  21. [ModuleName.APP_START]: APP_STARTS_BASE_URL,
  22. [ModuleName.VITAL]: VITALS_BASE_URL,
  23. [ModuleName.RESOURCE]: RESOURCES_BASE_URL,
  24. [ModuleName.AI]: AI_BASE_URL,
  25. [ModuleName.MOBILE_UI]: MOBILE_UI_BASE_URL,
  26. [ModuleName.OTHER]: '',
  27. [ModuleName.ALL]: '',
  28. };
  29. type ModuleNameStrings = `${ModuleName}`;
  30. type RoutableModuleNames = Exclude<ModuleNameStrings, '' | 'other'>;
  31. export const useModuleURL = (
  32. moduleName: RoutableModuleNames,
  33. bare: boolean = false
  34. ): string => {
  35. const builder = useModuleURLBuilder(bare);
  36. return builder(moduleName);
  37. };
  38. type URLBuilder = (moduleName: RoutableModuleNames) => string;
  39. export function useModuleURLBuilder(bare: boolean = false): URLBuilder {
  40. 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).
  41. if (!organization) {
  42. // 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
  43. return () => '';
  44. }
  45. const {slug} = organization;
  46. return function (moduleName: RoutableModuleNames) {
  47. return bare
  48. ? `${INSIGHTS_BASE_URL}/${MODULE_BASE_URLS[moduleName]}`
  49. : normalizeUrl(
  50. `/organizations/${slug}/${INSIGHTS_BASE_URL}/${MODULE_BASE_URLS[moduleName]}`
  51. );
  52. };
  53. }