Added search suggestions to the quick launch menu.
If this feture and the search feature of the `quicklaunch` are turned on, up to *four* search suggestions will be shown as results.
This commit is contained in:
parent
a01f60ec25
commit
cc73839874
@ -16,9 +16,9 @@ export default function QuickLaunch({
|
|||||||
}) {
|
}) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { settings } = useContext(SettingsContext);
|
const { settings } = useContext(SettingsContext);
|
||||||
const { searchDescriptions, hideVisitURL } = settings?.quicklaunch
|
const { searchDescriptions, hideVisitURL, hideSearchSuggestions } = settings?.quicklaunch
|
||||||
? settings.quicklaunch
|
? settings.quicklaunch
|
||||||
: { searchDescriptions: false, hideVisitURL: false };
|
: { searchDescriptions: false, hideVisitURL: false, hideSearchSuggestions: false };
|
||||||
|
|
||||||
const searchField = useRef();
|
const searchField = useRef();
|
||||||
|
|
||||||
@ -90,6 +90,7 @@ export default function QuickLaunch({
|
|||||||
}
|
}
|
||||||
|
|
||||||
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) => {
|
||||||
@ -112,6 +113,25 @@ export default function QuickLaunch({
|
|||||||
name: `${searchProvider.name ?? t("quicklaunch.custom")} ${t("quicklaunch.search")}`,
|
name: `${searchProvider.name ?? t("quicklaunch.custom")} ${t("quicklaunch.search")}`,
|
||||||
type: "search",
|
type: "search",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!hideSearchSuggestions) {
|
||||||
|
const searchSuggestionResult = await fetch(`/api/searchSuggestion?query=${encodeURIComponent(searchString)}&providerName=${searchProvider.name}`);
|
||||||
|
const searchSuggestion = (await searchSuggestionResult.json())[1];
|
||||||
|
|
||||||
|
// Check if there is a search suggestion
|
||||||
|
if (searchSuggestion) {
|
||||||
|
// Restrict the searchSuggestion to 4 entries
|
||||||
|
if (searchSuggestion.length - 4 > 0) {
|
||||||
|
searchSuggestion.splice(-(searchSuggestion.length-4));
|
||||||
|
}
|
||||||
|
|
||||||
|
newResults = newResults.concat(searchSuggestion.map((suggestion)=>({
|
||||||
|
href: searchProvider.url + encodeURIComponent(suggestion),
|
||||||
|
name: suggestion,
|
||||||
|
type: "search",
|
||||||
|
})));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!hideVisitURL && url) {
|
if (!hideVisitURL && url) {
|
||||||
@ -128,7 +148,8 @@ export default function QuickLaunch({
|
|||||||
setCurrentItemIndex(0);
|
setCurrentItemIndex(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [searchString, servicesAndBookmarks, searchDescriptions, hideVisitURL, searchProvider, url, t]);
|
})()
|
||||||
|
}, [searchString, servicesAndBookmarks, searchDescriptions, hideVisitURL, hideSearchSuggestions, searchProvider, url, t]);
|
||||||
|
|
||||||
const [hidden, setHidden] = useState(true);
|
const [hidden, setHidden] = useState(true);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@ -12,31 +12,37 @@ export const searchProviders = {
|
|||||||
google: {
|
google: {
|
||||||
name: "Google",
|
name: "Google",
|
||||||
url: "https://www.google.com/search?q=",
|
url: "https://www.google.com/search?q=",
|
||||||
|
suggestionUrl: "https://www.google.com/complete/search?client=chrome&q=home",
|
||||||
icon: SiGoogle,
|
icon: SiGoogle,
|
||||||
},
|
},
|
||||||
duckduckgo: {
|
duckduckgo: {
|
||||||
name: "DuckDuckGo",
|
name: "DuckDuckGo",
|
||||||
url: "https://duckduckgo.com/?q=",
|
url: "https://duckduckgo.com/?q=",
|
||||||
|
suggestionUrl: "https://duckduckgo.com/ac/?type=list&q=",
|
||||||
icon: SiDuckduckgo,
|
icon: SiDuckduckgo,
|
||||||
},
|
},
|
||||||
bing: {
|
bing: {
|
||||||
name: "Bing",
|
name: "Bing",
|
||||||
url: "https://www.bing.com/search?q=",
|
url: "https://www.bing.com/search?q=",
|
||||||
|
suggestionUrl: "https://api.bing.com/osjson.aspx?query=",
|
||||||
icon: SiMicrosoftbing,
|
icon: SiMicrosoftbing,
|
||||||
},
|
},
|
||||||
baidu: {
|
baidu: {
|
||||||
name: "Baidu",
|
name: "Baidu",
|
||||||
url: "https://www.baidu.com/s?wd=",
|
url: "https://www.baidu.com/s?wd=",
|
||||||
|
suggestionUrl: "http://suggestion.baidu.com/su?&action=opensearch&ie=utf-8&wd=",
|
||||||
icon: SiBaidu,
|
icon: SiBaidu,
|
||||||
},
|
},
|
||||||
brave: {
|
brave: {
|
||||||
name: "Brave",
|
name: "Brave",
|
||||||
url: "https://search.brave.com/search?q=",
|
url: "https://search.brave.com/search?q=",
|
||||||
|
suggestionUrl: "https://search.brave.com/api/suggest?&rich=false&q=",
|
||||||
icon: SiBrave,
|
icon: SiBrave,
|
||||||
},
|
},
|
||||||
custom: {
|
custom: {
|
||||||
name: "Custom",
|
name: "Custom",
|
||||||
url: false,
|
url: false,
|
||||||
|
suggestionUrl: false,
|
||||||
icon: FiSearch,
|
icon: FiSearch,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
14
src/pages/api/searchSuggestion.js
Normal file
14
src/pages/api/searchSuggestion.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { searchProviders } from "components/widgets/search/search";
|
||||||
|
import cachedFetch from "utils/proxy/cached-fetch";
|
||||||
|
|
||||||
|
export default async function handler(req, res) {
|
||||||
|
const { query, providerName } = req.query;
|
||||||
|
|
||||||
|
const provider = Object.values(searchProviders).find(({ name }) => name === providerName);
|
||||||
|
|
||||||
|
if (!provider.suggestionUrl) {
|
||||||
|
return res.json([query, []]); // Responde with the same array format but with no suggestions.
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.send(await cachedFetch(`${provider.suggestionUrl}${query}`, 5));
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user