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(() => { useEffect(() => {
(async () => {
if (searchString.length === 0) setResults([]); if (searchString.length === 0) setResults([]);
else { else {
let newResults = servicesAndBookmarks.filter((r) => { let newResults = servicesAndBookmarks.filter((r) => {
@ -115,24 +115,31 @@ export default function QuickLaunch({
}); });
if (!hideSearchSuggestions) { if (!hideSearchSuggestions) {
const searchSuggestionResult = await fetch(`/api/searchSuggestion?query=${encodeURIComponent(searchString)}&providerName=${searchProvider.name}`); fetch(`/api/searchSuggestion?query=${encodeURIComponent(searchString)}&providerName=${searchProvider.name}`).then(async (searchSuggestionResult) => {
const searchSuggestion = (await searchSuggestionResult.json())[1]; const newSearchSuggestions = (await searchSuggestionResult.json())[1];
// Check if there is a search suggestion // Check if there is a search suggestion
if (searchSuggestion) { if (newSearchSuggestions) {
// Restrict the searchSuggestion to 4 entries // Restrict the searchSuggestion to 4 entries
if (searchSuggestion.length - 4 > 0) { if (newSearchSuggestions.length - 4 > 0) {
searchSuggestion.splice(-(searchSuggestion.length-4)); 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), href: searchProvider.url + encodeURIComponent(suggestion),
name: suggestion, name: suggestion,
type: "search", type: "search",
}))); })));
} }
} }
}
if (!hideVisitURL && url) { if (!hideVisitURL && url) {
newResults.unshift({ newResults.unshift({
@ -148,8 +155,7 @@ export default function QuickLaunch({
setCurrentItemIndex(0); setCurrentItemIndex(0);
} }
} }
})() }, [searchString, servicesAndBookmarks, searchDescriptions, hideVisitURL, hideSearchSuggestions, searchSuggestions, searchProvider, url, t]);
}, [searchString, servicesAndBookmarks, searchDescriptions, hideVisitURL, hideSearchSuggestions, searchProvider, url, t]);
const [hidden, setHidden] = useState(true); const [hidden, setHidden] = useState(true);
useEffect(() => { useEffect(() => {