11"use client"
22
3- import { useState , useEffect , useRef , useCallback } from "react"
3+ import { useState , useRef , useCallback } from "react"
44import { motion , AnimatePresence , useScroll , useTransform } from "framer-motion"
55import { useRouter } from "next/navigation"
66import { Search , History , X , BookOpen , TrendingUp } from "lucide-react"
@@ -17,6 +17,7 @@ import { LiveTicker } from "@/shared/components/ui/live-ticker"
1717import { toast } from "sonner"
1818import { useI18n } from "@/shared/providers/locale-provider"
1919import { localizePathname } from "@/shared/i18n/config"
20+ import { useHasMounted } from "@/shared/hooks/use-has-mounted"
2021
2122export function HeroSection ( ) {
2223 const { t, locale } = useI18n ( )
@@ -25,10 +26,10 @@ export function HeroSection() {
2526 const [ open , setOpen ] = useState ( false )
2627 const [ isFocused , setIsFocused ] = useState ( false )
2728 const [ query , setQuery ] = useState ( "" )
28- const [ mounted , setMounted ] = useState ( false )
2929 const [ selectedIndex , setSelectedIndex ] = useState ( - 1 )
3030 const inputRef = useRef < HTMLInputElement > ( null )
3131 const sectionRef = useRef < HTMLElement > ( null )
32+ const mounted = useHasMounted ( )
3233 const isMobile = useIsMobile ( )
3334 const prefersReducedMotion = useReducedMotion ( )
3435
@@ -54,9 +55,8 @@ export function HeroSection() {
5455 ? t ( "home.search.placeholder.mobile" , { example : placeholderText } )
5556 : t ( "home.search.placeholder.desktop" , { example : placeholderText } )
5657
57- useEffect ( ( ) => {
58- setMounted ( true )
59- } , [ ] )
58+ const selectedSearch = selectedIndex >= 0 ? recentSearches [ selectedIndex ] ?? null : null
59+ const activeQuery = selectedSearch ?? query
6060
6161 const handleFocus = useCallback ( ( ) => {
6262 setOpen ( true )
@@ -92,7 +92,7 @@ export function HeroSection() {
9292
9393 const handleKeyDown = useCallback ( ( e : React . KeyboardEvent ) => {
9494 if ( ! open ) {
95- if ( e . key === 'Enter' ) handleSearch ( query )
95+ if ( e . key === 'Enter' ) handleSearch ( activeQuery )
9696 return
9797 }
9898 switch ( e . key ) {
@@ -107,7 +107,7 @@ export function HeroSection() {
107107 case 'Enter' :
108108 e . preventDefault ( )
109109 if ( selectedIndex >= 0 ) handleSearch ( recentSearches [ selectedIndex ] )
110- else handleSearch ( query )
110+ else handleSearch ( activeQuery )
111111 break
112112 case 'Escape' :
113113 setOpen ( false )
@@ -116,13 +116,7 @@ export function HeroSection() {
116116 inputRef . current ?. blur ( )
117117 break
118118 }
119- } , [ open , query , recentSearches , selectedIndex , handleSearch ] )
120-
121- useEffect ( ( ) => {
122- if ( selectedIndex >= 0 && recentSearches [ selectedIndex ] ) {
123- setQuery ( recentSearches [ selectedIndex ] )
124- }
125- } , [ selectedIndex , recentSearches ] )
119+ } , [ activeQuery , open , recentSearches , selectedIndex , handleSearch ] )
126120
127121 return (
128122 < section
@@ -191,7 +185,7 @@ export function HeroSection() {
191185 ref = { inputRef }
192186 className = "h-14 sm:h-16 w-full bg-transparent px-2 text-base sm:text-lg font-medium outline-none placeholder:text-muted-foreground/40 font-sans"
193187 placeholder = { placeholder }
194- value = { query }
188+ value = { activeQuery }
195189 onChange = { ( e ) => {
196190 setQuery ( e . target . value )
197191 setSelectedIndex ( - 1 )
@@ -215,7 +209,7 @@ export function HeroSection() {
215209 < Button
216210 size = "lg"
217211 className = "h-10 sm:h-12 rounded-xl px-4 sm:px-6 text-sm sm:text-base font-bold bg-primary text-primary-foreground hover:bg-primary/90 shadow-lg"
218- onClick = { ( ) => handleSearch ( query ) }
212+ onClick = { ( ) => handleSearch ( activeQuery ) }
219213 >
220214 { t ( "home.search.submit" ) }
221215 </ Button >
0 commit comments