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"; const cpuSensorLabels = ["cpu_thermal", "Core"]; function convertToFahrenheit(t) { return t * 9/5 + 32 } 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 = 0; let maxTemp = 80; const cpuSensors = data.sensors?.filter(s => cpuSensorLabels.some(label => s.label.startsWith(label)) && s.type === "temperature_core"); if (options.cputemp && cpuSensors) { try { mainTemp = cpuSensors.reduce((acc, s) => acc + s.value, 0) / cpuSensors.length; maxTemp = Math.max(cpuSensors.reduce((acc, s) => acc + s.warning, 0) / cpuSensors.length, maxTemp); if (unit === "fahrenheit") { mainTemp = convertToFahrenheit(mainTemp); maxTemp = convertToFahrenheit(maxTemp); } } catch (e) { // cpu sensor retrieval failed } } 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 > 0 && (
{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}
)}
); }