alert updates

This commit is contained in:
Henry Dollman
2024-07-15 15:49:00 -04:00
parent f1819e59b9
commit 6696e1c749
9 changed files with 224 additions and 115 deletions

View File

@@ -1,8 +1,9 @@
import PocketBase from 'pocketbase'
import { atom } from 'nanostores'
import { SystemRecord } from '@/types'
import { AlertRecord, SystemRecord } from '@/types'
import { createRouter } from '@nanostores/router'
/** PocketBase JS Client */
export const pb = new PocketBase('/')
export const $router = createRouter(
@@ -13,12 +14,19 @@ export const $router = createRouter(
{ links: false }
)
/** Navigate to url using router */
export const navigate = (urlString: string) => {
$router.open(urlString)
}
/** Store if user is authenticated */
export const $authenticated = atom(pb.authStore.isValid)
export const $servers = atom([] as SystemRecord[])
/** List of system records */
export const $systems = atom([] as SystemRecord[])
/** List of alert records */
export const $alerts = atom([] as AlertRecord[])
/** SSH public key */
export const $publicKey = atom('')

View File

@@ -1,8 +1,10 @@
import { toast } from '@/components/ui/use-toast'
import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
import { $servers, pb } from './stores'
import { SystemRecord } from '@/types'
import { $alerts, $systems, pb } from './stores'
import { AlertRecord, SystemRecord } from '@/types'
import { RecordModel, RecordSubscription } from 'pocketbase'
import { WritableAtom } from 'nanostores'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
@@ -28,7 +30,15 @@ export const updateServerList = () => {
pb.collection<SystemRecord>('systems')
.getFullList({ sort: '+name' })
.then((records) => {
$servers.set(records)
$systems.set(records)
})
}
export const updateAlerts = () => {
pb.collection('alerts')
.getFullList<AlertRecord>({ fields: 'id,name,system' })
.then((records) => {
$alerts.set(records)
})
}
@@ -56,3 +66,33 @@ export const updateFavicon = (newIconUrl: string) =>
((document.querySelector("link[rel='icon']") as HTMLLinkElement).href = newIconUrl)
export const isAdmin = () => pb.authStore.model?.admin
/** Update systems / alerts list when records change */
export function updateRecordList<T extends RecordModel>(
e: RecordSubscription<T>,
$store: WritableAtom<T[]>
) {
const curRecords = $store.get()
const newRecords = []
// console.log('e', e)
if (e.action === 'delete') {
for (const server of curRecords) {
if (server.id !== e.record.id) {
newRecords.push(server)
}
}
} else {
let found = 0
for (const server of curRecords) {
if (server.id === e.record.id) {
found = newRecords.push(e.record)
} else {
newRecords.push(server)
}
}
if (!found) {
newRecords.push(e.record)
}
}
$store.set(newRecords)
}