Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom API widget #1858

Merged
merged 2 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/utils/config/service-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ export function cleanServiceGroups(groups) {
stream, // mjpeg
fit,
method, // openmediavault widget
mappings, // customapi widget
refreshInterval,
} = cleanedService.widget;

let fieldsList = fields;
Expand Down Expand Up @@ -372,6 +374,10 @@ export function cleanServiceGroups(groups) {
if (type === "openmediavault") {
if (method) cleanedService.widget.method = method;
}
if (type === "customapi") {
if (mappings) cleanedService.widget.mappings = mappings;
if (refreshInterval) cleanedService.widget.refreshInterval = refreshInterval;
}
}

return cleanedService;
Expand Down
1 change: 1 addition & 0 deletions src/widgets/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const components = {
channelsdvrserver: dynamic(() => import("./channelsdvrserver/component")),
cloudflared: dynamic(() => import("./cloudflared/component")),
coinmarketcap: dynamic(() => import("./coinmarketcap/component")),
customapi: dynamic(() => import("./customapi/component")),
deluge: dynamic(() => import("./deluge/component")),
diskstation: dynamic(() => import("./diskstation/component")),
downloadstation: dynamic(() => import("./downloadstation/component")),
Expand Down
75 changes: 75 additions & 0 deletions src/widgets/customapi/component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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";

function getValue(field, data) {
let value = data;
let lastField = field;
let key = '';

while (typeof lastField === "object") {
key = Object.keys(lastField)[0] ?? null;

if (key === null) {
break;
}

value = value[key];
lastField = lastField[key];
}

if (typeof value === 'undefined') {
return null;
}

return value[lastField] ?? null;
}

function formatValue(t, mapping, value) {
switch (mapping?.format) {
case 'number':
return t("common.number", { value: parseInt(value, 10) });
case 'float':
return t("common.number", { value });
case 'percent':
return t("common.percent", { value });
case 'text':
default:
return value;
}
}

export default function Component({ service }) {
const { t } = useTranslation();

const { widget } = service;

const { mappings = [], refreshInterval = 10000 } = widget;
const { data: customData, error: customError } = useWidgetAPI(widget, null, {
refreshInterval: Math.max(1000, refreshInterval),
});

if (customError) {
return <Container service={service} error={customError} />;
}

if (!customData) {
return (
<Container service={service}>
{ mappings.slice(0,4).map(item => <Block label={item.label} key={item.field} />) }
</Container>
);
}

return (
<Container service={service}>
{ mappings.slice(0,4).map(mapping => <Block
label={mapping.label}
key={mapping.field}
value={formatValue(t, mapping, getValue(mapping.field, customData))}
/>) }
</Container>
);
}
8 changes: 8 additions & 0 deletions src/widgets/customapi/widget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import genericProxyHandler from "utils/proxy/handlers/generic";

const widget = {
api: "{url}",
proxyHandler: genericProxyHandler,
};

export default widget;
2 changes: 2 additions & 0 deletions src/widgets/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import changedetectionio from "./changedetectionio/widget";
import channelsdvrserver from "./channelsdvrserver/widget";
import cloudflared from "./cloudflared/widget";
import coinmarketcap from "./coinmarketcap/widget";
import customapi from "./customapi/widget";
import deluge from "./deluge/widget";
import diskstation from "./diskstation/widget";
import downloadstation from "./downloadstation/widget";
Expand Down Expand Up @@ -107,6 +108,7 @@ const widgets = {
channelsdvrserver,
cloudflared,
coinmarketcap,
customapi,
deluge,
diskstation,
downloadstation,
Expand Down