import useSWR from "swr"; import { BiError } from "react-icons/bi"; import { FaMemory, FaRegClock, FaThermometerHalf } from "react-icons/fa"; import { FiCpu } from "react-icons/fi"; import { useTranslation } from "next-i18next"; import UsageBar from "../resources/usage-bar"; export default function Widget({ options }) { const { t, i18n } = useTranslation(); const { data, error } = useSWR( `/api/widgets/glances?${new URLSearchParams({ lang: i18n.language, ...options }).toString()}`, { refreshInterval: 1500, } ); if (error || data?.error) { return (
{t("widget.api_error")}
); } if (!data) { return (
{t("glances.wait")}
{t("glances.wait")}
{options.label && (
{options.label}
)}
); } const unit = options.units === "imperial" ? "fahrenheit" : "celsius"; let mainTemp; let maxTemp = 80; if (options.cputemp && data.sensors) { mainTemp = unit === "celsius" ? data.sensors.find(s => s.label.includes("cpu_thermal")).value : data.sensors.find(s => s.label.includes("cpu_thermal")).value * 5/9 + 32; if (data.sensors.warning) maxTemp = data.sensors.warning; } const tempPercent = Math.round((mainTemp / maxTemp) * 100); return (
{t("common.number", { value: data.quicklook.cpu, style: "unit", unit: "percent", maximumFractionDigits: 0, })}
{t("glances.cpu")}
{t("common.number", { value: data.quicklook.mem, style: "unit", unit: "percent", maximumFractionDigits: 0, })}
{t("glances.mem")}
{options.cputemp && mainTemp && (
{t("common.number", { value: mainTemp, maximumFractionDigits: 1, style: "unit", unit })}
{t("glances.temp")}
)} {options.uptime && data.uptime && (
{data.uptime.replace(" days,", t("glances.days")).replace(/:\d\d:\d\d$/g, t("glances.hours"))}
{t("glances.uptime")}
)}
{options.label && (
{options.label}
)}
); }