Add Hoarder service widget
Add a new service widget for Hoarder to the homepage.
This commit is contained in:
parent
b2158b0921
commit
beca91b71e
3
src/pages/api/widgets/hoarder.js
Normal file
3
src/pages/api/widgets/hoarder.js
Normal file
@ -0,0 +1,3 @@
|
||||
import hoarderProxyHandler from "src/widgets/hoarder/proxy";
|
||||
|
||||
export default hoarderProxyHandler;
|
||||
35
src/widgets/hoarder/component.jsx
Normal file
35
src/widgets/hoarder/component.jsx
Normal file
@ -0,0 +1,35 @@
|
||||
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: bookmarksData, error: bookmarksError } = useWidgetAPI(widget, "bookmarks");
|
||||
const { data: notesData, error: notesError } = useWidgetAPI(widget, "notes");
|
||||
|
||||
if (bookmarksError || notesError) {
|
||||
const finalError = bookmarksError ?? notesError;
|
||||
return <Container service={service} error={finalError} />;
|
||||
}
|
||||
|
||||
if (!bookmarksData || !notesData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="hoarder.bookmarks" />
|
||||
<Block label="hoarder.notes" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="hoarder.bookmarks" value={t("common.number", { value: bookmarksData.length })} />
|
||||
<Block label="hoarder.notes" value={t("common.number", { value: notesData.length })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
56
src/widgets/hoarder/proxy.js
Normal file
56
src/widgets/hoarder/proxy.js
Normal file
@ -0,0 +1,56 @@
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import createLogger from "utils/logger";
|
||||
import widgets from "widgets/widgets";
|
||||
|
||||
const proxyName = "hoarderProxyHandler";
|
||||
const logger = createLogger(proxyName);
|
||||
|
||||
async function retrieveFromAPI(url, key) {
|
||||
const headers = {
|
||||
"content-type": "application/json",
|
||||
Authorization: `Bearer ${key}`,
|
||||
};
|
||||
|
||||
const [status, , data] = await httpProxy(url, { headers });
|
||||
|
||||
if (status !== 200) {
|
||||
throw new Error(`Error getting data from Hoarder: ${status}. Data: ${data.toString()}`);
|
||||
}
|
||||
|
||||
return JSON.parse(Buffer.from(data).toString());
|
||||
}
|
||||
|
||||
export default async function hoarderProxyHandler(req, res) {
|
||||
const { group, service, endpoint, index } = req.query;
|
||||
|
||||
if (!group || !service) {
|
||||
logger.debug("Invalid or missing service '%s' or group '%s'", service, group);
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
||||
const widget = await getServiceWidget(group, service, index);
|
||||
|
||||
if (!widget) {
|
||||
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group);
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
||||
if (!widget.key) {
|
||||
logger.debug("Invalid or missing key for service '%s' in group '%s'", service, group);
|
||||
return res.status(400).json({ error: "Missing widget key" });
|
||||
}
|
||||
|
||||
const apiURL = widgets[widget.type].api;
|
||||
|
||||
try {
|
||||
const url = new URL(formatApiCall(apiURL, { endpoint, ...widget }));
|
||||
const data = await retrieveFromAPI(url, widget.key);
|
||||
|
||||
return res.status(200).send(data);
|
||||
} catch (e) {
|
||||
if (e) logger.error(e);
|
||||
return res.status(500).send({ error: { message: e.message } });
|
||||
}
|
||||
}
|
||||
17
src/widgets/hoarder/widget.js
Normal file
17
src/widgets/hoarder/widget.js
Normal file
@ -0,0 +1,17 @@
|
||||
import hoarderProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/{endpoint}",
|
||||
proxyHandler: hoarderProxyHandler,
|
||||
|
||||
mappings: {
|
||||
bookmarks: {
|
||||
endpoint: "bookmarks",
|
||||
},
|
||||
notes: {
|
||||
endpoint: "notes",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
||||
@ -130,6 +130,7 @@ import xteve from "./xteve/widget";
|
||||
import urbackup from "./urbackup/widget";
|
||||
import romm from "./romm/widget";
|
||||
import zabbix from "./zabbix/widget";
|
||||
import hoarder from "./hoarder/widget";
|
||||
|
||||
const widgets = {
|
||||
adguard,
|
||||
@ -267,6 +268,7 @@ const widgets = {
|
||||
whatsupdocker,
|
||||
xteve,
|
||||
zabbix,
|
||||
hoarder,
|
||||
};
|
||||
|
||||
export default widgets;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user