From 9c13e34fb10a77e947aaf4e715dfc58f2e3711ad Mon Sep 17 00:00:00 2001 From: HoomanHq Date: Sun, 10 Nov 2024 21:59:34 +0700 Subject: [PATCH] feat: only same tree next prev (#1239) Co-authored-by: dharamveergit --- src/lib/nextPrevPages.ts | 85 ++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/src/lib/nextPrevPages.ts b/src/lib/nextPrevPages.ts index 836382ff..0d6b8ab2 100644 --- a/src/lib/nextPrevPages.ts +++ b/src/lib/nextPrevPages.ts @@ -2,31 +2,40 @@ import { getNavigation } from "@/layouts/navigation"; export function findPrevAndNextPages(pathname: string) { const initialUrl = pathname?.match(/\/([^\/]*)\//)?.[1]; - const nav = getNavigation(initialUrl); - let allItems: any = []; - - function flattenNav(nav) { - nav.forEach((item) => { - allItems.push(item); - if (item.children && item.children.length > 0) { - flattenNav(item.children); - } - }); - } + const nav = getNavigation(initialUrl)?.map((item) => { + return item?.children?.length + ? { ...item, children: item?.children?.filter((child) => !child.href) } + : item; + }); - flattenNav(nav); + const currentItem = findItemByHref(nav, pathname); + if (!currentItem) return { prevPage: null, nextPage: null }; - const currentIndex = allItems.findIndex((item) => item.href === pathname); + const parentItem = findParentItem(nav, currentItem); + if (!parentItem) return { prevPage: null, nextPage: null }; - const prevIndex = currentIndex - 1; - const nextIndex = currentIndex + 1; + const currentIndex = parentItem.children.findIndex( + (item) => item.href === pathname, + ); - let prevPage = prevIndex >= 0 ? allItems[prevIndex] : null; + let prevPage = + currentIndex > 0 ? parentItem.children[currentIndex - 1] : null; let nextPage = - nextIndex < allItems.length ? findFirstSubitem(allItems[nextIndex]) : null; + currentIndex < parentItem.children.length - 1 + ? parentItem.children[currentIndex + 1] + : null; + + if (prevPage === null && hasSubItems(currentItem)) { + prevPage = currentItem.children[0]; + } - if (currentIndex > 0 && hasSubItems(allItems[currentIndex - 1])) { - prevPage = findLevelBackItem(allItems, currentIndex - 2); + if (nextPage === null && hasSubItems(currentItem)) { + nextPage = currentItem.children[0]; + } + + if (!hasSubItems(currentItem)) { + prevPage = findPrevSibling(parentItem, currentItem); + nextPage = findNextSibling(parentItem, currentItem); } return { prevPage, nextPage }; @@ -36,20 +45,38 @@ function hasSubItems(item) { return item.children && item.children.length > 0; } -function findFirstSubitem(item) { - if (hasSubItems(item) && item?.children && item?.children?.length > 0) { - return findFirstSubitem(item?.children[0]); +function findItemByHref(nav, href) { + for (let item of nav) { + if (item.href === href) { + return item; + } else if (item.children && item.children.length > 0) { + const childItem = findItemByHref(item.children, href); + if (childItem) return childItem; + } } - return item; + return null; } -function findLevelBackItem(allItems, index: number) { - if (index >= 0) { - const currentItem = allItems[index]; - if (hasSubItems(currentItem)) { - return findLevelBackItem(allItems, index - 1); +function findParentItem(nav, currentItem) { + for (let item of nav) { + if (item.children && item.children.includes(currentItem)) { + return item; + } else if (item.children && item.children.length > 0) { + const parentItem = findParentItem(item.children, currentItem); + if (parentItem) return parentItem; } - return currentItem; } return null; } + +function findPrevSibling(parentItem, currentItem) { + const index = parentItem.children.findIndex((item) => item === currentItem); + return index > 0 ? parentItem.children[index - 1] : null; +} + +function findNextSibling(parentItem, currentItem) { + const index = parentItem.children.findIndex((item) => item === currentItem); + return index < parentItem.children.length - 1 + ? parentItem.children[index + 1] + : null; +}