2022-08-27 00:55:13 +03:00
|
|
|
import useSWR from "swr";
|
|
|
|
import { FiHardDrive } from "react-icons/fi";
|
2022-09-25 19:43:00 +03:00
|
|
|
import { useTranslation } from "next-i18next";
|
2022-09-09 07:10:33 +03:00
|
|
|
|
2023-06-05 23:18:18 +01:00
|
|
|
import Resource from "../widget/resource";
|
2023-06-03 01:10:15 +01:00
|
|
|
import Error from "../widget/error";
|
|
|
|
|
2023-07-27 01:35:48 +02:00
|
|
|
export default function Disk({ options, 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=disk&target=${options.disk}`, {
|
2023-07-27 01:35:48 +02:00
|
|
|
refreshInterval: refresh,
|
2022-08-27 00:55:13 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (error || data?.error) {
|
2023-06-03 01:10:15 +01:00
|
|
|
return <Error options={options} />
|
2022-08-27 00:55:13 +03:00
|
|
|
}
|
|
|
|
|
2023-09-24 08:31:15 -07:00
|
|
|
if (!data || !data.drive) {
|
2023-06-05 23:18:18 +01:00
|
|
|
return <Resource
|
|
|
|
icon={FiHardDrive}
|
|
|
|
value="-"
|
|
|
|
label={t("resources.free")}
|
|
|
|
expandedValue="-"
|
|
|
|
expandedLabel={t("resources.total")}
|
|
|
|
expanded={expanded}
|
|
|
|
percentage="0"
|
|
|
|
/>;
|
2022-08-27 00:55:13 +03:00
|
|
|
}
|
|
|
|
|
2023-03-30 14:36:40 -07:00
|
|
|
// data.drive.used not accurate?
|
|
|
|
const percent = Math.round(((data.drive.size - data.drive.available) / data.drive.size) * 100);
|
2022-09-07 17:17:01 +03:00
|
|
|
|
2023-06-05 23:18:18 +01:00
|
|
|
return <Resource
|
|
|
|
icon={FiHardDrive}
|
|
|
|
value={t("common.bytes", { value: data.drive.available })}
|
|
|
|
label={t("resources.free")}
|
|
|
|
expandedValue={t("common.bytes", { value: data.drive.size })}
|
|
|
|
expandedLabel={t("resources.total")}
|
|
|
|
percentage={percent}
|
|
|
|
expanded={expanded}
|
|
|
|
/>;
|
2022-08-27 00:55:13 +03:00
|
|
|
}
|