45 lines
1.5 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: torrentData, error: torrentError } = useWidgetAPI(widget);
2022-09-26 00:35:54 +03:00
if (torrentError) {
2022-09-26 15:25:10 +03:00
return <Container error={t("widget.api_error")} />;
2022-09-26 00:35:54 +03:00
}
if (!torrentData) {
return (
<Container service={service}>
<Block label="transmission.leech" />
<Block label="transmission.download" />
<Block label="transmission.seed" />
<Block label="transmission.upload" />
2022-09-26 15:25:10 +03:00
</Container>
2022-09-26 00:35:54 +03:00
);
}
const { torrents } = torrentData.arguments;
const rateDl = torrents.reduce((acc, torrent) => acc + torrent.rateDownload, 0);
const rateUl = torrents.reduce((acc, torrent) => acc + torrent.rateUpload, 0);
const completed = torrents.filter((torrent) => torrent.percentDone === 1)?.length || 0;
const leech = torrents.length - completed || 0;
2022-09-26 00:35:54 +03:00
return (
<Container service={service}>
<Block label="transmission.leech" value={t("common.number", { value: leech })} />
<Block label="transmission.download" value={t("common.bitrate", { value: rateDl * 8 })} />
<Block label="transmission.seed" value={t("common.number", { value: completed })} />
<Block label="transmission.upload" value={t("common.bitrate", { value: rateUl * 8 })} />
2022-09-26 15:25:10 +03:00
</Container>
2022-09-26 00:35:54 +03:00
);
}