Skip to content

Commit 94bbcbe

Browse files
fgeckshamoon
andauthored
Feature: Spoolman Widget (#3959)
Co-authored-by: shamoon <[email protected]>
1 parent 4a3a4c8 commit 94bbcbe

File tree

8 files changed

+105
-0
lines changed

8 files changed

+105
-0
lines changed

docs/widgets/services/spoolman.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: Spoolman
3+
description: Spoolman Widget Configuration
4+
---
5+
6+
Learn more about [Spoolman](https://github.com/Donkie/Spoolman).
7+
8+
4 spools are displayed by default. If more than 4 spools are configured in spoolman you can use the spoolIds configuration option to control which are displayed.
9+
10+
```yaml
11+
widget:
12+
type: spoolman
13+
url: http://spoolman.host.or.ip
14+
spoolIds: [1, 2, 3, 4] # optional
15+
```

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ nav:
138138
- widgets/services/scrutiny.md
139139
- widgets/services/sonarr.md
140140
- widgets/services/speedtest-tracker.md
141+
- widgets/services/spoolman.md
141142
- widgets/services/stash.md
142143
- widgets/services/stocks.md
143144
- widgets/services/swagdashboard.md

public/locales/en/common.json

+3
Original file line numberDiff line numberDiff line change
@@ -998,5 +998,8 @@
998998
"progressing": "Progressing",
999999
"missing": "Missing",
10001000
"suspended": "Suspended"
1001+
},
1002+
"spoolman": {
1003+
"loading": "Loading"
10011004
}
10021005
}

src/utils/config/service-helpers.js

+6
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,9 @@ export function cleanServiceGroups(groups) {
492492

493493
// technitium
494494
range,
495+
496+
// spoolman
497+
spoolIds,
495498
} = cleanedService.widget;
496499

497500
let fieldsList = fields;
@@ -653,6 +656,9 @@ export function cleanServiceGroups(groups) {
653656
if (metrics) cleanedService.widget.metrics = metrics;
654657
if (refreshInterval) cleanedService.widget.refreshInterval = refreshInterval;
655658
}
659+
if (type === "spoolman") {
660+
if (spoolIds !== undefined) cleanedService.widget.spoolIds = spoolIds;
661+
}
656662
}
657663

658664
return cleanedService;

src/widgets/components.js

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ const components = {
111111
scrutiny: dynamic(() => import("./scrutiny/component")),
112112
sonarr: dynamic(() => import("./sonarr/component")),
113113
speedtest: dynamic(() => import("./speedtest/component")),
114+
spoolman: dynamic(() => import("./spoolman/component")),
114115
stash: dynamic(() => import("./stash/component")),
115116
stocks: dynamic(() => import("./stocks/component")),
116117
strelaysrv: dynamic(() => import("./strelaysrv/component")),

src/widgets/spoolman/component.jsx

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { useTranslation } from "next-i18next";
2+
3+
import Container from "components/services/widget/container";
4+
import Block from "components/services/widget/block";
5+
import useWidgetAPI from "utils/proxy/use-widget-api";
6+
7+
export default function Component({ service }) {
8+
const { t } = useTranslation();
9+
const { widget } = service;
10+
11+
// eslint-disable-next-line prefer-const
12+
let { data: spoolData, error: spoolError } = useWidgetAPI(widget, "spools");
13+
14+
if (spoolError) {
15+
return <Container service={service} error={spoolError} />;
16+
}
17+
18+
if (!spoolData) {
19+
const nBlocksGuess = widget.spoolIds?.length ?? 4;
20+
return (
21+
<Container service={service}>
22+
{[...Array(nBlocksGuess)].map((_, i) => (
23+
// eslint-disable-next-line react/no-array-index-key
24+
<Block key={i} label="spoolman.loading" />
25+
))}
26+
</Container>
27+
);
28+
}
29+
30+
if (spoolData.error || spoolData.message) {
31+
return <Container service={service} error={spoolData?.error ?? spoolData} />;
32+
}
33+
34+
if (spoolData.length === 0) {
35+
return (
36+
<Container service={service}>
37+
<Block label="spoolman.noSpools" />
38+
</Container>
39+
);
40+
}
41+
42+
if (widget.spoolIds?.length) {
43+
spoolData = spoolData.filter((spool) => widget.spoolIds.includes(spool.id));
44+
}
45+
46+
if (spoolData.length > 4) {
47+
spoolData = spoolData.slice(0, 4);
48+
}
49+
50+
return (
51+
<Container service={service}>
52+
{spoolData.map((spool) => (
53+
<Block
54+
key={spool.id}
55+
label={spool.filament.name}
56+
value={t("common.percent", {
57+
value: (spool.remaining_weight / spool.initial_weight) * 100,
58+
})}
59+
/>
60+
))}
61+
</Container>
62+
);
63+
}

src/widgets/spoolman/widget.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
2+
3+
const widget = {
4+
api: "{url}/api/v1/{endpoint}",
5+
proxyHandler: credentialedProxyHandler,
6+
7+
mappings: {
8+
spools: {
9+
endpoint: "spool",
10+
},
11+
},
12+
};
13+
14+
export default widget;

src/widgets/widgets.js

+2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ import sabnzbd from "./sabnzbd/widget";
102102
import scrutiny from "./scrutiny/widget";
103103
import sonarr from "./sonarr/widget";
104104
import speedtest from "./speedtest/widget";
105+
import spoolman from "./spoolman/widget";
105106
import stash from "./stash/widget";
106107
import stocks from "./stocks/widget";
107108
import strelaysrv from "./strelaysrv/widget";
@@ -237,6 +238,7 @@ const widgets = {
237238
scrutiny,
238239
sonarr,
239240
speedtest,
241+
spoolman,
240242
stash,
241243
stocks,
242244
strelaysrv,

0 commit comments

Comments
 (0)