Skip to content

Commit c33af34

Browse files
Merge pull request #120 from replayio/aditya/fix-second-level-nagivation
2 parents 09eded2 + 0d693db commit c33af34

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

src/components/Navigation.tsx

+15-28
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,30 @@ import { NavIcon } from './NavIcon'
1010
import styles from './Navigation.module.css'
1111

1212
function ItemLink({
13+
className,
1314
item,
14-
pathname,
1515
onLinkClick,
16-
className,
16+
parent,
17+
pathname,
1718
}: {
19+
className?: string
1820
item: NavigationItem
19-
pathname: string
2021
onLinkClick?: React.MouseEventHandler<HTMLAnchorElement>
21-
className?: string
22+
parent?: NavigationItem
23+
pathname: string
2224
}) {
25+
const isSelected =
26+
parent && item.href === parent.href
27+
? // This is for sub item with same link as parent (For example: Cypress of Test Runner)
28+
pathname === parent.href
29+
: // Rest of items
30+
item.href && pathname.includes(item.href)
31+
2332
return (
2433
<Link
2534
href={item.href || '#'}
2635
onClick={item.href ? onLinkClick : undefined}
27-
className={clsx(
28-
styles.item,
29-
className,
30-
item.href && pathname.includes(item.href) && styles.selected,
31-
)}
36+
className={clsx(styles.item, className, isSelected && styles.selected)}
3237
>
3338
<NavIcon
3439
icon={item.icon}
@@ -116,29 +121,11 @@ export function Navigation({
116121
>
117122
<ItemLink
118123
item={subItem}
124+
parent={item}
119125
pathname={pathname}
120126
onLinkClick={onLinkClick}
121127
/>
122128
</Disclosure.Button>
123-
{subItem.links && (
124-
<Disclosure.Panel as="ul">
125-
{subItem.links.map((tertiary) => (
126-
<li key={tertiary.title}>
127-
<Disclosure.Button
128-
className={`${styles.button} ${styles.tertiary}`}
129-
>
130-
<Link
131-
href={tertiary.href}
132-
onClick={onLinkClick}
133-
className={clsx(styles.item)}
134-
>
135-
{tertiary.title}
136-
</Link>
137-
</Disclosure.Button>
138-
</li>
139-
))}
140-
</Disclosure.Panel>
141-
)}
142129
</li>
143130
))}
144131
</Disclosure.Panel>

src/components/PrevNextLinks.tsx

+26-6
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,12 +50,32 @@ 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+
72+
const allLinks = flattenNavigation(navigation)
5373
export function PrevNextLinks() {
54-
let pathname = usePathname()
55-
let allLinks = navigation.flatMap((section) => section.links ?? [])
56-
let linkIndex = allLinks.findIndex((link) => link.href === pathname)
57-
let previousPage = linkIndex > -1 ? allLinks[linkIndex - 1] : null
58-
let nextPage = linkIndex > -1 ? allLinks[linkIndex + 1] : null
74+
const pathname = usePathname()
75+
76+
const linkIndex = allLinks.findIndex((link) => link.href === pathname)
77+
const previousPage = linkIndex > -1 ? allLinks[linkIndex - 1] : null
78+
const nextPage = linkIndex > -1 ? allLinks[linkIndex + 1] : null
5979

6080
if (!nextPage && !previousPage) {
6181
return null

src/lib/navigation.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ export type NavigationItem = {
88
title: string
99
}
1010

11-
12-
export const navigation: NavigationItem[] = [
11+
export const navigation: NavigationItem[] = [
1312
{
1413
title: 'Intro to time travel',
1514
href: '/time-travel-intro',
@@ -149,7 +148,7 @@ export const navigation: NavigationItem[] = [
149148
{ title: 'Overview', href: '/test-runners/overview', icon: 'overview' },
150149
{
151150
title: 'Cypress.io',
152-
href: '',
151+
href: '/test-runners/cypress-io',
153152
icon: 'cypress',
154153
links: [
155154
{
@@ -191,7 +190,7 @@ export const navigation: NavigationItem[] = [
191190
},
192191
{
193192
title: 'Playwright',
194-
href: '',
193+
href: '/test-runners/playwright/record-your-first-replay',
195194
icon: 'playwright',
196195
links: [
197196
{
@@ -202,7 +201,7 @@ export const navigation: NavigationItem[] = [
202201
{
203202
title: 'Debugging tests',
204203
href: '/test-runners/playwright/debugging-tests',
205-
icon: 'debuggingtests'
204+
icon: 'debuggingtests',
206205
},
207206
{
208207
title: 'GitHub actions',
@@ -336,7 +335,7 @@ export const navigation: NavigationItem[] = [
336335
links: [
337336
{
338337
title: 'Replay APIs',
339-
href: '',
338+
href: '/integrations/replay-apis/graphql-api',
340339
icon: 'replayapis',
341340
links: [
342341
{
@@ -358,7 +357,7 @@ export const navigation: NavigationItem[] = [
358357
},
359358
{
360359
title: 'Frameworks & libraries',
361-
href: '',
360+
href: '/integrations/frameworks-libraries/nextjs',
362361
icon: 'frameworks',
363362
links: [
364363
{
@@ -370,7 +369,7 @@ export const navigation: NavigationItem[] = [
370369
},
371370
{
372371
title: '3rd Party integrations',
373-
href: '',
372+
href: '/integrations/3rd-party-integrations/github-embeds',
374373
icon: 'integrations',
375374
links: [
376375
{

0 commit comments

Comments
 (0)