import { BreadcrumbItem, Breadcrumbs } from '@heroui/breadcrumbs' import { Button } from '@heroui/button' import { useLocalStorage } from '@uidotdev/usehooks' import clsx from 'clsx' import { AnimatePresence, motion } from 'motion/react' import { useEffect, useMemo, useRef } from 'react' import { ErrorBoundary } from 'react-error-boundary' import { MdMenu, MdMenuOpen } from 'react-icons/md' import { useLocation, useNavigate } from 'react-router-dom' import key from '@/const/key' import errorFallbackRender from '@/components/error_fallback' // import PageLoading from "@/components/Loading/PageLoading"; import SideBar from '@/components/sidebar' import useAuth from '@/hooks/auth' import type { MenuItem } from '@/config/site' import { siteConfig } from '@/config/site' import QQManager from '@/controllers/qq_manager' const menus: MenuItem[] = siteConfig.navItems const findTitle = (menus: MenuItem[], pathname: string): string[] => { const paths: string[] = [] if (pathname) { for (const item of menus) { if (item.href === pathname) { paths.push(item.label) } else if (item.items) { const title = findTitle(item.items, pathname) if (title.length > 0) { paths.push(item.label) paths.push(...title) } } } } return paths } const Layout: React.FC<{ children: React.ReactNode }> = ({ children }) => { const location = useLocation() const contentRef = useRef(null) const [openSideBar, setOpenSideBar] = useLocalStorage(key.sideBarOpen, true) const [b64img] = useLocalStorage(key.backgroundImage, '') const navigate = useNavigate() const { isAuth } = useAuth() const checkIsQQLogin = async () => { try { const result = await QQManager.checkQQLoginStatus() if (!result.isLogin) { if (isAuth) { navigate('/qq_login', { replace: true }) } else { navigate('/web_login', { replace: true }) } } } catch (error) { navigate('/web_login', { replace: true }) } } useEffect(() => { contentRef?.current?.scrollTo?.({ top: 0, behavior: 'smooth' }) }, [location.pathname]) useEffect(() => { if (isAuth) { checkIsQQLogin() } }, [isAuth]) const title = useMemo(() => { return findTitle(menus, location.pathname) }, [location.pathname]) return (
{title?.map((item, index) => ( {item} ))}
{children}
) } export default Layout