@@ -2,7 +2,6 @@ import { ChangeEvent, useCallback, useRef, useState } from 'react';
2
2
import { MatchResultItem , PageSearcher } from '../../logic/search' ;
3
3
import SearchSvg from './icons/search.svg' ;
4
4
import { ComponentPropsWithIsland } from '../../../shared/types/index' ;
5
- import { useLocaleSiteData } from '../../logic' ;
6
5
import { throttle } from 'lodash-es' ;
7
6
8
7
function SuggestionContent ( props : {
@@ -65,8 +64,9 @@ function SuggestionContent(props: {
65
64
}
66
65
67
66
// eslint-disable-next-line @typescript-eslint/no-unused-vars
68
- export function Search ( _props : ComponentPropsWithIsland ) {
69
- const localeData = useLocaleSiteData ( ) ;
67
+ export function Search (
68
+ props : ComponentPropsWithIsland & { langRoutePrefix : string }
69
+ ) {
70
70
const [ suggestions , setSuggestions ] = useState < MatchResultItem [ ] > ( [ ] ) ;
71
71
const [ query , setQuery ] = useState ( '' ) ;
72
72
const [ focused , setFocused ] = useState ( false ) ;
@@ -76,25 +76,29 @@ export function Search(_props: ComponentPropsWithIsland) {
76
76
const initPageSearcher = useCallback ( ( ) => {
77
77
if ( ! psRef . current ) {
78
78
return import ( '../../logic/search' ) . then ( ( { PageSearcher } ) => {
79
- psRef . current = new PageSearcher ( localeData . lang || 'en' ) ;
79
+ psRef . current = new PageSearcher ( props . langRoutePrefix ) ;
80
80
psRef . current . init ( ) ;
81
81
} ) ;
82
82
}
83
83
return Promise . resolve ( ) ;
84
- } , [ localeData . lang ] ) ;
84
+ } , [ props . langRoutePrefix ] ) ;
85
85
86
86
// eslint-disable-next-line react-hooks/exhaustive-deps
87
87
const onQueryChanged = useCallback (
88
- throttle ( async ( e : ChangeEvent < HTMLInputElement > ) => {
89
- await initPageSearcher ( ) ;
90
- const newQuery = e . target . value ;
91
- setQuery ( newQuery ) ;
92
- if ( psRef . current ) {
93
- psRef . current . match ( newQuery ) . then ( ( matched ) => {
94
- setSuggestions ( matched ) ;
95
- } ) ;
96
- }
97
- } , 300 ) ,
88
+ throttle (
89
+ async ( e : ChangeEvent < HTMLInputElement > ) => {
90
+ await initPageSearcher ( ) ;
91
+ const newQuery = e . target . value ;
92
+ setQuery ( newQuery ) ;
93
+ if ( psRef . current ) {
94
+ psRef . current . match ( newQuery ) . then ( ( matched ) => {
95
+ setSuggestions ( matched ) ;
96
+ } ) ;
97
+ }
98
+ } ,
99
+ 200 ,
100
+ { leading : true , trailing : true }
101
+ ) ,
98
102
[ initPageSearcher ]
99
103
) ;
100
104
return (
0 commit comments