import { siteConfig } from '@/lib/config' import { useGlobal } from '@/lib/global' import { useRouter } from 'next/router' import { useEffect, useState } from 'react' import CONFIG from '../config' import BlogPostCard from './BlogPostCard' import NavPostItem from './NavPostItem' /** * 部落格列表滾動分頁 * @param posts 所有文章 * @param tags 所有標籤 * @returns {JSX.Element} * @constructor */ const NavPostList = props => { const { filteredNavPages } = props const { locale, currentSearch } = useGlobal() const router = useRouter() // 依分類將文章歸成資料夾 const categoryFolders = groupArticles(filteredNavPages) // 存放被展開的群組 const [expandedGroups, setExpandedGroups] = useState([]) // 是否採用排他折疊,一次只展開一個資料夾 const GITBOOK_EXCLUSIVE_COLLAPSE = siteConfig( 'GITBOOK_EXCLUSIVE_COLLAPSE', null, CONFIG ) useEffect(() => { // 展開資料夾 setTimeout(() => { const currentPath = decodeURIComponent(router.asPath.split('?')[0]) const defaultOpenIndex = getDefaultOpenIndexByPath( categoryFolders, currentPath ) setExpandedGroups([defaultOpenIndex]) }, 500) // eslint-disable-next-line react-hooks/exhaustive-deps }, [router, filteredNavPages]) // 切換折疊項目,當陣列狀態改變時觸發 const toggleItem = index => { let newExpandedGroups = [...expandedGroups] // 建立新的展開群組陣列 // 若 expandedGroups 中不存在則加入,存在則移除 if (expandedGroups.includes(index)) { // 若 expandedGroups 中包含 index,則移除 newExpandedGroups = newExpandedGroups.filter( expandedIndex => expandedIndex !== index ) } else { // 若 expandedGroups 中不含 index,則加入 newExpandedGroups.push(index) } // 是否排他 if (GITBOOK_EXCLUSIVE_COLLAPSE) { // 若折疊選單為排他模式,僅保留目前群組其餘關閉 newExpandedGroups = newExpandedGroups.filter( expandedIndex => expandedIndex === index ) } // 更新展開群組陣列 setExpandedGroups(newExpandedGroups) } // 無資料時 if (!categoryFolders || categoryFolders.length === 0) { // 空白內容 return (
{locale.COMMON.NO_RESULTS_FOUND}{' '} {currentSearch &&