refactor: add subscribe / unsubscribe to alertManager

This commit is contained in:
henrygd
2025-08-24 19:48:21 -04:00
parent b64318d9e8
commit 0638ff3c21
3 changed files with 34 additions and 13 deletions

View File

@@ -292,11 +292,11 @@ const SectionTable = memo(({ fingerprints = [] }: { fingerprints: FingerprintRec
<TableBody className="whitespace-pre"> <TableBody className="whitespace-pre">
{fingerprints.map((fingerprint, i) => ( {fingerprints.map((fingerprint, i) => (
<TableRow key={i}> <TableRow key={i}>
<TableCell className="font-medium ps-5 py-2.5">{fingerprint.expand.system.name}</TableCell> <TableCell className="font-medium ps-5 py-2">{fingerprint.expand.system.name}</TableCell>
<TableCell className="font-mono text-[0.95em] py-2.5">{fingerprint.token}</TableCell> <TableCell className="font-mono text-[0.95em] py-2">{fingerprint.token}</TableCell>
<TableCell className="font-mono text-[0.95em] py-2.5">{fingerprint.fingerprint}</TableCell> <TableCell className="font-mono text-[0.95em] py-2">{fingerprint.fingerprint}</TableCell>
{!isReadOnly && ( {!isReadOnly && (
<TableCell className="py-2.5 px-4 xl:px-2"> <TableCell className="py-2 px-4 xl:px-2">
<ActionsButtonTable fingerprint={fingerprint} /> <ActionsButtonTable fingerprint={fingerprint} />
</TableCell> </TableCell>
)} )}

View File

@@ -537,6 +537,7 @@ export const getSystemNameFromId = (() => {
/** Helper to manage user alerts */ /** Helper to manage user alerts */
export const alertManager = (() => { export const alertManager = (() => {
const collection = pb.collection<AlertRecord>("alerts") const collection = pb.collection<AlertRecord>("alerts")
let unsub: () => void
/** Fields to fetch from alerts collection */ /** Fields to fetch from alerts collection */
const fields = "id,name,system,value,min,triggered" const fields = "id,name,system,value,min,triggered"
@@ -597,17 +598,29 @@ export const alertManager = (() => {
} }
})() })()
collection.subscribe("*", batchUpdate, { fields }) async function subscribe() {
unsub = await collection.subscribe("*", batchUpdate, { fields })
}
function unsubscribe() {
unsub?.()
}
async function refresh() {
const records = await fetchAlerts()
add(records)
}
return { return {
/** Add alerts to store */ /** Add alerts to store */
add, add,
/** Remove alerts from store */ /** Remove alerts from store */
remove, remove,
/** Subscribe to alerts */
subscribe,
/** Unsubscribe from alerts */
unsubscribe,
/** Refresh alerts with latest data from hub */ /** Refresh alerts with latest data from hub */
async refresh() { refresh,
const records = await fetchAlerts()
add(records)
},
} }
})() })()

View File

@@ -38,10 +38,17 @@ const App = memo(() => {
}) })
// get servers / alerts / settings // get servers / alerts / settings
updateUserSettings() updateUserSettings()
// get alerts after system list is loaded // need to get system list before alerts
updateSystemList().then(alertManager.refresh) updateSystemList()
// get alerts
.then(alertManager.refresh)
// subscribe to new alert updates
.then(alertManager.subscribe)
return () => updateFavicon("favicon.svg") return () => {
updateFavicon("favicon.svg")
alertManager.unsubscribe()
}
}, []) }, [])
// update favicon // update favicon
@@ -54,7 +61,8 @@ const App = memo(() => {
if (system.status === SystemStatus.Down) { if (system.status === SystemStatus.Down) {
updateFavicon("favicon-red.svg") updateFavicon("favicon-red.svg")
return return
} else if (system.status === SystemStatus.Up) { }
if (system.status === SystemStatus.Up) {
up = true up = true
} }
} }