useModuleURL.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 SCREEN_RENDERING_BASE_URL} from 'sentry/views/insights/mobile/screenRendering/settings';
  12. import {BASE_URL as MOBILE_SCREENS_BASE_URL} from 'sentry/views/insights/mobile/screens/settings';
  13. import {BASE_URL as MOBILE_UI_BASE_URL} from 'sentry/views/insights/mobile/ui/settings';
  14. import {DOMAIN_VIEW_BASE_URL} from 'sentry/views/insights/pages/settings';
  15. import {
  16. type DomainView,
  17. useDomainViewFilters,
  18. } from 'sentry/views/insights/pages/useFilters';
  19. import {getModuleView} from 'sentry/views/insights/pages/utils';
  20. import {BASE_URL as QUEUE_BASE_URL} from 'sentry/views/insights/queues/settings';
  21. import {ModuleName} from 'sentry/views/insights/types';
  22. export const MODULE_BASE_URLS: Record<ModuleName, string> = {
  23. [ModuleName.DB]: DB_BASE_URL,
  24. [ModuleName.HTTP]: HTTP_BASE_URL,
  25. [ModuleName.CACHE]: CACHE_BASE_URL,
  26. [ModuleName.QUEUE]: QUEUE_BASE_URL,
  27. [ModuleName.SCREEN_LOAD]: SCREEN_LOADS_BASE_URL,
  28. [ModuleName.APP_START]: APP_STARTS_BASE_URL,
  29. [ModuleName.VITAL]: VITALS_BASE_URL,
  30. [ModuleName.RESOURCE]: RESOURCES_BASE_URL,
  31. [ModuleName.AI]: AI_BASE_URL,
  32. [ModuleName.MOBILE_UI]: MOBILE_UI_BASE_URL,
  33. [ModuleName.MOBILE_SCREENS]: MOBILE_SCREENS_BASE_URL,
  34. [ModuleName.SCREEN_RENDERING]: SCREEN_RENDERING_BASE_URL,
  35. [ModuleName.OTHER]: '',
  36. };
  37. type ModuleNameStrings = `${ModuleName}`;
  38. export type RoutableModuleNames = Exclude<ModuleNameStrings, '' | 'other'>;
  39. export const useModuleURL = (
  40. moduleName: RoutableModuleNames,
  41. bare: boolean = false,
  42. view?: DomainView // Todo - this should be required when a module belongs to multiple views
  43. ): string => {
  44. const builder = useModuleURLBuilder(bare);
  45. return builder(moduleName, view);
  46. };
  47. export type URLBuilder = (
  48. moduleName: RoutableModuleNames,
  49. domainView?: DomainView
  50. ) => string;
  51. /**
  52. * This hook returns a function to build URLs for the module summary pages.
  53. * This function will return the domain specific module url, the domain is determined in the following order of priority:
  54. * 1. The domain view passed in by the user
  55. * 2. (when detectDomainView=true) The current domain view (i.e if the current url is `/performance/frontend`, the current view is frontned)
  56. * 3. The default view for the module
  57. */
  58. export function useModuleURLBuilder(
  59. bare: boolean = false,
  60. detectDomainView: boolean = true
  61. ): URLBuilder {
  62. 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).
  63. const {view: currentView} = useDomainViewFilters();
  64. if (!organization) {
  65. // 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
  66. return () => '';
  67. }
  68. const {slug} = organization;
  69. return function (moduleName: RoutableModuleNames, domainView?: DomainView) {
  70. let view = detectDomainView ? currentView : currentView ?? domainView;
  71. if (!view) {
  72. view = getModuleView(moduleName as ModuleName);
  73. }
  74. return bare
  75. ? `${DOMAIN_VIEW_BASE_URL}/${view}/${MODULE_BASE_URLS[moduleName]}`
  76. : normalizeUrl(
  77. `/organizations/${slug}/${DOMAIN_VIEW_BASE_URL}/${view}/${MODULE_BASE_URLS[moduleName]}`
  78. );
  79. };
  80. }