web: extra check for valid system before adding (#1063)

This commit is contained in:
henrygd
2025-09-11 15:37:11 -04:00
parent bcdb4c92b5
commit efa37b2312

View File

@@ -1,3 +1,4 @@
/** biome-ignore-all lint/suspicious/noAssignInExpressions: it's fine :) */
import type { PreinitializedMapStore } from "nanostores" import type { PreinitializedMapStore } from "nanostores"
import { pb, verifyAuth } from "@/lib/api" import { pb, verifyAuth } from "@/lib/api"
import { import {
@@ -16,9 +17,10 @@ const COLLECTION = pb.collection<SystemRecord>("systems")
const FIELDS_DEFAULT = "id,name,host,port,info,status" const FIELDS_DEFAULT = "id,name,host,port,info,status"
/** Maximum system name length for display purposes */ /** Maximum system name length for display purposes */
const MAX_SYSTEM_NAME_LENGTH = 20 const MAX_SYSTEM_NAME_LENGTH = 22
let initialized = false let initialized = false
// biome-ignore lint/suspicious/noConfusingVoidType: typescript rocks
let unsub: (() => void) | undefined | void let unsub: (() => void) | undefined | void
/** Initialize the systems manager and set up listeners */ /** Initialize the systems manager and set up listeners */
@@ -104,20 +106,37 @@ async function fetchSystems(): Promise<SystemRecord[]> {
} }
} }
/** Makes sure the system has valid info object and throws if not */
function validateSystemInfo(system: SystemRecord) {
if (!("cpu" in system.info)) {
throw new Error(`${system.name} has no CPU info`)
}
}
/** Add system to both name and ID stores */ /** Add system to both name and ID stores */
export function add(system: SystemRecord) { export function add(system: SystemRecord) {
try {
validateSystemInfo(system)
$allSystemsByName.setKey(system.name, system) $allSystemsByName.setKey(system.name, system)
$allSystemsById.setKey(system.id, system) $allSystemsById.setKey(system.id, system)
} catch (error) {
console.error(error)
}
} }
/** Update system in stores */ /** Update system in stores */
export function update(system: SystemRecord) { export function update(system: SystemRecord) {
try {
validateSystemInfo(system)
// if name changed, make sure old name is removed from the name store // if name changed, make sure old name is removed from the name store
const oldName = $allSystemsById.get()[system.id]?.name const oldName = $allSystemsById.get()[system.id]?.name
if (oldName !== system.name) { if (oldName !== system.name) {
$allSystemsByName.setKey(oldName, undefined as any) $allSystemsByName.setKey(oldName, undefined as unknown as SystemRecord)
} }
add(system) add(system)
} catch (error) {
console.error(error)
}
} }
/** Remove system from stores */ /** Remove system from stores */
@@ -132,7 +151,7 @@ export function remove(system: SystemRecord) {
/** Remove system from specific store */ /** Remove system from specific store */
function removeFromStore(system: SystemRecord, store: PreinitializedMapStore<Record<string, SystemRecord>>) { function removeFromStore(system: SystemRecord, store: PreinitializedMapStore<Record<string, SystemRecord>>) {
const key = store === $allSystemsByName ? system.name : system.id const key = store === $allSystemsByName ? system.name : system.id
store.setKey(key, undefined as any) store.setKey(key, undefined as unknown as SystemRecord)
} }
/** Action functions for subscription */ /** Action functions for subscription */