From c79d45f91e775b0d2eed070263021d0cffe695c9 Mon Sep 17 00:00:00 2001 From: Denis Papec Date: Sun, 16 Apr 2023 00:05:50 +0100 Subject: [PATCH] Add optional boxed styling and error component to information widgets Signed-off-by: Denis Papec --- src/components/widgets/datetime/datetime.jsx | 8 +++-- src/components/widgets/error.jsx | 23 +++++++++++++ src/components/widgets/glances/glances.jsx | 30 +++++++--------- src/components/widgets/greeting/greeting.jsx | 7 +++- .../widgets/kubernetes/kubernetes.jsx | 29 +++++++--------- src/components/widgets/logo/logo.jsx | 7 +++- src/components/widgets/longhorn/longhorn.jsx | 25 +++++++------- .../widgets/openmeteo/openmeteo.jsx | 34 +++++++++---------- .../widgets/openweathermap/weather.jsx | 33 +++++++++--------- .../widgets/resources/resources.jsx | 7 +++- src/components/widgets/search/search.jsx | 11 +++--- .../widgets/unifi_console/unifi_console.jsx | 26 +++++++------- src/components/widgets/weather/weather.jsx | 33 +++++++++--------- src/pages/api/widgets/longhorn.js | 2 +- 14 files changed, 153 insertions(+), 122 deletions(-) create mode 100644 src/components/widgets/error.jsx diff --git a/src/components/widgets/datetime/datetime.jsx b/src/components/widgets/datetime/datetime.jsx index 869834736..fc883ec35 100644 --- a/src/components/widgets/datetime/datetime.jsx +++ b/src/components/widgets/datetime/datetime.jsx @@ -1,5 +1,6 @@ import { useState, useEffect } from "react"; import { useTranslation } from "next-i18next"; +import classNames from "classnames"; const textSizes = { "4xl": "text-4xl", @@ -17,7 +18,7 @@ export default function DateTime({ options }) { const { i18n } = useTranslation(); const [date, setDate] = useState(""); const dateLocale = locale ?? i18n.language; - + useEffect(() => { const dateFormat = new Intl.DateTimeFormat(dateLocale, { ...format }); const interval = setInterval(() => { @@ -27,7 +28,10 @@ export default function DateTime({ options }) { }, [date, setDate, dateLocale, format]); return ( -
+
{date} diff --git a/src/components/widgets/error.jsx b/src/components/widgets/error.jsx new file mode 100644 index 000000000..92e0076a1 --- /dev/null +++ b/src/components/widgets/error.jsx @@ -0,0 +1,23 @@ +import { useTranslation } from "react-i18next"; +import { BiError } from "react-icons/bi"; +import classNames from "classnames"; + +export default function Error({ options }) { + const { t } = useTranslation(); + + return ( +
+
+
+ +
+ {t("widget.api_error")} +
+
+
+
+ ); +} diff --git a/src/components/widgets/glances/glances.jsx b/src/components/widgets/glances/glances.jsx index 85dd44c0c..b6daba7b7 100644 --- a/src/components/widgets/glances/glances.jsx +++ b/src/components/widgets/glances/glances.jsx @@ -1,11 +1,12 @@ import useSWR from "swr"; import { useContext } from "react"; -import { BiError } from "react-icons/bi"; import { FaMemory, FaRegClock, FaThermometerHalf } from "react-icons/fa"; import { FiCpu, FiHardDrive } from "react-icons/fi"; import { useTranslation } from "next-i18next"; +import classNames from "classnames"; import UsageBar from "../resources/usage-bar"; +import Error from "../error"; import { SettingsContext } from "utils/contexts/settings"; @@ -26,23 +27,15 @@ export default function Widget({ options }) { ); if (error || data?.error) { - return ( -
-
-
- -
- {t("widget.api_error")} -
-
-
-
- ); + return } if (!data) { return ( -
+
@@ -101,7 +94,10 @@ export default function Widget({ options }) { } return ( - +
@@ -184,7 +180,7 @@ export default function Widget({ options }) {
- {t("common.number", { + {t("common.number", { value: mainTemp, maximumFractionDigits: 1, style: "unit", @@ -196,7 +192,7 @@ export default function Widget({ options }) { {options.expanded && (
- {t("common.number", { + {t("common.number", { value: maxTemp, maximumFractionDigits: 1, style: "unit", diff --git a/src/components/widgets/greeting/greeting.jsx b/src/components/widgets/greeting/greeting.jsx index da0f063d1..2e129560c 100644 --- a/src/components/widgets/greeting/greeting.jsx +++ b/src/components/widgets/greeting/greeting.jsx @@ -1,3 +1,5 @@ +import classNames from "classnames"; + const textSizes = { "4xl": "text-4xl", "3xl": "text-3xl", @@ -12,7 +14,10 @@ const textSizes = { export default function Greeting({ options }) { if (options.text) { return ( -
+
{options.text} diff --git a/src/components/widgets/kubernetes/kubernetes.jsx b/src/components/widgets/kubernetes/kubernetes.jsx index 78c4caaf9..514993da3 100644 --- a/src/components/widgets/kubernetes/kubernetes.jsx +++ b/src/components/widgets/kubernetes/kubernetes.jsx @@ -1,12 +1,14 @@ import useSWR from "swr"; -import { BiError } from "react-icons/bi"; import { useTranslation } from "next-i18next"; +import classNames from "classnames"; + +import Error from "../error"; import Node from "./node"; export default function Widget({ options }) { const { cluster, nodes } = options; - const { t, i18n } = useTranslation(); + const { i18n } = useTranslation(); const defaultData = { cpu: { @@ -29,23 +31,15 @@ export default function Widget({ options }) { ); if (error || data?.error) { - return ( -
-
-
- -
- {t("widget.api_error")} -
-
-
-
- ); + return } if (!data) { return ( -
+
{cluster.show && @@ -59,7 +53,10 @@ export default function Widget({ options }) { } return ( -
+
{cluster.show && diff --git a/src/components/widgets/logo/logo.jsx b/src/components/widgets/logo/logo.jsx index 96e8569fc..6cba17bf7 100644 --- a/src/components/widgets/logo/logo.jsx +++ b/src/components/widgets/logo/logo.jsx @@ -1,8 +1,13 @@ +import classNames from "classnames"; + import ResolvedIcon from "components/resolvedicon" export default function Logo({ options }) { return ( -
+
{options.icon ? : // fallback to homepage logo diff --git a/src/components/widgets/longhorn/longhorn.jsx b/src/components/widgets/longhorn/longhorn.jsx index 9fcb21b4a..5139f00ad 100644 --- a/src/components/widgets/longhorn/longhorn.jsx +++ b/src/components/widgets/longhorn/longhorn.jsx @@ -1,37 +1,36 @@ import useSWR from "swr"; -import { BiError } from "react-icons/bi"; -import { useTranslation } from "next-i18next"; +import classNames from "classnames"; + +import Error from "../error"; import Node from "./node"; export default function Longhorn({ options }) { const { expanded, total, labels, include, nodes } = options; - const { t } = useTranslation(); const { data, error } = useSWR(`/api/widgets/longhorn`, { refreshInterval: 1500 }); if (error || data?.error) { - return ( -
- -
- {t("widget.api_error")} -
-
- ); + return } if (!data) { return ( -
+
); } return ( -
+
{data.nodes .filter((node) => { diff --git a/src/components/widgets/openmeteo/openmeteo.jsx b/src/components/widgets/openmeteo/openmeteo.jsx index 0d29aef53..1381cc55a 100644 --- a/src/components/widgets/openmeteo/openmeteo.jsx +++ b/src/components/widgets/openmeteo/openmeteo.jsx @@ -1,9 +1,11 @@ import useSWR from "swr"; import { useState } from "react"; -import { BiError } from "react-icons/bi"; import { WiCloudDown } from "react-icons/wi"; import { MdLocationDisabled, MdLocationSearching } from "react-icons/md"; import { useTranslation } from "next-i18next"; +import classNames from "classnames"; + +import Error from "../error"; import Icon from "./icon"; @@ -15,24 +17,15 @@ function Widget({ options }) { ); if (error || data?.error) { - return ( -
-
-
- -
- {t("widget.api_error")} - - -
-
-
-
- ); + return } if (!data) { return ( -
+
@@ -50,7 +43,10 @@ function Widget({ options }) { const timeOfDay = data.current_weather.time > data.daily.sunrise[0] && data.current_weather.time < data.daily.sunset[0] ? "day" : "night"; return ( -
+
@@ -107,8 +103,10 @@ export default function OpenMeteo({ options }) {