Skip to content

Commit 917eed8

Browse files
committed
Searchbar bug fix with whitespace
1 parent 1caf5c8 commit 917eed8

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

frontend/components/Navbar/Searchbar.tsx

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default function Searchbar() {
3535
const bkmkDispatch = useBookmarkDispatch();
3636
const [searchType, setSearchType] = useState(searchTypes[0]);
3737
const [searchText, setSearchText] = useState("");
38+
const [lastSearched, setLastSearched] = useState("");
3839
const [strTags, setStrTags] = useState<string[]>([]);
3940
const [shouldSplit, setShouldSplit] = useState(false);
4041
const [modified, setModified] = useState(false);
@@ -88,7 +89,6 @@ export default function Searchbar() {
8889
trimmedInput.length &&
8990
!strTags.includes(trimmedInput)
9091
) {
91-
console.log(trimmedInput);
9292
e.preventDefault();
9393
setStrTags((prevState) => [...prevState, trimmedInput]);
9494
setSearchText("");
@@ -107,6 +107,24 @@ export default function Searchbar() {
107107
}
108108
}
109109

110+
interface SearchHelper {
111+
text: string;
112+
wordsList: string[];
113+
}
114+
115+
function cleanText(text: string, wordsList: string[]): SearchHelper {
116+
const trimmed = text.trim();
117+
const nextWordStart = trimmed.lastIndexOf(" ");
118+
if (nextWordStart > 0) {
119+
wordsList.push(trimmed.slice(nextWordStart + 1));
120+
return cleanText(trimmed.slice(0, nextWordStart), wordsList);
121+
} else if (trimmed.length > 0) {
122+
wordsList.push(text);
123+
return cleanText("", wordsList);
124+
}
125+
return { text: "", wordsList: wordsList };
126+
}
127+
110128
function isSearchIsTypeSearchTypeChange(unmodifiedSearch: string): boolean {
111129
let input: string | undefined = unmodifiedSearch.at(1);
112130
let found = false;
@@ -141,12 +159,15 @@ export default function Searchbar() {
141159

142160
async function search(searchText: string, searchType: SearchTypeEnum) {
143161
let searchData: Bookmark[] = [];
144-
if (searchType == SearchTypeEnum.titleSearch) {
145-
await api
146-
.searchBookmarkByTitleKeywords(searchText.trim().replaceAll(" ", ","))
147-
.then((successResult) => {
162+
163+
if (searchType == SearchTypeEnum.titleSearch && searchText.trim().length) {
164+
const words = cleanText(searchText, []).wordsList.reverse().join(",");
165+
if (words.length && words.trim() !== lastSearched.trim()) {
166+
setLastSearched(words);
167+
await api.searchBookmarkByTitleKeywords(words).then((successResult) => {
148168
searchData = successResult.data as Bookmark[];
149169
});
170+
}
150171
} else if (searchType == SearchTypeEnum.tagSearch && strTags.length) {
151172
await api
152173
.searchBookmarkByTags(strTags.join(","))
@@ -156,10 +177,12 @@ export default function Searchbar() {
156177
} else if (searchType == SearchTypeEnum.textSearch) {
157178
await api.searchBookmarkByText(searchText);
158179
}
159-
bkmkDispatch({
160-
type: "search",
161-
bookmarks: searchData,
162-
});
180+
if (searchData.length > 0) {
181+
bkmkDispatch({
182+
type: "search",
183+
bookmarks: searchData,
184+
});
185+
}
163186
}
164187

165188
return (

0 commit comments

Comments
 (0)