33import { useContext , useRef , useState } from 'react'
44import NavigationLogo from './NavigationLogo'
55import { ContextTopTenPosts } from '@/context/ContextTopTenPosts'
6+ import { ContextTopUsers } from '@/context/ContextTopUsers'
67import { useNavigationOpacity } from '@/hooks/useNavigationOpacity'
78import NavigationButton from './NavigationButton'
89
910const 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