Skip to content

Commit 6120deb

Browse files
committed
Fix search input bug that caused an incorrect message to be shown
1 parent 8691d1f commit 6120deb

File tree

2 files changed

+45
-54
lines changed

2 files changed

+45
-54
lines changed

src/Home/SearchHeaderPanel/SearchInput.tsx

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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']}

src/Home/index.tsx

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,9 @@ function Home() {
904904
const [queryFromHistoryValue, setQueryFromHistoryValue] = useState('');
905905
const [hasQueryChanged, setHasQueryChanged] = useState(true);
906906

907+
908+
console.log('QUERY', hasQueryChanged);
909+
907910
const activeFilter = useMemo(() => state.search.filter, [state.search.filter]);
908911

909912
const activeFocusedIdx = useMemo(() => {
@@ -918,13 +921,6 @@ function Home() {
918921
return state.results[activeFilter].items.length === 0;
919922
}, [state.results, activeFilter]);
920923

921-
const hasCurrentQueryNoResults = useMemo(() => {
922-
return hasQueryChanged && hasActiveFilterEmptyResults;
923-
}, [
924-
hasActiveFilterEmptyResults,
925-
hasQueryChanged,
926-
]);
927-
928924
const isActiveFilterLoading = useMemo(() => {
929925
return state.results[activeFilter].isLoading;
930926
}, [state.results, activeFilter]);
@@ -1830,10 +1826,9 @@ function Home() {
18301826
{!isActiveFilterLoading &&
18311827
<>
18321828
{state.search.isQueryPresent
1829+
&& state.searchMode !== SearchMode.Automatic
18331830
&& hasActiveFilterEmptyResults
1834-
&& hasCurrentQueryNoResults
18351831
&& !hasQueryChanged
1836-
&& state.searchMode !== SearchMode.Automatic
18371832
&&
18381833
<InfoWrapper>
18391834
<InfoMessageLeft>
@@ -1847,15 +1842,14 @@ function Home() {
18471842
}
18481843

18491844
{state.search.isQueryPresent
1845+
&& state.searchMode !== SearchMode.Automatic
18501846
&& hasActiveFilterEmptyResults
1851-
&& !hasCurrentQueryNoResults
18521847
&& hasQueryChanged
1853-
&& state.searchMode !== SearchMode.Automatic
18541848
&&
18551849
<InfoWrapper>
18561850
<InfoMessageLeft>
18571851
Type your search query and press
1858-
</InfoMessageLeft>
1852+
</InfoMessageLeft>
18591853
<TextHotkey hotkey={['Enter']} />
18601854
<InfoMessageRight>
18611855
to search
@@ -1865,7 +1859,7 @@ function Home() {
18651859

18661860
{!state.search.isQueryPresent
18671861
&& state.searchMode !== SearchMode.Automatic
1868-
&& hasQueryChanged
1862+
&& hasActiveFilterEmptyResults
18691863
&&
18701864
<InfoWrapper>
18711865
<InfoMessageLeft>
@@ -1880,8 +1874,7 @@ function Home() {
18801874

18811875
{state.search.isQueryPresent
18821876
&& state.searchMode === SearchMode.Automatic
1883-
&& hasCurrentQueryNoResults
1884-
&& !hasQueryChanged
1877+
&& hasActiveFilterEmptyResults
18851878
&&
18861879
<InfoMessage>
18871880
Nothing found. Try a different query.
@@ -1890,7 +1883,7 @@ function Home() {
18901883

18911884
{!state.search.isQueryPresent
18921885
&& state.searchMode === SearchMode.Automatic
1893-
&& hasQueryChanged
1886+
&& hasActiveFilterEmptyResults
18941887
&&
18951888
<InfoMessage>
18961889
Type your search query

0 commit comments

Comments
 (0)