43 lines
1.1 KiB
React
Raw Normal View History

2022-08-27 00:55:13 +03:00
import useSWR from "swr";
import { FaMemory } from "react-icons/fa";
2022-09-25 19:43:00 +03:00
import { useTranslation } from "next-i18next";
2022-09-09 07:10:33 +03:00
import Resource from "../widget/resource";
import Error from "../widget/error";
export default function Memory({ expanded, refresh = 1500 }) {
2022-09-08 11:48:16 +03:00
const { t } = useTranslation();
2023-10-18 23:49:42 +08:00
const { data, error } = useSWR(`api/widgets/resources?type=memory`, {
refreshInterval: refresh,
2022-08-27 00:55:13 +03:00
});
if (error || data?.error) {
return <Error />
2022-08-27 00:55:13 +03:00
}
if (!data) {
return <Resource
icon={FaMemory}
value="-"
label={t("resources.free")}
expandedValue="-"
expandedLabel={t("resources.total")}
expanded={expanded}
percentage="0"
/>;
2022-08-27 00:55:13 +03:00
}
2023-04-03 23:22:28 -07:00
const percent = Math.round((data.memory.active / data.memory.total) * 100);
2022-09-07 17:17:01 +03:00
return <Resource
icon={FaMemory}
value={t("common.bytes", { value: data.memory.available, maximumFractionDigits: 1, binary: true })}
label={t("resources.free")}
expandedValue={t("common.bytes", { value: data.memory.total, maximumFractionDigits: 1, binary: true })}
expandedLabel={t("resources.total")}
percentage={percent}
expanded={expanded}
/>;
2022-08-27 00:55:13 +03:00
}