diff --git a/apps/client/src/features/share/utils.ts b/apps/client/src/features/share/utils.ts index cc73eb57..d5201693 100644 --- a/apps/client/src/features/share/utils.ts +++ b/apps/client/src/features/share/utils.ts @@ -66,3 +66,20 @@ export function buildSharedPageTree( return sortTree(tree); } + + +// Recursively checks if a page exists in the shared page tree. +export function isPageInTree( + tree: SharedPageTreeNode[], + pageSlugId: string, +): boolean { + for (const node of tree) { + if (node.slugId === pageSlugId) { + return true; + } + if (isPageInTree(node.children, pageSlugId)) { + return true; + } + } + return false; +} diff --git a/apps/client/src/pages/share/shared-page.tsx b/apps/client/src/pages/share/shared-page.tsx index 274b76cb..ff7302c5 100644 --- a/apps/client/src/pages/share/shared-page.tsx +++ b/apps/client/src/pages/share/shared-page.tsx @@ -8,6 +8,9 @@ import ReadonlyPageEditor from "@/features/editor/readonly-page-editor.tsx"; import { extractPageSlugId } from "@/lib"; import { Error404 } from "@/components/ui/error-404.tsx"; import ShareBranding from "@/features/share/components/share-branding.tsx"; +import { useAtomValue } from "jotai"; +import { sharedTreeDataAtom } from "@/features/share/atoms/shared-page-atom.ts"; +import { isPageInTree } from "@/features/share/utils.ts"; export default function SharedPage() { const { t } = useTranslation(); @@ -19,13 +22,22 @@ export default function SharedPage() { pageId: extractPageSlugId(pageSlug), }); + const sharedTreeData = useAtomValue(sharedTreeDataAtom); + useEffect(() => { if (shareId && data) { if (data.share.key !== shareId) { - navigate(`/share/${data.share.key}/p/${pageSlug}`, { replace: true }); + + // Check if the current page is part of the active sharing tree (sidebar) - If we are part of it, we will not redirect, keeping the sidebar visible. + const isPartOfTree = + sharedTreeData && isPageInTree(sharedTreeData, data.page.slugId); + + if (!isPartOfTree) { + navigate(`/share/${data.share.key}/p/${pageSlug}`, { replace: true }); + } } } - }, [shareId, data]); + }, [shareId, data, sharedTreeData]); if (isLoading) { return <>;