modulePageProviders.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {type ComponentProps, useEffect} from 'react';
  2. import * as qs from 'query-string';
  3. import Feature from 'sentry/components/acl/feature';
  4. import * as Layout from 'sentry/components/layouts/thirds';
  5. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  6. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import {useNavigate} from 'sentry/utils/useNavigate';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import {NoAccess} from 'sentry/views/performance/database/noAccess';
  11. import {useInsightsTitle} from 'sentry/views/performance/utils/useInsightsTitle';
  12. import {useModuleTitle} from 'sentry/views/performance/utils/useModuleTitle';
  13. import type {ModuleName} from 'sentry/views/starfish/types';
  14. type ModuleNameStrings = `${ModuleName}`;
  15. type TitleableModuleNames = Exclude<ModuleNameStrings, '' | 'other'>;
  16. interface Props {
  17. children: React.ReactNode;
  18. features: ComponentProps<typeof Feature>['features'];
  19. moduleName: TitleableModuleNames;
  20. pageTitle?: string;
  21. }
  22. export function ModulePageProviders({moduleName, pageTitle, children, features}: Props) {
  23. const organization = useOrganization();
  24. const location = useLocation();
  25. const navigate = useNavigate();
  26. const insightsTitle = useInsightsTitle(moduleName);
  27. const moduleTitle = useModuleTitle(moduleName);
  28. const fullPageTitle = [pageTitle, moduleTitle, insightsTitle]
  29. .filter(Boolean)
  30. .join(' — ');
  31. const areInsightsEnabled = organization?.features?.includes('performance-insights');
  32. const isOnInsightsRoute = location.pathname.includes(`/insights/`);
  33. useEffect(() => {
  34. // If the Insights feature is enabled, redirect users to the `/insights/` equivalent URL!
  35. if (areInsightsEnabled && !isOnInsightsRoute) {
  36. const newPathname = location.pathname.replace(/\/performance\//g, '/insights/');
  37. navigate(`${newPathname}?${qs.stringify(location.query)}`);
  38. }
  39. }, [
  40. navigate,
  41. location.pathname,
  42. location.query,
  43. areInsightsEnabled,
  44. isOnInsightsRoute,
  45. ]);
  46. return (
  47. <PageFiltersContainer>
  48. <SentryDocumentTitle title={fullPageTitle} orgSlug={organization.slug}>
  49. <Layout.Page>
  50. <Feature
  51. features={features}
  52. organization={organization}
  53. renderDisabled={NoAccess}
  54. >
  55. {children}
  56. </Feature>
  57. </Layout.Page>
  58. </SentryDocumentTitle>
  59. </PageFiltersContainer>
  60. );
  61. }