@@ -102,32 +102,34 @@ function SearchInput({
102102} : SearchInputProps ) {
103103 const [ value , setValue ] = useState ( '' ) ;
104104 const [ lastValue , setLastValue ] = useState ( '' ) ;
105-
106- const debouncedValue = useDebounce ( value , 280 ) ;
107-
108105 const [ lastDocSource , setLastDocSource ] = useState < DocSource > ( ) ;
109106 const [ isEmptyQuery , setIsEmptyQuery ] = useState ( true ) ;
110-
111107 const [ lastHistoryValue , setLastHistoryValue ] = useState ( '' ) ;
112108
109+ const debouncedValue = useDebounce ( value , 280 ) ;
110+
111+ const search = useCallback ( ( query : string ) => {
112+ invokeSearch ( query ) ;
113+ setLastValue ( query ) ;
114+ } , [ invokeSearch ] ) ;
115+
113116 useEffect ( ( ) => {
114117 if ( ! historyValue ) return ;
115- if ( historyValue !== lastHistoryValue && historyValue !== value ) {
116- if ( historyValue !== lastValue ) {
117- onDidQueryChanged ( ) ;
118- }
119- setValue ( historyValue ) ;
120- setLastHistoryValue ( historyValue ) ;
121- invokeSearch ( historyValue ) ;
122- setLastValue ( historyValue ) ;
123- }
118+ if ( historyValue === lastHistoryValue ) return ;
119+ if ( historyValue === value ) return ;
120+
121+ onDidQueryChanged ( ) ;
122+ setValue ( historyValue ) ;
123+ setLastHistoryValue ( historyValue ) ;
124+
125+ search ( historyValue ) ;
124126 } , [
125127 historyValue ,
126- onDidQueryChanged ,
127- lastValue ,
128128 lastHistoryValue ,
129129 value ,
130- invokeSearch ,
130+ lastValue ,
131+ onDidQueryChanged ,
132+ search ,
131133 ] ) ;
132134
133135 function handleChangeValue ( e : any ) {
@@ -147,14 +149,15 @@ function SearchInput({
147149 }
148150
149151 useEffect ( ( ) => {
150- if ( activeDocSource !== lastDocSource ) {
151- setLastDocSource ( activeDocSource ) ;
152- invokeSearch ( value ) ;
153- }
152+ if ( activeDocSource === lastDocSource ) return ;
153+
154+ setLastDocSource ( activeDocSource ) ;
155+ search ( value ) ;
154156 } , [
155- value ,
156- lastDocSource ,
157157 activeDocSource ,
158+ lastDocSource ,
159+ search ,
160+ value ,
158161 ] ) ;
159162
160163 function handleInputKeyDown ( e : any ) {
@@ -172,32 +175,27 @@ function SearchInput({
172175 useHotkeys ( 'enter' , ( event ) => {
173176 if ( isSignInModalOpened ) return ;
174177 if ( isDocsFilterModalOpened ) return ;
175-
176178 if ( isSearchHistoryPreviewVisible ) return onEnterInSearchHistory ( ) ;
177-
178- if ( searchMode !== SearchMode . OnEnterPress ) return ;
179- invokeSearch ( value ) ;
180- setLastValue ( value ) ;
179+ if ( searchMode === SearchMode . OnEnterPress ) search ( value ) ;
181180 } , { filter : ( ) => true } , [
182- invokeSearch ,
183181 isSignInModalOpened ,
184182 isDocsFilterModalOpened ,
185- value ,
186- searchMode ,
183+ isSearchHistoryPreviewVisible ,
187184 onEnterInSearchHistory ,
185+ searchMode ,
186+ search ,
187+ value ,
188188 ] ) ;
189189
190190 useEffect ( ( ) => {
191- if ( searchMode !== SearchMode . Automatic ) return ;
192-
193- if ( lastValue === debouncedValue ) return ;
194- invokeSearch ( debouncedValue ) ;
195- setLastValue ( debouncedValue ) ;
191+ if ( searchMode === SearchMode . Automatic && lastValue !== debouncedValue ) {
192+ search ( debouncedValue ) ;
193+ }
196194 } , [
197- invokeSearch ,
198- debouncedValue ,
199195 searchMode ,
200196 lastValue ,
197+ debouncedValue ,
198+ search ,
201199 ] ) ;
202200
203201 useIPCRenderer ( 'did-show-main-window' , ( ) => {
@@ -237,7 +235,7 @@ function SearchInput({
237235
238236 { searchMode === SearchMode . OnEnterPress &&
239237 < HotkeyWrapper
240- onClick = { ( ) => invokeSearch ( value ) }
238+ onClick = { ( ) => search ( value ) }
241239 >
242240 < Hotkey
243241 hotkey = { [ 'Enter' ] }
0 commit comments