Refactor proxy for brevity

pull/442/head
Michael Shamoon 2 years ago
parent df3e2eff11
commit d942e989bd

@ -9,13 +9,13 @@ export default function Component({ service }) {
const { widget } = service;
const { data: homebridge, error: homebridgeError } = useWidgetAPI(widget, "info");
const { data: homebridgeData, error: homebridgeError } = useWidgetAPI(widget, "info");
if (homebridgeError || (homebridge && !homebridge.data)) {
if (homebridgeError || homebridgeData?.error) {
return <Container error={t("widget.api_error")} />;
}
if (!homebridge) {
if (!homebridgeData) {
return (
<Container service={service}>
<Block label="widget.status" />
@ -29,21 +29,21 @@ export default function Component({ service }) {
<Container service={service}>
<Block
label="widget.status"
value={`${homebridge.data.status[0].toUpperCase()}${homebridge.data.status.substr(1)}`}
value={`${homebridgeData.status[0].toUpperCase()}${homebridgeData.status.substr(1)}`}
/>
<Block
label="homebridge.updates"
value={
(homebridge.data.updateAvailable || homebridge.data.plugins.updatesAvailable)
(homebridgeData.updateAvailable || homebridgeData.plugins?.updatesAvailable)
? t("homebridge.update_available")
: t("homebridge.up_to_date")}
/>
{homebridge?.data?.childBridges.quantity > 0 &&
{homebridgeData?.childBridges?.total > 0 &&
<Block
label="homebridge.child_bridges"
value={t("homebridge.child_bridges_status", {
total: homebridge.data.childBridges.quantity,
ok: homebridge.data.childBridges.quantityWithOkStatus
total: homebridgeData.childBridges.total,
ok: homebridgeData.childBridges.running
})}
/>}
</Container>

@ -62,22 +62,6 @@ async function apiCall(widget, endpoint) {
return { status, contentType, data: JSON.parse(data.toString()), responseHeaders };
}
function formatPluginsResponse(plugins) {
const quantity = plugins?.data.filter(p => p.updateAvailable).length;
return {
updatesAvailable: quantity > 0,
quantity,
}
}
function formatChildBridgesResponse(childBridges) {
const quantity = childBridges?.data?.length
return {
quantity,
quantityWithOkStatus: childBridges?.data?.filter(cb => cb.status === "ok").length,
}
}
export default async function homebridgeProxyHandler(req, res) {
const { group, service } = req.query;
@ -97,17 +81,20 @@ export default async function homebridgeProxyHandler(req, res) {
await login(widget);
}
const statusRs = await apiCall(widget, "status/homebridge");
const versionRs = await apiCall(widget, "status/homebridge-version");
const childBrigdeRs = await apiCall(widget, "status/homebridge/child-bridges");
const pluginsRs = await apiCall(widget, "plugins");
const { data: statusData } = await apiCall(widget, "status/homebridge");
const { data: versionData } = await apiCall(widget, "status/homebridge-version");
const { data: childBridgeData } = await apiCall(widget, "status/homebridge/child-bridges");
const { data: pluginsData } = await apiCall(widget, "plugins");
return res.status(200).send({
data: {
status: statusRs?.data?.status,
updateAvailable: versionRs?.data?.updateAvailable,
plugins: formatPluginsResponse(pluginsRs),
childBridges: formatChildBridgesResponse(childBrigdeRs),
}
status: statusData?.status,
updateAvailable: versionData?.updateAvailable,
plugins: {
updatesAvailable: pluginsData?.filter(p => p.updateAvailable).length,
},
childBridges: {
running: childBridgeData?.filter(cb => cb.status === "ok").length,
total: childBridgeData?.length
}
});
}

Loading…
Cancel
Save