useModuleURL.tsx 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import useOrganization from 'sentry/utils/useOrganization';
  2. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  3. import {BASE_URL as AI_BASE_URL} from 'sentry/views/llmMonitoring/settings';
  4. import {BASE_URL as RESOURCES_BASE_URL} from 'sentry/views/performance/browser/resources/settings';
  5. import {BASE_URL as VITALS_BASE_URL} from 'sentry/views/performance/browser/webVitals/settings';
  6. import {BASE_URL as CACHE_BASE_URL} from 'sentry/views/performance/cache/settings';
  7. import {BASE_URL as DB_BASE_URL} from 'sentry/views/performance/database/settings';
  8. import {BASE_URL as HTTP_BASE_URL} from 'sentry/views/performance/http/settings';
  9. import {BASE_URL as APP_STARTS_BASE_URL} from 'sentry/views/performance/mobile/appStarts/settings';
  10. import {BASE_URL as SCREEN_LOADS_BASE_URL} from 'sentry/views/performance/mobile/screenload/settings';
  11. import {BASE_URL as MOBILE_UI_BASE_URL} from 'sentry/views/performance/mobile/ui/settings';
  12. import {BASE_URL as QUEUE_BASE_URL} from 'sentry/views/performance/queues/settings';
  13. import {useInsightsURLBuilder} from 'sentry/views/performance/utils/useInsightsURL';
  14. import {ModuleName} from 'sentry/views/starfish/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. const insightsURLBuilder = useInsightsURLBuilder();
  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. const insightsURL = insightsURLBuilder(moduleName);
  49. if (moduleName === ModuleName.AI) {
  50. // AI Doesn't live under "/performance", which means `insightsURL` might be an empty string, so we need to account for that
  51. const moduleURLSegment = [insightsURL, AI_BASE_URL].filter(Boolean).join('/');
  52. return bare
  53. ? moduleURLSegment
  54. : normalizeUrl(`/organizations/${slug}/${moduleURLSegment}`);
  55. }
  56. return bare
  57. ? `${insightsURL}/${MODULE_BASE_URLS[moduleName]}`
  58. : normalizeUrl(
  59. `/organizations/${slug}/${insightsURL}/${MODULE_BASE_URLS[moduleName]}`
  60. );
  61. };
  62. }