127 lines
3.3 KiB
React
Raw Normal View History

2023-08-01 13:05:17 +03:00
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 defaultPointsLimit = 15;
const defaultInterval = (isChart) => (isChart ? 1000 : 5000);
2023-08-01 13:05:17 +03:00
export default function Component({ service }) {
const { t } = useTranslation();
2023-09-06 13:53:39 +03:00
const { widget } = service;
const { chart } = widget;
const { refreshInterval = defaultInterval(chart), pointsLimit = defaultPointsLimit } = widget;
2023-09-06 13:53:39 +03:00
2023-08-01 13:05:17 +03:00
const [dataPoints, setDataPoints] = useState(new Array(pointsLimit).fill({ value: 0 }, 0, pointsLimit));
const { data, error } = useWidgetAPI(service.widget, "mem", {
refreshInterval: Math.max(defaultInterval(chart), refreshInterval),
2023-08-01 13:05:17 +03:00
});
useEffect(() => {
if (data) {
setDataPoints((prevDataPoints) => {
const newDataPoints = [...prevDataPoints, { a: data.used, b: data.free }];
if (newDataPoints.length > pointsLimit) {
newDataPoints.shift();
}
return newDataPoints;
2023-08-01 13:05:17 +03:00
});
}
}, [data, pointsLimit]);
2023-08-01 13:05:17 +03:00
if (error) {
return (
<Container chart={chart}>
<Error error={error} />
</Container>
);
2023-08-01 13:05:17 +03:00
}
if (!data) {
return (
<Container chart={chart}>
<Block position="bottom-3 left-3">-</Block>
</Container>
);
2023-08-01 13:05:17 +03:00
}
return (
<Container chart={chart}>
2023-09-06 13:53:39 +03:00
{chart && (
<ChartDual
dataPoints={dataPoints}
max={data.total}
label={[t("resources.used"), t("resources.free")]}
formatter={(value) =>
t("common.bytes", {
value,
maximumFractionDigits: 0,
binary: true,
})
}
2023-09-06 13:53:39 +03:00
/>
)}
2023-08-01 13:05:17 +03:00
{data && !error && (
2023-08-01 16:39:46 +03:00
<Block position="bottom-3 left-3">
2023-09-06 13:53:39 +03:00
{data.free && chart && (
2023-08-01 13:05:17 +03:00
<div className="text-xs opacity-50">
{t("common.bytes", {
value: data.free,
maximumFractionDigits: 1,
binary: true,
})}{" "}
{t("resources.free")}
2023-08-01 13:05:17 +03:00
</div>
)}
{data.total && (
<div className="text-xs opacity-50">
{t("common.bytes", {
value: data.total,
maximumFractionDigits: 1,
binary: true,
})}{" "}
{t("resources.total")}
2023-08-01 13:05:17 +03:00
</div>
)}
</Block>
)}
{!chart && (
2023-09-06 13:53:39 +03:00
<Block position="top-3 right-3">
{data.free && (
<div className="text-xs opacity-50">
{t("common.bytes", {
value: data.free,
maximumFractionDigits: 1,
2023-09-06 13:53:39 +03:00
binary: true,
})}{" "}
{t("resources.free")}
2023-09-06 13:53:39 +03:00
</div>
)}
</Block>
)}
2023-08-01 16:39:46 +03:00
<Block position="bottom-3 right-3">
2023-08-01 13:05:17 +03:00
<div className="text-xs font-bold opacity-75">
{t("common.bytes", {
value: data.used,
maximumFractionDigits: 1,
binary: true,
})}{" "}
{t("resources.used")}
2023-08-01 13:05:17 +03:00
</div>
</Block>
</Container>
);
}