85 lines
2.4 KiB
React
Raw Normal View History

2023-08-22 10:49:14 +03:00
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";
export default function Component({ service }) {
const { t } = useTranslation();
const { widget } = service;
2023-09-06 13:53:39 +03:00
const { chart } = widget;
2023-08-22 10:49:14 +03:00
const [, fsName] = widget.metric.split(':');
const { data, error } = useWidgetAPI(widget, 'fs', {
refreshInterval: 1000,
});
if (error) {
2023-09-06 15:28:37 +03:00
return <Container chart={chart}><Error error={error} /></Container>;
2023-08-22 10:49:14 +03:00
}
if (!data) {
2023-09-06 15:28:37 +03:00
return <Container chart={chart}><Block position="bottom-3 left-3">-</Block></Container>;
2023-08-22 10:49:14 +03:00
}
const fsData = data.find((item) => item[item.key] === fsName);
if (!fsData) {
2023-09-06 15:28:37 +03:00
return <Container chart={chart}><Block position="bottom-3 left-3">-</Block></Container>;
2023-08-22 10:49:14 +03:00
}
return (
2023-09-06 13:53:39 +03:00
<Container chart={chart}>
{ chart && (
<div className="absolute top-0 left-0 right-0 bottom-0">
2023-08-22 10:49:14 +03:00
<div style={{
2023-09-06 13:53:39 +03:00
height: `${Math.max(20, (fsData.size/fsData.free))}%`,
}} className="absolute bottom-0 border-t border-t-theme-500 bg-gradient-to-b from-theme-500/40 to-theme-500/10 w-full" />
</div>
)}
<Block position="bottom-3 left-3">
{ fsData.used && chart && (
<div className="text-xs opacity-50">
2023-08-22 10:49:14 +03:00
{t("common.bbytes", {
value: fsData.used,
maximumFractionDigits: 0,
})} {t("resources.used")}
</div>
2023-09-06 13:53:39 +03:00
)}
<div className="text-xs opacity-75">
{t("common.bbytes", {
value: fsData.free,
maximumFractionDigits: 0,
})} {t("resources.free")}
</div>
</Block>
{ !chart && (
<Block position="top-3 right-3">
{fsData.used && (
<div className="text-xs opacity-50">
{t("common.bbytes", {
value: fsData.used,
maximumFractionDigits: 0,
})} {t("resources.used")}
</div>
)}
</Block>
)}
2023-08-22 10:49:14 +03:00
2023-09-06 13:53:39 +03:00
<Block position="bottom-3 right-3">
<div className="text-xs opacity-75">
2023-08-22 10:49:14 +03:00
{t("common.bbytes", {
value: fsData.size,
maximumFractionDigits: 1,
2023-09-06 13:53:39 +03:00
})} {t("resources.total")}
2023-08-22 10:49:14 +03:00
</div>
</Block>
</Container>
);
}