2022-08-27 03:50:49 +03:00
|
|
|
import useSWR from "swr";
|
2022-09-08 11:48:16 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-08-27 03:50:49 +03:00
|
|
|
|
|
|
|
import Widget from "../widget";
|
|
|
|
import Block from "../block";
|
|
|
|
|
2022-09-04 21:58:42 +03:00
|
|
|
import { formatApiUrl } from "utils/api-helpers";
|
2022-08-27 03:50:49 +03:00
|
|
|
|
|
|
|
export default function Speedtest({ service }) {
|
2022-09-08 11:48:16 +03:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
2022-08-27 03:50:49 +03:00
|
|
|
const config = service.widget;
|
|
|
|
|
2022-09-04 21:58:42 +03:00
|
|
|
const { data: speedtestData, error: speedtestError } = useSWR(formatApiUrl(config, "speedtest/latest"));
|
2022-08-27 03:50:49 +03:00
|
|
|
|
|
|
|
if (speedtestError) {
|
2022-09-08 11:48:16 +03:00
|
|
|
return <Widget error={t("widget.api_error")} />;
|
2022-08-27 03:50:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!speedtestData) {
|
|
|
|
return (
|
|
|
|
<Widget>
|
2022-09-08 11:48:16 +03:00
|
|
|
<Block label={t("speedtest.download")} />
|
|
|
|
<Block label={t("speedtest.upload")} />
|
|
|
|
<Block label={t("speedtest.ping")} />
|
2022-08-27 03:50:49 +03:00
|
|
|
</Widget>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Widget>
|
2022-09-08 11:48:16 +03:00
|
|
|
<Block
|
|
|
|
label={t("speedtest.download")}
|
|
|
|
value={t("common.bitrate", { value: speedtestData.data.download * 1024 * 1024 })}
|
|
|
|
/>
|
|
|
|
<Block
|
|
|
|
label={t("speedtest.upload")}
|
|
|
|
value={t("common.bitrate", { value: speedtestData.data.upload * 1024 * 1024 })}
|
|
|
|
/>
|
|
|
|
<Block
|
|
|
|
label={t("speedtest.ping")}
|
|
|
|
value={t("common.ms", { value: speedtestData.data.ping, style: "unit", unit: "millisecond" })}
|
|
|
|
/>
|
2022-08-27 03:50:49 +03:00
|
|
|
</Widget>
|
|
|
|
);
|
|
|
|
}
|