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 (
2022-09-26 15:25:10 +03:00
<Container>
2022-09-26 00:35:54 +03:00
<Block label={t("transmission.leech")} />
<Block label={t("transmission.download")} />
<Block label={t("transmission.seed")} />
<Block label={t("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);
2022-09-26 00:35:54 +03:00
const leech = torrents.length - completed;
return (
2022-09-26 15:25:10 +03:00
<Container>
2022-09-26 00:35:54 +03:00
<Block label={t("transmission.leech")} value={t("common.number", { value: leech })} />
<Block label={t("transmission.download")} value={t("common.bitrate", { value: rateDl * 8 })} />
2022-09-26 00:35:54 +03:00
<Block label={t("transmission.seed")} value={t("common.number", { value: completed })} />
<Block label={t("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
);
}