2022-08-27 03:50:49 +03:00
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
import Widget from "../widget";
|
|
|
|
import Block from "../block";
|
|
|
|
|
|
|
|
import { formatBits } from "utils/stats-helpers";
|
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 }) {
|
|
|
|
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) {
|
|
|
|
return <Widget error="Speedtest API Error" />;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!speedtestData) {
|
|
|
|
return (
|
|
|
|
<Widget>
|
|
|
|
<Block label="Download" />
|
|
|
|
<Block label="Upload" />
|
|
|
|
<Block label="Ping" />
|
|
|
|
</Widget>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Widget>
|
2022-09-04 21:58:42 +03:00
|
|
|
<Block label="Download" value={`${formatBits(speedtestData.data.download * 1024 * 1024, 0)}ps`} />
|
|
|
|
<Block label="Upload" value={`${formatBits(speedtestData.data.upload * 1024 * 1024, 0)}ps`} />
|
2022-08-27 03:50:49 +03:00
|
|
|
<Block label="Ping" value={`${speedtestData.data.ping} ms`} />
|
|
|
|
</Widget>
|
|
|
|
);
|
|
|
|
}
|