import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { ActionIcon, AppShell, Group, ScrollArea, Tooltip, } from "@mantine/core"; import { useGetSharedPageTreeQuery } from "@/features/share/queries/share-query.ts"; import { useParams } from "react-router-dom"; import SharedTree from "@/features/share/components/shared-tree.tsx"; import { TableOfContents } from "@/features/editor/components/table-of-contents/table-of-contents.tsx"; import { readOnlyEditorAtom } from "@/features/editor/atoms/editor-atoms.ts"; import { ThemeToggle } from "@/components/theme-toggle.tsx"; import { useAtomValue, useSetAtom } from "jotai"; import { useAtom } from "jotai"; import { sharedPageFullWidthAtom, sharedPageTreeAtom, sharedTreeDataAtom, } from "@/features/share/atoms/shared-page-atom"; import { buildSharedPageTree } from "@/features/share/utils"; import { desktopSidebarAtom, mobileSidebarAtom, sidebarWidthAtom, } from "@/components/layouts/global/hooks/atoms/sidebar-atom.ts"; import SidebarToggle from "@/components/ui/sidebar-toggle-button.tsx"; import { useTranslation } from "react-i18next"; import { useToggleSidebar } from "@/components/layouts/global/hooks/hooks/use-toggle-sidebar.ts"; import { mobileTableOfContentAsideAtom, tableOfContentAsideAtom, } from "@/features/share/atoms/sidebar-atom.ts"; import { IconArrowsHorizontal, IconList } from "@tabler/icons-react"; import { useToggleToc } from "@/features/share/hooks/use-toggle-toc.ts"; import classes from "./share.module.css"; import { SearchControl, SearchMobileControl, } from "@/features/search/components/search-control.tsx"; import { ShareSearchSpotlight } from "@/features/search/components/share-search-spotlight.tsx"; import { shareSearchSpotlight } from "@/features/search/constants"; import ShareBranding from '@/features/share/components/share-branding.tsx'; import { MAIN_CONTENT_ID, SkipToMain } from "@/components/ui/skip-to-main.tsx"; const MemoizedSharedTree = React.memo(SharedTree); export default function ShareShell({ children, }: { children: React.ReactNode; }) { const { t } = useTranslation(); const [mobileOpened] = useAtom(mobileSidebarAtom); const [desktopOpened] = useAtom(desktopSidebarAtom); const toggleMobile = useToggleSidebar(mobileSidebarAtom); const toggleDesktop = useToggleSidebar(desktopSidebarAtom); const [tocOpened] = useAtom(tableOfContentAsideAtom); const [mobileTocOpened] = useAtom(mobileTableOfContentAsideAtom); const toggleTocMobile = useToggleToc(mobileTableOfContentAsideAtom); const toggleToc = useToggleToc(tableOfContentAsideAtom); const [fullWidth, setFullWidth] = useAtom(sharedPageFullWidthAtom); const [sidebarWidth, setSidebarWidth] = useAtom(sidebarWidthAtom); const [isResizing, setIsResizing] = useState(false); const sidebarRef = useRef(null); const startResizing = useCallback((e: React.MouseEvent) => { e.preventDefault(); setIsResizing(true); }, []); const stopResizing = useCallback(() => { setIsResizing(false); }, []); const resize = useCallback( (e: MouseEvent) => { if (!isResizing || !sidebarRef.current) return; const newWidth = e.clientX - sidebarRef.current.getBoundingClientRect().left; if (newWidth < 220) { setSidebarWidth(220); return; } if (newWidth > 600) { setSidebarWidth(600); return; } setSidebarWidth(newWidth); }, [isResizing, setSidebarWidth], ); useEffect(() => { window.addEventListener("mousemove", resize); window.addEventListener("mouseup", stopResizing); return () => { window.removeEventListener("mousemove", resize); window.removeEventListener("mouseup", stopResizing); }; }, [resize, stopResizing]); const { shareId } = useParams(); const { data } = useGetSharedPageTreeQuery(shareId); const readOnlyEditor = useAtomValue(readOnlyEditorAtom); // @ts-ignore const setSharedPageTree = useSetAtom(sharedPageTreeAtom); // @ts-ignore const setSharedTreeData = useSetAtom(sharedTreeDataAtom); // Build and set the tree data when it changes const treeData = useMemo(() => { if (!data?.pageTree) return null; return buildSharedPageTree(data.pageTree); }, [data?.pageTree]); useEffect(() => { setSharedPageTree(data || null); setSharedTreeData(treeData); }, [data, treeData, setSharedPageTree, setSharedTreeData]); return ( <> 1 && { navbar: { width: sidebarWidth, breakpoint: "sm", collapsed: { mobile: !mobileOpened, desktop: !desktopOpened, }, }, })} aside={{ width: 300, breakpoint: "sm", collapsed: { mobile: !mobileTocOpened, desktop: !tocOpened, }, }} padding="md" > {data?.pageTree?.length > 1 && ( <> )} {shareId && ( )} <> {shareId && ( )} setFullWidth((v) => !v)} visibleFrom="sm" size="sm" > {data?.pageTree?.length > 1 && (
)} {children} {data && shareId && !(data.features?.length > 0) && }
{readOnlyEditor && ( )}
); }