From 794db0ac6ab27d83d612a42d675981393146405e Mon Sep 17 00:00:00 2001 From: henrygd Date: Tue, 2 Sep 2025 19:12:39 -0400 Subject: [PATCH] make sure old names are removed in systemsbyname store --- beszel/site/src/lib/stores.ts | 2 +- beszel/site/src/lib/systemsManager.ts | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/beszel/site/src/lib/stores.ts b/beszel/site/src/lib/stores.ts index 985fbff..81a4cb6 100644 --- a/beszel/site/src/lib/stores.ts +++ b/beszel/site/src/lib/stores.ts @@ -17,7 +17,7 @@ export const $downSystems = map>({}) /** Map of paused systems by id */ export const $pausedSystems = map>({}) /** List of all system records */ -export const $systems: ReadableAtom = computed($allSystemsByName, Object.values) +export const $systems: ReadableAtom = computed($allSystemsById, Object.values) /** Map of alert records by system id and alert name */ export const $alerts = map({}) diff --git a/beszel/site/src/lib/systemsManager.ts b/beszel/site/src/lib/systemsManager.ts index c539200..4570c19 100644 --- a/beszel/site/src/lib/systemsManager.ts +++ b/beszel/site/src/lib/systemsManager.ts @@ -29,7 +29,7 @@ export function init() { initialized = true // sync system stores on change - $allSystemsByName.listen((newSystems, oldSystems, changedKey) => { + $allSystemsById.listen((newSystems, oldSystems, changedKey) => { const oldSystem = oldSystems[changedKey] const newSystem = newSystems[changedKey] @@ -59,6 +59,10 @@ export function init() { $pausedSystems.setKey(newSystem.id, newSystem) removeFromStore(newSystem, $upSystems) removeFromStore(newSystem, $downSystems) + } else if (newStatus === SystemStatus.Pending) { + removeFromStore(newSystem, $upSystems) + removeFromStore(newSystem, $downSystems) + removeFromStore(newSystem, $pausedSystems) } // run things that need to be done when systems change @@ -100,13 +104,22 @@ async function fetchSystems(): Promise { } } -// Store management functions /** Add system to both name and ID stores */ export function add(system: SystemRecord) { $allSystemsByName.setKey(system.name, system) $allSystemsById.setKey(system.id, system) } +/** Update system in stores */ +export function update(system: SystemRecord) { + // if name changed, make sure old name is removed from the name store + const oldName = $allSystemsById.get()[system.id]?.name + if (oldName !== system.name) { + $allSystemsByName.setKey(oldName, undefined as any) + } + add(system) +} + /** Remove system from stores */ export function remove(system: SystemRecord) { removeFromStore(system, $allSystemsByName) @@ -125,7 +138,7 @@ function removeFromStore(system: SystemRecord, store: PreinitializedMapStore void> = { create: add, - update: add, + update: update, delete: remove, }