diff --git a/src/components/widgets/search/search.jsx b/src/components/widgets/search/search.jsx index dfb18367e..61b8786d5 100644 --- a/src/components/widgets/search/search.jsx +++ b/src/components/widgets/search/search.jsx @@ -1,7 +1,8 @@ -import { useState } from "react"; +import { useState, useEffect, Fragment } from "react"; import { useTranslation } from "next-i18next"; import { FiSearch } from "react-icons/fi"; import { SiDuckduckgo, SiMicrosoftbing, SiGoogle, SiBaidu, SiBrave } from "react-icons/si"; +import { Listbox, Transition } from "@headlessui/react"; export const searchProviders = { google: { @@ -36,21 +37,59 @@ export const searchProviders = { }, }; +function classNames(...classes) { + return classes.filter(Boolean).join(" "); +} + +function useProviderState() { + const key = "search-name"; + + const [value, setValue] = useState(providers.google); + useEffect(() => { + const storedName = localStorage.getItem(key); + let storedProvider = null; + if (storedName) { + storedProvider = Object.values(providers).find((el) => el.name === storedName); + if (storedProvider) { + setValue(storedProvider); + } + } + }, []); + + return [ + value, + (val) => { + setValue(val); + localStorage.setItem(key, val.name); + }, + ]; +} + +function getAvailableProviderIds(options) { + if (options.providers && Array.isArray(options.providers)) { + return Object.keys(providers).filter((value) => options.providers.includes(value)); + } + return null; +} + export default function Search({ options }) { const { t } = useTranslation(); const provider = searchProviders[options.provider]; const [query, setQuery] = useState(""); + const [selectedProvider, setSelectedProvider] = useProviderState(); - if (!provider) { + const availableProviderIds = getAvailableProviderIds(options); + if (!provider && !availableProviderIds) { return null; } function handleSubmit(event) { const q = encodeURIComponent(query); - if (provider.url) { - window.open(`${provider.url}${q}`, options.target || "_blank"); + const url = provider ? provider.url : selectedProvider.url; + if (url) { + window.open(`${url}${q}`, options.target || "_blank"); } else { window.open(`${options.url}${q}`, options.target || "_blank"); } @@ -60,6 +99,73 @@ export default function Search({ options }) { setQuery(""); } + const multiProviders = () => ( + +
+ + + {t("search.search")} + +
+ + + +
+ {availableProviderIds.map((providerId) => { + const p = providers[providerId]; + return ( + + {({ active }) => ( +
  • + +
  • + )} +
    + ); + })} +
    +
    +
    +
    + ); + + const singleProvider = () => ( + + ); + return (
    @@ -82,17 +188,7 @@ export default function Search({ options }) { // eslint-disable-next-line jsx-a11y/no-autofocus autoFocus={options.focus} /> - + {provider ? singleProvider() : multiProviders()} ); }