useModuleURL.tsx 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 {
  5. BASE_URL as RESOURCES_BASE_URL,
  6. PERFORMANCE_BASE_URL as PERFORMANCE_RESOURCES_BASE_URL,
  7. } from 'sentry/views/performance/browser/resources/settings';
  8. import {BASE_URL as VITALS_BASE_URL} from 'sentry/views/performance/browser/webVitals/settings';
  9. import {BASE_URL as CACHE_BASE_URL} from 'sentry/views/performance/cache/settings';
  10. import {BASE_URL as DB_BASE_URL} from 'sentry/views/performance/database/settings';
  11. import {BASE_URL as HTTP_BASE_URL} from 'sentry/views/performance/http/settings';
  12. import {BASE_URL as APP_STARTS_BASE_URL} from 'sentry/views/performance/mobile/appStarts/settings';
  13. import {BASE_URL as SCREEN_LOADS_BASE_URL} from 'sentry/views/performance/mobile/screenload/settings';
  14. import {BASE_URL as MOBILE_UI_BASE_URL} from 'sentry/views/performance/mobile/ui/settings';
  15. import {BASE_URL as QUEUE_BASE_URL} from 'sentry/views/performance/queues/settings';
  16. import {useInsightsURLBuilder} from 'sentry/views/performance/utils/useInsightsURL';
  17. import {ModuleName} from 'sentry/views/starfish/types';
  18. export const MODULE_BASE_URLS: Record<ModuleName, string> = {
  19. [ModuleName.DB]: DB_BASE_URL,
  20. [ModuleName.HTTP]: HTTP_BASE_URL,
  21. [ModuleName.CACHE]: CACHE_BASE_URL,
  22. [ModuleName.QUEUE]: QUEUE_BASE_URL,
  23. [ModuleName.SCREEN_LOAD]: SCREEN_LOADS_BASE_URL,
  24. [ModuleName.APP_START]: APP_STARTS_BASE_URL,
  25. [ModuleName.VITAL]: VITALS_BASE_URL,
  26. [ModuleName.RESOURCE]: RESOURCES_BASE_URL,
  27. [ModuleName.AI]: AI_BASE_URL,
  28. [ModuleName.MOBILE_UI]: MOBILE_UI_BASE_URL,
  29. [ModuleName.OTHER]: '',
  30. [ModuleName.ALL]: '',
  31. };
  32. type ModuleNameStrings = `${ModuleName}`;
  33. type RoutableModuleNames = Exclude<ModuleNameStrings, '' | 'other'>;
  34. export const useModuleURL = (
  35. moduleName: RoutableModuleNames,
  36. bare: boolean = false
  37. ): string => {
  38. const builder = useModuleURLBuilder(bare);
  39. return builder(moduleName);
  40. };
  41. type URLBuilder = (moduleName: RoutableModuleNames) => string;
  42. export function useModuleURLBuilder(bare: boolean = false): URLBuilder {
  43. 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).
  44. const isInsightsEnabled = organization?.features?.includes('performance-insights');
  45. const insightsURLBuilder = useInsightsURLBuilder();
  46. if (!organization) {
  47. // 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
  48. return () => '';
  49. }
  50. const {slug} = organization;
  51. return function (moduleName: RoutableModuleNames) {
  52. const insightsURL = insightsURLBuilder(moduleName);
  53. if (moduleName === ModuleName.AI) {
  54. // AI Doesn't live under "/performance", which means `insightsURL` might be an empty string, so we need to account for that
  55. const moduleURLSegment = [insightsURL, AI_BASE_URL].filter(Boolean).join('/');
  56. return bare
  57. ? moduleURLSegment
  58. : normalizeUrl(`/organizations/${slug}/${moduleURLSegment}`);
  59. }
  60. if (!isInsightsEnabled && moduleName === ModuleName.RESOURCE) {
  61. return bare
  62. ? `${insightsURL}/${PERFORMANCE_RESOURCES_BASE_URL}`
  63. : normalizeUrl(
  64. `/organizations/${slug}/${insightsURL}/${PERFORMANCE_RESOURCES_BASE_URL}`
  65. );
  66. }
  67. return bare
  68. ? `${insightsURL}/${MODULE_BASE_URLS[moduleName]}`
  69. : normalizeUrl(
  70. `/organizations/${slug}/${insightsURL}/${MODULE_BASE_URLS[moduleName]}`
  71. );
  72. };
  73. }