ssr systems

This commit is contained in:
henrygd
2025-09-01 22:06:16 -04:00
parent fadfc5d81d
commit 605e3f7e9d
9 changed files with 152 additions and 16 deletions

View File

@@ -10,7 +10,8 @@
globalThis.BESZEL = {
BASE_PATH: "%BASE_URL%",
HUB_VERSION: "{{V}}",
HUB_URL: "{{HUB_URL}}"
HUB_URL: "{{HUB_URL}}",
SYSTEMS: '{SYSTEMS}'
}
</script>
</head>

View File

@@ -12,6 +12,13 @@ export const pb = new PocketBase(basePath)
export const isAdmin = () => pb.authStore.record?.role === "admin"
export const isReadOnlyUser = () => pb.authStore.record?.role === "readonly"
export const updateCookieToken = () => {
console.log("setting token", pb.authStore.token)
document.cookie = `beszauth=${pb.authStore.token}; path=/; expires=${new Date(
Date.now() + 7 * 24 * 60 * 60 * 1000
).toString()}`
}
export const verifyAuth = () => {
pb.collection("users")
.authRefresh()

View File

@@ -141,7 +141,13 @@ export async function subscribe() {
}
/** Refresh all systems with latest data from the hub */
export async function refresh() {
export async function refresh(records: SystemRecord[] = []) {
if (records.length) {
for (const record of records) {
add(record)
}
return
}
try {
const records = await fetchSystems()
if (!records.length) {

View File

@@ -5,7 +5,7 @@ import ReactDOM from "react-dom/client"
import { ThemeProvider } from "./components/theme-provider.tsx"
import { DirectionProvider } from "@radix-ui/react-direction"
import { $authenticated, $publicKey, $copyContent, $direction } from "./lib/stores.ts"
import { pb, updateUserSettings } from "./lib/api.ts"
import { pb, updateUserSettings, updateCookieToken } from "./lib/api.ts"
import * as systemsManager from "./lib/systemsManager.ts"
import { useStore } from "@nanostores/react"
import { Toaster } from "./components/ui/toaster.tsx"
@@ -27,8 +27,10 @@ const App = memo(() => {
useEffect(() => {
// change auth store on auth change
updateCookieToken()
pb.authStore.onChange(() => {
$authenticated.set(pb.authStore.isValid)
updateCookieToken()
})
// get version / public key
pb.send("/api/beszel/getkey", {}).then((data) => {
@@ -36,11 +38,17 @@ const App = memo(() => {
})
// get user settings
updateUserSettings()
const startingSystems = globalThis.BESZEL.SYSTEMS
for (const system of startingSystems) {
// if (typeof system.info === "string") {
system.info = JSON.parse(system.info as unknown as string)
// }
}
// need to get system list before alerts
systemsManager.init()
systemsManager
// get current systems list
.refresh()
.refresh(startingSystems)
// subscribe to new system updates
.then(systemsManager.subscribe)
// get current alerts
@@ -51,6 +59,7 @@ const App = memo(() => {
// updateFavicon("favicon.svg")
alertManager.unsubscribe()
systemsManager.unsubscribe()
globalThis.BESZEL.SYSTEMS = []
}
}, [])

View File

@@ -7,6 +7,8 @@ declare global {
BASE_PATH: string
HUB_VERSION: string
HUB_URL: string
/** initial list of systems */
SYSTEMS: SystemRecord[]
}
}

View File

@@ -3,7 +3,6 @@ import path from "path"
import tailwindcss from "@tailwindcss/vite"
import react from "@vitejs/plugin-react-swc"
import { lingui } from "@lingui/vite-plugin"
import { version } from "./package.json"
export default defineConfig({
base: "./",
@@ -13,13 +12,6 @@ export default defineConfig({
}),
lingui(),
tailwindcss(),
{
name: "replace version in index.html during dev",
apply: "serve",
transformIndexHtml(html) {
return html.replace("{{V}}", version).replace("{{HUB_URL}}", "")
},
},
],
esbuild: {
legalComments: "external",