Skip to content

Commit a6dbbb2

Browse files
authored
Merge pull request #131 from forestream/lfop-61
Fix table of contents rendering after routing
2 parents 7ecaff2 + a55443c commit a6dbbb2

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

  • apps/landing/src/components/table-of-contents

apps/landing/src/components/table-of-contents/index.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { createContext, useContext, useEffect, useMemo, useState } from 'react'
66
export interface Content {
77
label: string
88
value: string
9+
pathname: string
910
}
1011

1112
function sortContentsByDomOrder(items: Content[]): Content[] {
@@ -44,13 +45,15 @@ export function TableOfContentsProvider({
4445
children: React.ReactNode
4546
}) {
4647
const pathname = usePathname()
47-
const [contents, setContents] = useState<{ label: string; value: string }[]>(
48+
const [contents, setContents] = useState<Content[]>([])
49+
const [intersectingContents, setIntersectingContents] = useState<Content[]>(
4850
[],
4951
)
50-
51-
const [intersectingContents, setIntersectingContents] = useState<
52-
{ label: string; value: string }[]
53-
>([])
52+
if (intersectingContents.some((content) => content.pathname !== pathname)) {
53+
setIntersectingContents((prev) =>
54+
prev.filter((content) => content.pathname === pathname),
55+
)
56+
}
5457

5558
const highlightedContent = intersectingContents[0] ?? null
5659

@@ -62,6 +65,7 @@ export function TableOfContentsProvider({
6265
const item = {
6366
label: entry.target.innerText ?? '',
6467
value: entry.target.id ?? '',
68+
pathname: pathname ?? '',
6569
}
6670
setContents((prev) =>
6771
prev.some((content) => content.value === item.value)
@@ -81,7 +85,7 @@ export function TableOfContentsProvider({
8185
}
8286
})
8387
})
84-
}, [])
88+
}, [pathname])
8589

8690
useEffect(() => {
8791
if (!io) return

0 commit comments

Comments
 (0)