mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
Redirect to single allowed section on share page.
This commit is contained in:
parent
af7f7adf5b
commit
c9f6653b62
3 changed files with 52 additions and 8 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
'use client';
|
'use client';
|
||||||
import { Column, Grid, useTheme } from '@umami/react-zen';
|
import { Column, Grid, useTheme } from '@umami/react-zen';
|
||||||
import { useEffect } from 'react';
|
import { useRouter } from 'next/navigation';
|
||||||
|
import { useEffect, useMemo } from 'react';
|
||||||
import { AttributionPage } from '@/app/(main)/websites/[websiteId]/(reports)/attribution/AttributionPage';
|
import { AttributionPage } from '@/app/(main)/websites/[websiteId]/(reports)/attribution/AttributionPage';
|
||||||
import { BreakdownPage } from '@/app/(main)/websites/[websiteId]/(reports)/breakdown/BreakdownPage';
|
import { BreakdownPage } from '@/app/(main)/websites/[websiteId]/(reports)/breakdown/BreakdownPage';
|
||||||
import { FunnelsPage } from '@/app/(main)/websites/[websiteId]/(reports)/funnels/FunnelsPage';
|
import { FunnelsPage } from '@/app/(main)/websites/[websiteId]/(reports)/funnels/FunnelsPage';
|
||||||
|
|
@ -18,8 +19,8 @@ import { WebsitePage } from '@/app/(main)/websites/[websiteId]/WebsitePage';
|
||||||
import { WebsiteProvider } from '@/app/(main)/websites/WebsiteProvider';
|
import { WebsiteProvider } from '@/app/(main)/websites/WebsiteProvider';
|
||||||
import { PageBody } from '@/components/common/PageBody';
|
import { PageBody } from '@/components/common/PageBody';
|
||||||
import { useShareTokenQuery } from '@/components/hooks';
|
import { useShareTokenQuery } from '@/components/hooks';
|
||||||
import { Footer } from './Footer';
|
import { ShareFooter } from './ShareFooter';
|
||||||
import { Header } from './Header';
|
import { ShareHeader } from './ShareHeader';
|
||||||
import { ShareNav } from './ShareNav';
|
import { ShareNav } from './ShareNav';
|
||||||
|
|
||||||
const PAGE_COMPONENTS: Record<string, React.ComponentType<{ websiteId: string }>> = {
|
const PAGE_COMPONENTS: Record<string, React.ComponentType<{ websiteId: string }>> = {
|
||||||
|
|
@ -39,9 +40,34 @@ const PAGE_COMPONENTS: Record<string, React.ComponentType<{ websiteId: string }>
|
||||||
attribution: AttributionPage,
|
attribution: AttributionPage,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// All section IDs that can be enabled/disabled via parameters
|
||||||
|
const ALL_SECTION_IDS = [
|
||||||
|
'overview',
|
||||||
|
'events',
|
||||||
|
'sessions',
|
||||||
|
'realtime',
|
||||||
|
'compare',
|
||||||
|
'breakdown',
|
||||||
|
'goals',
|
||||||
|
'funnels',
|
||||||
|
'journeys',
|
||||||
|
'retention',
|
||||||
|
'utm',
|
||||||
|
'revenue',
|
||||||
|
'attribution',
|
||||||
|
];
|
||||||
|
|
||||||
export function SharePage({ shareId, path = '' }: { shareId: string; path?: string }) {
|
export function SharePage({ shareId, path = '' }: { shareId: string; path?: string }) {
|
||||||
const { shareToken, isLoading } = useShareTokenQuery(shareId);
|
const { shareToken, isLoading } = useShareTokenQuery(shareId);
|
||||||
const { setTheme } = useTheme();
|
const { setTheme } = useTheme();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
// Calculate allowed sections
|
||||||
|
const allowedSections = useMemo(() => {
|
||||||
|
if (!shareToken?.parameters) return [];
|
||||||
|
const params = shareToken.parameters;
|
||||||
|
return ALL_SECTION_IDS.filter(id => params[id] !== false);
|
||||||
|
}, [shareToken?.parameters]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const url = new URL(window?.location?.href);
|
const url = new URL(window?.location?.href);
|
||||||
|
|
@ -52,12 +78,32 @@ export function SharePage({ shareId, path = '' }: { shareId: string; path?: stri
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// Redirect to the only allowed section if there's just one and we're on the base path
|
||||||
|
useEffect(() => {
|
||||||
|
if (
|
||||||
|
allowedSections.length === 1 &&
|
||||||
|
allowedSections[0] !== 'overview' &&
|
||||||
|
(path === '' || path === 'overview')
|
||||||
|
) {
|
||||||
|
router.replace(`/share/${shareId}/${allowedSections[0]}`);
|
||||||
|
}
|
||||||
|
}, [allowedSections, shareId, path, router]);
|
||||||
|
|
||||||
if (isLoading || !shareToken) {
|
if (isLoading || !shareToken) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { websiteId, parameters = {}, whiteLabel } = shareToken;
|
const { websiteId, parameters = {}, whiteLabel } = shareToken;
|
||||||
|
|
||||||
|
// Redirect to only allowed section - return null while redirecting
|
||||||
|
if (
|
||||||
|
allowedSections.length === 1 &&
|
||||||
|
allowedSections[0] !== 'overview' &&
|
||||||
|
(path === '' || path === 'overview')
|
||||||
|
) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if the requested path is allowed
|
// Check if the requested path is allowed
|
||||||
const pageKey = path || '';
|
const pageKey = path || '';
|
||||||
const isAllowed = pageKey === '' || pageKey === 'overview' || parameters[pageKey] !== false;
|
const isAllowed = pageKey === '' || pageKey === 'overview' || parameters[pageKey] !== false;
|
||||||
|
|
@ -69,8 +115,7 @@ export function SharePage({ shareId, path = '' }: { shareId: string; path?: stri
|
||||||
const PageComponent = PAGE_COMPONENTS[pageKey] || WebsitePage;
|
const PageComponent = PAGE_COMPONENTS[pageKey] || WebsitePage;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Column backgroundColor="2">
|
<Column backgroundColor="2" minHeight="100%">
|
||||||
<Header />
|
|
||||||
<Grid columns={{ xs: '1fr', lg: 'auto 1fr' }} width="100%" height="100%">
|
<Grid columns={{ xs: '1fr', lg: 'auto 1fr' }} width="100%" height="100%">
|
||||||
<Column
|
<Column
|
||||||
display={{ xs: 'none', lg: 'flex' }}
|
display={{ xs: 'none', lg: 'flex' }}
|
||||||
|
|
@ -84,15 +129,14 @@ export function SharePage({ shareId, path = '' }: { shareId: string; path?: stri
|
||||||
</Column>
|
</Column>
|
||||||
<PageBody gap>
|
<PageBody gap>
|
||||||
<WebsiteProvider websiteId={websiteId}>
|
<WebsiteProvider websiteId={websiteId}>
|
||||||
<Header whiteLabel={whiteLabel} />
|
<ShareHeader whiteLabel={whiteLabel} />
|
||||||
<Column>
|
<Column>
|
||||||
<PageComponent websiteId={websiteId} />
|
<PageComponent websiteId={websiteId} />
|
||||||
</Column>
|
</Column>
|
||||||
<Footer whiteLabel={whiteLabel} />
|
<ShareFooter whiteLabel={whiteLabel} />
|
||||||
</WebsiteProvider>
|
</WebsiteProvider>
|
||||||
</PageBody>
|
</PageBody>
|
||||||
</Grid>
|
</Grid>
|
||||||
<Footer />
|
|
||||||
</Column>
|
</Column>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue