2023-03-05 14:11:43 -08:00
|
|
|
import useSWR from "swr";
|
|
|
|
import { FaThermometerHalf } from "react-icons/fa";
|
|
|
|
import { useTranslation } from "next-i18next";
|
2023-03-30 14:40:24 -07:00
|
|
|
|
2023-06-05 23:18:18 +01:00
|
|
|
import Resource from "../widget/resource";
|
2023-06-03 01:10:15 +01:00
|
|
|
import Error from "../widget/error";
|
|
|
|
|
2023-03-31 08:44:27 -07:00
|
|
|
function convertToFahrenheit(t) {
|
2023-10-17 23:26:55 -07:00
|
|
|
return (t * 9) / 5 + 32;
|
2023-03-31 08:44:27 -07:00
|
|
|
}
|
|
|
|
|
2023-07-27 01:35:48 +02:00
|
|
|
export default function CpuTemp({ expanded, units, refresh = 1500 }) {
|
2023-03-05 14:11:43 -08:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
2023-10-18 23:49:42 +08:00
|
|
|
const { data, error } = useSWR(`api/widgets/resources?type=cputemp`, {
|
2023-07-27 01:35:48 +02:00
|
|
|
refreshInterval: refresh,
|
2023-03-05 14:11:43 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (error || data?.error) {
|
2023-10-17 23:26:55 -07:00
|
|
|
return <Error />;
|
2023-03-05 14:11:43 -08:00
|
|
|
}
|
|
|
|
|
2023-03-31 08:44:27 -07:00
|
|
|
if (!data || !data.cputemp) {
|
2023-10-17 23:26:55 -07:00
|
|
|
return (
|
|
|
|
<Resource
|
|
|
|
icon={FaThermometerHalf}
|
|
|
|
value="-"
|
|
|
|
label={t("resources.temp")}
|
|
|
|
expandedValue="-"
|
|
|
|
expandedLabel={t("resources.max")}
|
|
|
|
expanded={expanded}
|
|
|
|
/>
|
|
|
|
);
|
2023-03-05 14:11:43 -08:00
|
|
|
}
|
|
|
|
|
2023-03-31 08:44:27 -07:00
|
|
|
let mainTemp = data.cputemp.main;
|
|
|
|
if (data.cputemp.cores?.length) {
|
|
|
|
mainTemp = data.cputemp.cores.reduce((a, b) => a + b) / data.cputemp.cores.length;
|
|
|
|
}
|
2023-03-05 14:11:43 -08:00
|
|
|
const unit = units === "imperial" ? "fahrenheit" : "celsius";
|
2023-10-17 23:26:55 -07:00
|
|
|
mainTemp = unit === "celsius" ? mainTemp : convertToFahrenheit(mainTemp);
|
|
|
|
const maxTemp = unit === "celsius" ? data.cputemp.max : convertToFahrenheit(data.cputemp.max);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Resource
|
|
|
|
icon={FaThermometerHalf}
|
|
|
|
value={t("common.number", {
|
|
|
|
value: mainTemp,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
style: "unit",
|
|
|
|
unit,
|
|
|
|
})}
|
|
|
|
label={t("resources.temp")}
|
|
|
|
expandedValue={t("common.number", {
|
|
|
|
value: maxTemp,
|
|
|
|
maximumFractionDigits: 1,
|
|
|
|
style: "unit",
|
|
|
|
unit,
|
|
|
|
})}
|
|
|
|
expandedLabel={t("resources.max")}
|
|
|
|
percentage={Math.round((mainTemp / maxTemp) * 100)}
|
|
|
|
expanded={expanded}
|
|
|
|
/>
|
|
|
|
);
|
2023-03-05 14:11:43 -08:00
|
|
|
}
|