import { Suspense, lazy, memo, useEffect, useMemo } from "react" import { Card, CardContent, CardHeader, CardTitle } from "../ui/card" import { $alerts, $systems, pb } from "@/lib/stores" import { useStore } from "@nanostores/react" import { GithubIcon } from "lucide-react" import { Separator } from "../ui/separator" import { alertInfo, updateRecordList, updateSystemList } from "@/lib/utils" import { AlertRecord, SystemRecord } from "@/types" import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert" import { $router, Link } from "../router" import { Plural, Trans, useLingui } from "@lingui/react/macro" import { getPagePath } from "@nanostores/router" const SystemsTable = lazy(() => import("../systems-table/systems-table")) export const Home = memo(() => { const alerts = useStore($alerts) const systems = useStore($systems) const { t } = useLingui() let alertsKey = "" const activeAlerts = useMemo(() => { const activeAlerts = alerts.filter((alert) => { const active = alert.triggered && alert.name in alertInfo if (!active) { return false } alert.sysname = systems.find((system) => system.id === alert.system)?.name alertsKey += alert.id return true }) return activeAlerts }, [systems, alerts]) useEffect(() => { document.title = t`Dashboard` + " / Beszel" }, [t]) useEffect(() => { // make sure we have the latest list of systems updateSystemList() // subscribe to real time updates for systems / alerts pb.collection("systems").subscribe("*", (e) => { updateRecordList(e, $systems) }) pb.collection("alerts").subscribe("*", (e) => { updateRecordList(e, $alerts) }) return () => { pb.collection("systems").unsubscribe("*") // pb.collection('alerts').unsubscribe('*') } }, []) return useMemo( () => ( <> {/* show active alerts */} {activeAlerts.length > 0 && }
GitHub Beszel {globalThis.BESZEL.HUB_VERSION}
), [alertsKey] ) }) const ActiveAlerts = memo(({ activeAlerts }: { activeAlerts: AlertRecord[] }) => { return (
Active Alerts
{activeAlerts.length > 0 && (
{activeAlerts.map((alert) => { const info = alertInfo[alert.name as keyof typeof alertInfo] return ( {alert.sysname} {info.name().toLowerCase().replace("cpu", "CPU")} Exceeds {alert.value} {info.unit} in last ) })}
)}
) })