52 lines
1.6 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 SingleResource from "../widget/single_resource";
import WidgetIcon from "../widget/widget_icon";
import ResourceValue from "../widget/resource_value";
import ResourceLabel from "../widget/resource_label";
import Error from "../widget/error";
import UsageBar from "./usage-bar";
2022-09-12 21:13:57 +03:00
export default function Memory({ expanded }) {
2022-09-08 11:48:16 +03:00
const { t } = useTranslation();
2022-08-27 00:55:13 +03:00
const { data, error } = useSWR(`/api/widgets/resources?type=memory`, {
refreshInterval: 1500,
});
if (error || data?.error) {
return <Error />
2022-08-27 00:55:13 +03:00
}
if (!data) {
return <SingleResource expanded={expanded}>
<WidgetIcon icon={FaMemory} />
<ResourceValue>-</ResourceValue>
<ResourceLabel>{t("resources.free")}</ResourceLabel>
<ResourceValue>-</ResourceValue>
<ResourceLabel>{t("resources.total")}</ResourceLabel>
<UsageBar percent={0} />
</SingleResource>;
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 <SingleResource expanded={expanded}>
<WidgetIcon icon={FaMemory} />
<ResourceValue>{t("common.bytes", { value: data.memory.available, maximumFractionDigits: 1, binary: true })}</ResourceValue>
<ResourceLabel>{t("resources.free")}</ResourceLabel>
<ResourceValue>
{t("common.bytes", {
value: data.memory.total,
maximumFractionDigits: 1,
binary: true,
})}
</ResourceValue>
<ResourceLabel>{t("resources.total")}</ResourceLabel>
<UsageBar percent={percent} />
</SingleResource>;
2022-08-27 00:55:13 +03:00
}