11'use client'
22
3+ import { useContext , useRef , useState } from 'react'
34import NavigationLogo from './NavigationLogo'
4- import { useRef , useState } from 'react'
5+ import { Button } from '@/components/ui/button'
6+ import { ContextTopTenPosts } from '@/context/ContextTopTenPosts'
57import { useNavigationOpacity } from '@/hooks/useNavigationOpacity'
8+ import NavigationButton from './NavigationButton'
69
710const Navigation = ( ) => {
11+ const contextTopTenPosts = useContext ( ContextTopTenPosts )
12+ if ( ! contextTopTenPosts ) {
13+ throw new Error (
14+ 'Feed must be used within a ContextTopTenPosts.Provider'
15+ )
16+ }
17+ const topTenPostsRef = contextTopTenPosts
18+
819 const [ navOpacity , setNavOpacity ] = useState < string > ( 'opacity-100' )
920 const timerRef = useRef < ReturnType < typeof setTimeout > | null > ( null )
1021
1122 useNavigationOpacity ( { setNavOpacity, timerRef } )
1223
24+ const handleScrollToTopTenPosts = ( ) => {
25+ if ( topTenPostsRef . current ) {
26+ const OFFSET = 64
27+ const top =
28+ topTenPostsRef . current . getBoundingClientRect ( ) . top +
29+ window . scrollY -
30+ OFFSET
31+
32+ window . scrollTo ( { top, behavior : 'smooth' } )
33+ }
34+ }
35+
1336 return (
1437 < nav
1538 className = { `sticky top-0 z-50 w-full transition-opacity duration-1000 ease-in-out ${ navOpacity }
@@ -18,6 +41,11 @@ const Navigation = () => {
1841 >
1942 < div className = "max-w-7xl flex items-center justify-between m-auto h-16 px-2 sm:px-4 py-1 md:py-2" >
2043 < NavigationLogo />
44+
45+ < NavigationButton
46+ handleScroll = { handleScrollToTopTenPosts }
47+ label = "Top 10 Posts"
48+ />
2149 </ div >
2250 </ nav >
2351 )
0 commit comments