Skip to content

Commit d4f76d0

Browse files
Fix PrevNextLinks logic
1 parent ef690f4 commit d4f76d0

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/components/PrevNextLinks.tsx

+22-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Link from 'next/link'
44
import { usePathname } from 'next/navigation'
55
import clsx from 'clsx'
66

7-
import { navigation } from '@/lib/navigation'
7+
import { NavigationItem, navigation } from '@/lib/navigation'
88

99
function ArrowIcon(props: React.ComponentPropsWithoutRef<'svg'>) {
1010
return (
@@ -50,9 +50,29 @@ function PageLink({
5050
)
5151
}
5252

53+
function flattenNavigation(navigation: NavigationItem[]): NavigationItem[] {
54+
const seen = new Set<string>()
55+
const flatList: NavigationItem[] = []
56+
57+
function processItem(item: NavigationItem) {
58+
if (!seen.has(item.href)) {
59+
seen.add(item.href)
60+
flatList.push({ ...item, links: undefined })
61+
}
62+
63+
if (item.links) {
64+
item.links.forEach(processItem)
65+
}
66+
}
67+
navigation.forEach(processItem)
68+
69+
return flatList
70+
}
71+
5372
export function PrevNextLinks() {
5473
let pathname = usePathname()
55-
let allLinks = navigation.flatMap((section) => section.links ?? [])
74+
let allLinks = flattenNavigation(navigation)
75+
5676
let linkIndex = allLinks.findIndex((link) => link.href === pathname)
5777
let previousPage = linkIndex > -1 ? allLinks[linkIndex - 1] : null
5878
let nextPage = linkIndex > -1 ? allLinks[linkIndex + 1] : null

0 commit comments

Comments
 (0)