Files
umami/src/app/share/ShareProvider.tsx
T
Mike CaoandClaude Opus 4.5 d028bfa1f5 Move share page redirect logic to ShareProvider
Centralizes the single-section redirect logic in ShareProvider instead of
SharePage, reducing useEffect complexity and preventing children from
rendering during redirect.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-28 23:18:05 -08:00

65 lines
1.6 KiB
TypeScript

'use client';
import { Loading } from '@umami/react-zen';
import { usePathname, useRouter } from 'next/navigation';
import { createContext, type ReactNode, useEffect } from 'react';
import { useShareTokenQuery } from '@/components/hooks';
import type { WhiteLabel } from '@/lib/types';
export interface ShareData {
shareId: string;
websiteId: string;
parameters: any;
token: string;
whiteLabel?: WhiteLabel;
}
export const ShareContext = createContext<ShareData>(null);
const ALL_SECTION_IDS = [
'overview',
'events',
'sessions',
'realtime',
'compare',
'breakdown',
'goals',
'funnels',
'journeys',
'retention',
'utm',
'revenue',
'attribution',
];
export function ShareProvider({ shareId, children }: { shareId: string; children: ReactNode }) {
const { share, isLoading, isFetching } = useShareTokenQuery(shareId);
const router = useRouter();
const pathname = usePathname();
const path = pathname.split('/')[3];
const allowedSections = share?.parameters
? ALL_SECTION_IDS.filter(id => share.parameters[id] !== false)
: [];
const shouldRedirect =
allowedSections.length === 1 &&
allowedSections[0] !== 'overview' &&
(path === undefined || path === '' || path === 'overview');
useEffect(() => {
if (shouldRedirect) {
router.replace(`/share/${shareId}/${allowedSections[0]}`);
}
}, [shouldRedirect, shareId, allowedSections, router]);
if (isFetching && isLoading) {
return <Loading placement="absolute" />;
}
if (!share || shouldRedirect) {
return null;
}
return <ShareContext.Provider value={share}>{children}</ShareContext.Provider>;
}