Skip to content

Commit

Permalink
feat: only same tree next prev (#1239)
Browse files Browse the repository at this point in the history
Co-authored-by: dharamveergit <[email protected]>
  • Loading branch information
HoomanDgtl and dharamveergit authored Nov 10, 2024
1 parent 6314e2a commit 9c13e34
Showing 1 changed file with 56 additions and 29 deletions.
85 changes: 56 additions & 29 deletions src/lib/nextPrevPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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;
}

0 comments on commit 9c13e34

Please sign in to comment.