homepage/src/widgets/rutorrent/component.jsx

42 lines
1.3 KiB
React
Raw Normal View History

2022-09-26 00:35:54 +03:00
import { useTranslation } from "next-i18next";
2022-09-26 15:25:10 +03:00
import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
2022-09-27 22:59:14 +03:00
import useWidgetAPI from "utils/proxy/use-widget-api";
2022-09-26 00:35:54 +03:00
export default function Component({ service }) {
const { t } = useTranslation();
2022-09-27 22:59:14 +03:00
const { widget } = service;
2022-09-26 00:35:54 +03:00
2022-09-27 22:59:14 +03:00
const { data: statusData, error: statusError } = useWidgetAPI(widget);
2022-09-26 00:35:54 +03:00
if (statusError) {
2022-09-26 15:25:10 +03:00
return <Container error={t("widget.api_error")} />;
2022-09-26 00:35:54 +03:00
}
if (!statusData) {
return (
2022-09-26 15:25:10 +03:00
<Container>
2022-09-26 00:35:54 +03:00
<Block label={t("rutorrent.active")} />
<Block label={t("rutorrent.upload")} />
<Block label={t("rutorrent.download")} />
2022-09-26 15:25:10 +03:00
</Container>
2022-09-26 00:35:54 +03:00
);
}
const upload = statusData.reduce((acc, torrent) => acc + parseInt(torrent["d.get_up_rate"], 10), 0);
const download = statusData.reduce((acc, torrent) => acc + parseInt(torrent["d.get_down_rate"], 10), 0);
const active = statusData.filter((torrent) => torrent["d.get_state"] === "1");
return (
2022-09-26 15:25:10 +03:00
<Container>
2022-09-26 00:35:54 +03:00
<Block label={t("rutorrent.active")} value={active.length} />
<Block label={t("rutorrent.upload")} value={t("common.bitrate", { value: upload })} />
<Block label={t("rutorrent.download")} value={t("common.bitrate", { value: download })} />
2022-09-26 15:25:10 +03:00
</Container>
2022-09-26 00:35:54 +03:00
);
}