From 4581c4eeb0c150d40c42eb3b3c95546f095aac88 Mon Sep 17 00:00:00 2001 From: aidenpwnz Date: Thu, 1 Sep 2022 19:11:45 +0200 Subject: [PATCH] FEAT: Searchbar || FIX: spacings, overflows --- src/components/widget.jsx | 2 + src/components/widgets/search/search.jsx | 73 ++++++++++++++++++++++ src/components/widgets/weather/weather.jsx | 2 +- src/pages/index.js | 22 +++++-- src/skeleton/widgets.yaml | 7 +++ 5 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 src/components/widgets/search/search.jsx diff --git a/src/components/widget.jsx b/src/components/widget.jsx index 72032b441..dc5bee1b0 100644 --- a/src/components/widget.jsx +++ b/src/components/widget.jsx @@ -1,12 +1,14 @@ import WeatherApi from "components/widgets/weather/weather"; import OpenWeatherMap from "components/widgets/openweathermap/weather"; import Resources from "components/widgets/resources/resources"; +import Search from "components/widgets/search/search"; const widgetMappings = { weather: WeatherApi, // This key will be deprecated in the future weatherapi: WeatherApi, openweathermap: OpenWeatherMap, resources: Resources, + search: Search, }; export default function Widget({ widget }) { diff --git a/src/components/widgets/search/search.jsx b/src/components/widgets/search/search.jsx new file mode 100644 index 000000000..752c4166a --- /dev/null +++ b/src/components/widgets/search/search.jsx @@ -0,0 +1,73 @@ +import { useEffect, useState } from "react"; +import { FiSearch } from "react-icons/fi"; +import { FcGoogle } from "react-icons/fc"; +import { SiDuckduckgo } from "react-icons/si"; +import { SiMicrosoftbing } from "react-icons/si"; + +export default function Search({ options, classN }) { + const providers = ["google", "bing", "duckduckgo", "custom"]; + const targets = ["_blank", "_parent", "_top"]; + + const [query, setQuery] = useState(""); + + function search() { + if (!providers.includes(options.provider)) { + return; + } else { + if (options.provider === "custom") { + if (targets.includes(options.target)) { + window.open(options.customdata.url + query, options.target); + } else window.open(options.customdata.url + query, "_self"); + } else { + if (targets.includes(options.target)) { + window.open(`https://www.${options.provider}.com/search?q=` + query, `${options.target}`); + } else window.open(`https://www.${options.provider}.com/search?q=` + query, "_self"); + } + } + + setQuery(""); + } + + if (!options || (options.provider === "custom" && !options.customdata)) { + return <>; + } + + return ( +
+
+ {options.provider == "google" ? ( + + ) : options.provider == "duckduckgo" ? ( + + ) : options.provider == "bing" ? ( + + ) : options.provider == "custom" ? ( + options.customdata.abbr.length > 2 ? ( + options.customdata.abbr.substring(0, 2) + ) : ( + options.customdata.abbr + ) + ) : ( + "" + )} +
+ 1 ? "pl-12" : "pl-10" + } w-full text-sm text-gray-900 bg-gray-50 rounded-full border border-gray-300 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500 h-full`} + placeholder="Search..." + onChange={(s) => setQuery(s.currentTarget.value)} + required + /> + +
+ ); +} diff --git a/src/components/widgets/weather/weather.jsx b/src/components/widgets/weather/weather.jsx index 72565872e..f1c2d9018 100644 --- a/src/components/widgets/weather/weather.jsx +++ b/src/components/widgets/weather/weather.jsx @@ -33,7 +33,7 @@ export default function WeatherApi({ options }) { } return ( -
+
diff --git a/src/pages/index.js b/src/pages/index.js index d994b2d8f..99c1e695c 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -8,6 +8,7 @@ import ServicesGroup from "components/services/group"; import BookmarksGroup from "components/bookmarks/group"; import Widget from "components/widget"; import { ColorProvider } from "utils/color-context"; +import Search from "components/widgets/search/search"; const ThemeToggle = dynamic(() => import("components/theme-toggle"), { ssr: false, @@ -17,7 +18,7 @@ const ColorToggle = dynamic(() => import("components/color-toggle"), { ssr: false, }); -const rightAlignedWidgets = ["weatherapi", "openweathermap", "weather"]; +const rightAlignedWidgets = ["weatherapi", "openweathermap", "weather", "search"]; export default function Home() { const { data: services, error: servicesError } = useSWR("/api/services"); @@ -31,7 +32,7 @@ export default function Home() { Welcome
-
+
{widgets && ( <> {widgets @@ -39,12 +40,21 @@ export default function Home() { .map((widget, i) => ( ))} -
+ {widgets + .filter((widget) => widget.type === "search") + .map( + (widget, i) => + ??
+ )} {widgets .filter((widget) => rightAlignedWidgets.includes(widget.type)) - .map((widget, i) => ( - - ))} + .map((widget, i) => + widget.type === "search" ? ( + + ) : ( + + ) + )} )}
diff --git a/src/skeleton/widgets.yaml b/src/skeleton/widgets.yaml index 93cbb9e7a..a964efddd 100644 --- a/src/skeleton/widgets.yaml +++ b/src/skeleton/widgets.yaml @@ -5,3 +5,10 @@ cpu: true memory: true disk: / + +- search: # Searchbar in widgets area + provider: custom # Can be google, duckduckgo, bing or custom. + target: _blank # Can be _blank, _top, _self or _parent. + customdata: + url: https://startpage.com/search?q= # Required for custom provider. Remember to add the q param as per your provider. + abbr: G # Can be omitted. Only the first 2 characters will be considered.