import DarkModeButton from '@/components/DarkModeButton' import { AdSlot } from '@/components/GoogleAdsense' import { siteConfig } from '@/lib/config' import { useGlobal } from '@/lib/global' import { isBrowser } from '@/lib/utils' import CONFIG from '@/themes/fukasawa/config' import { debounce } from 'lodash' import { useRouter } from 'next/router' import { useEffect, useMemo, useState } from 'react' import Announcement from './Announcement' import Catalog from './Catalog' import GroupCategory from './GroupCategory' import GroupTag from './GroupTag' import Logo from './Logo' import MailChimpForm from './MailChimpForm' import { MenuList } from './MenuList' import SearchInput from './SearchInput' import SiteInfo from './SiteInfo' import SocialButton from './SocialButton' /** * 侧边栏 * @param {*} props * @returns */ function AsideLeft(props) { const { tagOptions, currentTag, categoryOptions, currentCategory, post, slot, notice } = props const router = useRouter() const { fullWidth } = useGlobal() const FUKASAWA_SIDEBAR_COLLAPSE_SATUS_DEFAULT = fullWidth || siteConfig('FUKASAWA_SIDEBAR_COLLAPSE_SATUS_DEFAULT', null, CONFIG) const FUKASAWA_SIDEBAR_COLLAPSE_ON_SCROLL = siteConfig( 'FUKASAWA_SIDEBAR_COLLAPSE_ON_SCROLL', false, CONFIG ) const FUKASAWA_SIDEBAR_COLLAPSE_BUTTON = siteConfig( 'FUKASAWA_SIDEBAR_COLLAPSE_BUTTON', null, CONFIG ) // 侧边栏折叠从 本地存储中获取 open 状态的初始值 const [isCollapsed, setIsCollapse] = useState(() => { if (typeof window !== 'undefined') { return ( localStorage.getItem('fukasawa-sidebar-collapse') === 'true' || FUKASAWA_SIDEBAR_COLLAPSE_SATUS_DEFAULT ) } return FUKASAWA_SIDEBAR_COLLAPSE_SATUS_DEFAULT }) // 在组件卸载时保存 open 状态到本地存储中 useEffect(() => { if (isBrowser) { localStorage.setItem('fukasawa-sidebar-collapse', isCollapsed) } }, [isCollapsed]) const isReverse = siteConfig('LAYOUT_SIDEBAR_REVERSE') const position = useMemo(() => { if (isCollapsed) { if (isReverse) { return 'right-2' } else { return 'left-2' } } else { if (isReverse) { return 'right-80' } else { return 'left-80' } } }, [isCollapsed]) // 折叠侧边栏 const toggleOpen = () => { setIsCollapse(!isCollapsed) } // 自动折叠侧边栏 onResize 窗口宽度小于1366 || 滚动条滚动至页面的300px时 ; 将open设置为false useEffect(() => { if (!FUKASAWA_SIDEBAR_COLLAPSE_ON_SCROLL) { return } const handleResize = debounce(() => { if (window.innerWidth < 1366 || window.scrollY >= 1366) { setIsCollapse(true) } else { setIsCollapse(false) } }, 100) if (post) { window.addEventListener('resize', handleResize) window.addEventListener('scroll', handleResize, { passive: true }) } return () => { if (post) { window.removeEventListener('resize', handleResize) window.removeEventListener('scroll', handleResize, { passive: true }) } } }, []) return ( ) } export default AsideLeft