Now only the search suggestions are handled asynchronously.

This commit removes the async wrapper in the useEffect again. Instead the search suggestions are stored in their own state and the `quicklaunch` results show the last suggestions until they are updated.

This allows the search for services be fast even with a slow internet connection.
This commit is contained in:
Flo2410 2024-01-26 22:20:43 +00:00
parent cc73839874
commit 769beb7dc6
No known key found for this signature in database
GPG Key ID: 8ECB00AC5216DC7F

View File

@ -89,8 +89,8 @@ export default function QuickLaunch({
}
}
const [searchSuggestions, setSearchSuggestions] = useState([])
useEffect(() => {
(async () => {
if (searchString.length === 0) setResults([]);
else {
let newResults = servicesAndBookmarks.filter((r) => {
@ -115,24 +115,31 @@ export default function QuickLaunch({
});
if (!hideSearchSuggestions) {
const searchSuggestionResult = await fetch(`/api/searchSuggestion?query=${encodeURIComponent(searchString)}&providerName=${searchProvider.name}`);
const searchSuggestion = (await searchSuggestionResult.json())[1];
fetch(`/api/searchSuggestion?query=${encodeURIComponent(searchString)}&providerName=${searchProvider.name}`).then(async (searchSuggestionResult) => {
const newSearchSuggestions = (await searchSuggestionResult.json())[1];
// Check if there is a search suggestion
if (searchSuggestion) {
if (newSearchSuggestions) {
// Restrict the searchSuggestion to 4 entries
if (searchSuggestion.length - 4 > 0) {
searchSuggestion.splice(-(searchSuggestion.length-4));
if (newSearchSuggestions.length - 4 > 0) {
newSearchSuggestions.splice(-(newSearchSuggestions.length-4));
}
newResults = newResults.concat(searchSuggestion.map((suggestion)=>({
// Save the new search suggestions in their state.
setSearchSuggestions(newSearchSuggestions);
}
}).catch(() => {
// If there is an error, just ignore it. There just will be no search suggestions.
});
// Show search suggestions from their state. This will show the "old" suggestions until they are updated.
newResults = newResults.concat(searchSuggestions.map((suggestion)=>({
href: searchProvider.url + encodeURIComponent(suggestion),
name: suggestion,
type: "search",
})));
}
}
}
if (!hideVisitURL && url) {
newResults.unshift({
@ -148,8 +155,7 @@ export default function QuickLaunch({
setCurrentItemIndex(0);
}
}
})()
}, [searchString, servicesAndBookmarks, searchDescriptions, hideVisitURL, hideSearchSuggestions, searchProvider, url, t]);
}, [searchString, servicesAndBookmarks, searchDescriptions, hideVisitURL, hideSearchSuggestions, searchSuggestions, searchProvider, url, t]);
const [hidden, setHidden] = useState(true);
useEffect(() => {