mirror of
https://github.com/karl0ss/homepage.git
synced 2025-05-05 23:13:40 +01:00
89 lines
2.4 KiB
React
89 lines
2.4 KiB
React
![]() |
import dynamic from "next/dynamic";
|
||
|
import { useState, useEffect } from "react";
|
||
|
import { useTranslation } from "next-i18next";
|
||
|
|
||
|
import Error from "../components/error";
|
||
|
import Container from "../components/container";
|
||
|
import Block from "../components/block";
|
||
|
|
||
|
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||
|
|
||
|
const ChartDual = dynamic(() => import("../components/chart_dual"), { ssr: false });
|
||
|
|
||
|
const pointsLimit = 15;
|
||
|
|
||
|
export default function Component({ service }) {
|
||
|
const { t } = useTranslation();
|
||
|
|
||
|
const [dataPoints, setDataPoints] = useState(new Array(pointsLimit).fill({ value: 0 }, 0, pointsLimit));
|
||
|
|
||
|
const { data, error } = useWidgetAPI(service.widget, 'mem', {
|
||
|
refreshInterval: 1000,
|
||
|
});
|
||
|
|
||
|
useEffect(() => {
|
||
|
if (data) {
|
||
|
setDataPoints((prevDataPoints) => {
|
||
|
const newDataPoints = [...prevDataPoints, { a: data.used, b: data.free }];
|
||
|
if (newDataPoints.length > pointsLimit) {
|
||
|
newDataPoints.shift();
|
||
|
}
|
||
|
return newDataPoints;
|
||
|
});
|
||
|
}
|
||
|
}, [data]);
|
||
|
|
||
|
if (error) {
|
||
|
return <Container><Error error={error} /></Container>;
|
||
|
}
|
||
|
|
||
|
if (!data) {
|
||
|
return <Container><Block position={{bottom: 2, left: 2}}>-</Block></Container>;
|
||
|
}
|
||
|
|
||
|
return (
|
||
|
<Container>
|
||
|
<ChartDual
|
||
|
dataPoints={dataPoints}
|
||
|
max={data.total}
|
||
|
label={[t("resources.used"), t("resources.free")]}
|
||
|
formatter={(value) => t("common.bytes", {
|
||
|
value,
|
||
|
maximumFractionDigits: 0,
|
||
|
})}
|
||
|
/>
|
||
|
|
||
|
{data && !error && (
|
||
|
<Block position={{bottom: 3, left: 3}}>
|
||
|
{data.free && (
|
||
|
<div className="text-xs opacity-50">
|
||
|
{t("common.bytes", {
|
||
|
value: data.free,
|
||
|
maximumFractionDigits: 0,
|
||
|
})} {t("resources.free")}
|
||
|
</div>
|
||
|
)}
|
||
|
|
||
|
{data.total && (
|
||
|
<div className="text-xs opacity-50">
|
||
|
{t("common.bytes", {
|
||
|
value: data.total,
|
||
|
maximumFractionDigits: 0,
|
||
|
})} {t("resources.total")}
|
||
|
</div>
|
||
|
)}
|
||
|
</Block>
|
||
|
)}
|
||
|
|
||
|
<Block position={{bottom: 3, right: 3}}>
|
||
|
<div className="text-xs font-bold opacity-75">
|
||
|
{t("common.bytes", {
|
||
|
value: data.used,
|
||
|
maximumFractionDigits: 0,
|
||
|
})} {t("resources.used")}
|
||
|
</div>
|
||
|
</Block>
|
||
|
</Container>
|
||
|
);
|
||
|
}
|