File tree 1 file changed +22
-2
lines changed
1 file changed +22
-2
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ import Link from 'next/link'
4
4
import { usePathname } from 'next/navigation'
5
5
import clsx from 'clsx'
6
6
7
- import { navigation } from '@/lib/navigation'
7
+ import { NavigationItem , navigation } from '@/lib/navigation'
8
8
9
9
function ArrowIcon ( props : React . ComponentPropsWithoutRef < 'svg' > ) {
10
10
return (
@@ -50,9 +50,29 @@ function PageLink({
50
50
)
51
51
}
52
52
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
+
53
72
export function PrevNextLinks ( ) {
54
73
let pathname = usePathname ( )
55
- let allLinks = navigation . flatMap ( ( section ) => section . links ?? [ ] )
74
+ let allLinks = flattenNavigation ( navigation )
75
+
56
76
let linkIndex = allLinks . findIndex ( ( link ) => link . href === pathname )
57
77
let previousPage = linkIndex > - 1 ? allLinks [ linkIndex - 1 ] : null
58
78
let nextPage = linkIndex > - 1 ? allLinks [ linkIndex + 1 ] : null
You can’t perform that action at this time.
0 commit comments