import Logo from './Logo' import GroupCategory from './GroupCategory' import { MenuList } from './MenuList' import GroupTag from './GroupTag' import SearchInput from './SearchInput' import SiteInfo from './SiteInfo' import Catalog from './Catalog' import Announcement from './Announcement' import { useRouter } from 'next/router' import DarkModeButton from '@/components/DarkModeButton' import SocialButton from './SocialButton' import CONFIG from '@/themes/fukasawa/config' import { AdSlot } from '@/components/GoogleAdsense' import { siteConfig } from '@/lib/config' import MailChimpForm from './MailChimpForm' import { useGlobal } from '@/lib/global' import { useEffect, useState } from 'react' import { isBrowser } from '@/lib/utils' import { debounce } from 'lodash' /** * 侧边栏 * @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) // 侧边栏折叠从 本地存储中获取 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 toggleOpen = () => { setIsCollapse(!isCollapsed) } // 自动折叠侧边栏 onResize 窗口宽度小于1366 || 滚动条滚动至页面的300px时 ; 将open设置为false useEffect(() => { if (!siteConfig('FUKASAWA_SIDEBAR_COLLAPSE_ON_SCROLL', false, CONFIG)) { 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