Skip to content

Commit 5936e20

Browse files
top users
1 parent 2e04aad commit 5936e20

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/components/navigation/Navigation.tsx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,44 @@
33
import { useContext, useRef, useState } from 'react'
44
import NavigationLogo from './NavigationLogo'
55
import { ContextTopTenPosts } from '@/context/ContextTopTenPosts'
6+
import { ContextTopUsers } from '@/context/ContextTopUsers'
67
import { useNavigationOpacity } from '@/hooks/useNavigationOpacity'
78
import NavigationButton from './NavigationButton'
89

910
const Navigation = () => {
1011
const contextTopTenPosts = useContext(ContextTopTenPosts)
1112
if (!contextTopTenPosts) {
1213
throw new Error(
13-
'Feed must be used within a ContextTopTenPosts.Provider'
14+
'Navigation must be used within a ContextTopTenPosts.Provider'
1415
)
1516
}
1617
const topTenPostsRef = contextTopTenPosts
1718

19+
const contextTopUsers = useContext(ContextTopUsers)
20+
if (!contextTopUsers) {
21+
throw new Error(
22+
'Navigation must be used within a ContextTopUsers.Provider'
23+
)
24+
}
25+
const topUsersRef = contextTopUsers
26+
1827
const [navOpacity, setNavOpacity] = useState<string>('opacity-100')
1928
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
2029

2130
useNavigationOpacity({ setNavOpacity, timerRef })
2231

32+
const handleScrollToTopUsers = () => {
33+
if (topUsersRef.current) {
34+
const OFFSET = 64
35+
const top =
36+
topUsersRef.current.getBoundingClientRect().top +
37+
window.scrollY -
38+
OFFSET
39+
40+
window.scrollTo({ top, behavior: 'smooth' })
41+
}
42+
}
43+
2344
const handleScrollToTopTenPosts = () => {
2445
if (topTenPostsRef.current) {
2546
const OFFSET = 64
@@ -40,11 +61,16 @@ const Navigation = () => {
4061
>
4162
<div className="max-w-7xl flex items-center justify-between m-auto h-16 px-2 sm:px-4 py-1 md:py-2">
4263
<NavigationLogo />
43-
44-
<NavigationButton
45-
handleScroll={handleScrollToTopTenPosts}
46-
label="Top 10 Posts"
47-
/>
64+
<div>
65+
<NavigationButton
66+
handleScroll={handleScrollToTopUsers}
67+
label="Most Active Users"
68+
/>
69+
<NavigationButton
70+
handleScroll={handleScrollToTopTenPosts}
71+
label="Top 10 Posts"
72+
/>
73+
</div>
4874
</div>
4975
</nav>
5076
)

0 commit comments

Comments
 (0)