Create the MySpeed widget
This commit is contained in:
parent
0962e3384c
commit
84e1b92125
64
src/widgets/myspeed/component.jsx
Normal file
64
src/widgets/myspeed/component.jsx
Normal file
@ -0,0 +1,64 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
const { widget } = service;
|
||||
const { data, error } = useWidgetAPI(widget, "info");
|
||||
|
||||
if (error) {
|
||||
return <Container service={service} error={error} />;
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="myspeed.ping" />
|
||||
<Block label="myspeed.download" />
|
||||
<Block label="myspeed.upload" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
if (data.message) {
|
||||
return <Container service={service} error={data} />;
|
||||
}
|
||||
|
||||
if (Array.isArray(data) && data.length > 0 && data[0].error) {
|
||||
try {
|
||||
return <Container service={service} error={JSON.parse(data[0].error)} />;
|
||||
} catch (e) {
|
||||
return <Container service={service} error={data[0].error} />;
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block
|
||||
label="myspeed.ping"
|
||||
value={t("common.ms", {
|
||||
value: data[0].ping,
|
||||
style: "unit",
|
||||
unit: "millisecond",
|
||||
})}
|
||||
/>
|
||||
<Block
|
||||
label="myspeed.download"
|
||||
value={t("common.bitrate", {
|
||||
value: data[0].download * 1000 * 1000,
|
||||
decimals: 2,
|
||||
})}
|
||||
/>
|
||||
<Block
|
||||
label="myspeed.upload"
|
||||
value={t("common.bitrate", {
|
||||
value: data[0].upload * 1000 * 1000,
|
||||
decimals: 2,
|
||||
})}
|
||||
/>
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
14
src/widgets/myspeed/widget.js
Normal file
14
src/widgets/myspeed/widget.js
Normal file
@ -0,0 +1,14 @@
|
||||
import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/{endpoint}",
|
||||
proxyHandler: credentialedProxyHandler,
|
||||
|
||||
mappings: {
|
||||
info: {
|
||||
endpoint: "speedtests?limit=1"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default widget;
|
||||
Loading…
Reference in New Issue
Block a user