mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-10-08 07:01:40 +02:00
An external spool holder that never gets used still takes a full card's width in the Filaments row, next to the AMS units that are actually in use. An eye icon at the right-hand end of that row's header now hides it, and clicking it again brings it back -- the affordance stays in place rather than moving to a settings page, so the choice is discoverable and reversible where it applies. Per printer rather than global. A global flag would suit a toolbar button, but an icon on the card that silently rearranged every other card would surprise; it is keyed by printer id in one localStorage entry, the same shape as printerCollapsedSections, and sits alongside the other browser-local printer-page view preferences. The toggle is offered only when the printer has at least one AMS. On a machine with no AMS the external spool is the entire filament section, so hiding it would leave an empty row with no control to undo it. The icon and the hide condition read the same canHideExternalSpool, so a preference stored before an AMS was unplugged cannot blank the row either -- the spool reappears instead. The store lives in a new utils/printerCardPrefs.ts rather than in the 9,157-line page. It re-reads before writing so two cards toggled in one session cannot clobber each other's entry, deletes the key instead of storing false, and treats a malformed or unavailable localStorage as "nothing hidden" so a private-mode browser cannot throw out of a render.
57 lines
2.2 KiB
TypeScript
57 lines
2.2 KiB
TypeScript
/**
|
|
* Per-printer view preferences for the printer card.
|
|
*
|
|
* These are browser-local, like every other printer-page view preference
|
|
* (`printerCardSize`, `hideDisconnectedPrinters`, `printerCollapsedSections`).
|
|
* They describe how one person wants their own screen to look, not anything
|
|
* about the printer, so they deliberately do not go to the backend.
|
|
*
|
|
* Keyed by printer id rather than held as a single global flag: the toggle
|
|
* lives on the card itself, so hiding the external spool on one printer must
|
|
* not silently rearrange every other card in a fleet.
|
|
*/
|
|
|
|
const HIDDEN_EXTERNAL_SPOOLS_KEY = 'printerHiddenExternalSpools';
|
|
|
|
function readHiddenExternalSpools(): Record<string, boolean> {
|
|
try {
|
|
const saved = localStorage.getItem(HIDDEN_EXTERNAL_SPOOLS_KEY);
|
|
if (!saved) return {};
|
|
const parsed: unknown = JSON.parse(saved);
|
|
// Anything that isn't a plain object (an older format, or a value another
|
|
// tab mangled) is discarded rather than indexed into.
|
|
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
|
|
return parsed as Record<string, boolean>;
|
|
} catch {
|
|
// Malformed JSON, or localStorage unavailable (private mode / blocked
|
|
// cookies). Showing the external spool is the safe default either way.
|
|
return {};
|
|
}
|
|
}
|
|
|
|
/** Whether this printer's external spool should be left out of the card. */
|
|
export function isExternalSpoolHidden(printerId: number): boolean {
|
|
return readHiddenExternalSpools()[String(printerId)] === true;
|
|
}
|
|
|
|
/**
|
|
* Persist the toggle. Re-reads before writing so two cards toggled in the same
|
|
* session can't clobber each other's entry, and drops the key entirely when
|
|
* shown again so the stored object doesn't accumulate `false` for every printer
|
|
* the user ever toggled twice.
|
|
*/
|
|
export function setExternalSpoolHidden(printerId: number, hidden: boolean): void {
|
|
const next = readHiddenExternalSpools();
|
|
if (hidden) {
|
|
next[String(printerId)] = true;
|
|
} else {
|
|
delete next[String(printerId)];
|
|
}
|
|
try {
|
|
localStorage.setItem(HIDDEN_EXTERNAL_SPOOLS_KEY, JSON.stringify(next));
|
|
} catch {
|
|
// Quota exceeded or private mode — the toggle still applies for this
|
|
// session, it just won't survive a reload.
|
|
}
|
|
}
|