Skip to content

Commit a6cb5d9

Browse files
committed
Add option to filter for filament names
1 parent 5fa01ad commit a6cb5d9

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

docs/widgets/services/spoolman.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ description: Spoolman Widget Configuration
55

66
Learn more about [Spoolman](https://github.com/Donkie/Spoolman).
77

8-
Keep track of your inventory of 3D-printer filament spools.
9-
Spoolman is a self-hosted web service designed to help you efficiently manage your 3D printer filament spools and monitor their usage. It acts as a centralized database that seamlessly integrates with popular 3D printing software like OctoPrint and Klipper/Moonraker. When connected, it automatically updates spool weights as printing progresses, giving you real-time insights into filament usage.
8+
4 spools are displayed by default. If more than 4 spools are configured in spoolman you can use the filamentNames configuration option to filter for spools by their filament name.
109

1110
```yaml
1211
widget:
1312
type: spoolman
1413
url: http://spoolman.host.or.ip
14+
filamentNames:
15+
- orange
16+
- blue
1517
```

src/widgets/spoolman/component.jsx

+32-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,22 @@ export default function Component({ service }) {
1010

1111
const { data: spoolData, error: spoolError } = useWidgetAPI(widget, "spools");
1212

13+
// Helper to handle filtering based on filamentNames
14+
const filterSpools = (data, filamentNames) => {
15+
if (!filamentNames || filamentNames.length === 0) return data; // No filtering if no filamentNames
16+
const limitedfilamentNames = filamentNames.slice(0, 4); // Limit to 4 names
17+
return data.filter(spool => limitedfilamentNames.includes(spool.filament.name));
18+
};
19+
20+
// Helper to limit spoolData length
21+
const limitSpoolData = (data, limit = 4) => (data.length > limit ? data.slice(0, limit) : data);
22+
23+
// Error handling
1324
if (spoolError) {
1425
return <Container service={service} error={spoolError} />;
1526
}
1627

28+
// Loading state
1729
if (!spoolData) {
1830
return (
1931
<Container service={service}>
@@ -23,17 +35,34 @@ export default function Component({ service }) {
2335
);
2436
}
2537

38+
// API error or unexpected response
2639
if (spoolData.error || spoolData.message) {
2740
return <Container service={service} error={spoolData?.error ?? spoolData} />;
2841
}
2942

43+
// No spools available
44+
if (spoolData.length === 0) {
45+
return (
46+
<Container service={service}>
47+
<Block label="spoolman.noSpools" />
48+
</Container>
49+
);
50+
}
51+
52+
// Filter and limit spools
53+
let filteredSpoolData = filterSpools(spoolData, widget.filamentNames);
54+
filteredSpoolData = limitSpoolData(filteredSpoolData);
55+
56+
// Render filtered and limited spools
3057
return (
3158
<Container service={service}>
32-
{spoolData.map((spool) => (
59+
{filteredSpoolData.map((spool) => (
3360
<Block
3461
key={spool.id}
35-
label={`${spool.filament.name}`}
36-
value={`${t("common.percent", { value: ((spool.remaining_weight / spool.initial_weight)*100)})}`}
62+
label={spool.filament.name}
63+
value={t("common.percent", {
64+
value: (spool.remaining_weight / spool.initial_weight) * 100,
65+
})}
3766
/>
3867
))}
3968
</Container>

0 commit comments

Comments
 (0)