Dashboard: run unchanged when a portal serves it

The same bundle is served by a portal under /i/{id}, so it reads its API base,
credential and router basepath from an injected config instead of the build-time
URL and localStorage. A normal installation finds no config and behaves exactly
as before; the credential deliberately never touches localStorage, since two
installations open in one browser share an origin and would overwrite each
other's session.

The websocket URL was resolving an absolute path against the base, which
discards the base's own path — harmless until the base gained one, then it
aimed the socket at the wrong host entirely.

Connection state gets a store of its own, apart from the engine's: the proxy's
503 carries {offline, last_seen}, which raises a banner naming when the
installation was last heard from and turns a failed mutation into 'not
delivered' rather than a generic error. The screen keeps its last data
underneath, since stale readings with a timestamp beat a blank page. A
reconnecting socket invalidates every query, because whatever happened while it
was down was missed.

Verified in a browser against a real hub and installation: the full UI loads
through the tunnel with no console errors, and killing the installation raises
the banner within a poll.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XtBzdDyLsmDaF1W7DLYtYM
This commit is contained in:
2026-08-19 17:09:53 +02:00
co-authored by Claude Opus 5
parent f4b81507d1
commit 32d8b42682
11 changed files with 254 additions and 12 deletions
+16 -2
View File
@@ -2,6 +2,8 @@ import { useQueryClient } from "@tanstack/react-query"
import { useEffect, useRef } from "react"
import { OpenAPI } from "@/client"
import { connectionStore } from "@/lib/connectionStore"
import { apiToken } from "@/lib/portal"
import { type LogLine, liveStore, type ValueSource } from "./liveStore"
import { flowKeys } from "./queries"
@@ -77,11 +79,14 @@ type FlowEvent =
function socketUrl(): string {
const base = String(OpenAPI.BASE || window.location.origin)
const url = new URL("/api/v1/flows/ws", base)
// Concatenated rather than resolved: an absolute path as the second argument
// to `new URL` discards the base's own path, which under a portal
// (`https://host/i/{id}`) would aim the socket at the wrong place entirely.
const url = new URL(`${base.replace(/\/$/, "")}/api/v1/flows/ws`)
url.protocol = url.protocol === "https:" ? "wss:" : "ws:"
// Browsers cannot set headers on a websocket handshake, so the token rides
// in the query string.
url.searchParams.set("token", localStorage.getItem("access_token") ?? "")
url.searchParams.set("token", apiToken())
return url.toString()
}
@@ -117,6 +122,10 @@ export function useFlowSocket(onAuthFailure?: () => void): void {
ws.onopen = () => {
retry.current = RECONNECT_MIN
liveStore.setConnected(true)
connectionStore.setOnline()
// Whatever happened while the socket was down was missed, so nothing
// held in cache can be trusted to still be current.
queryClient.invalidateQueries()
}
ws.onmessage = (event) => {
@@ -215,6 +224,11 @@ export function useFlowSocket(onAuthFailure?: () => void): void {
onAuthFailure?.()
return
}
// 1013 is the portal saying the installation is not attached — the one
// close code that means "offline" rather than "the socket dropped".
if (event.code === 1013) {
connectionStore.setOffline(null)
}
timer.current = setTimeout(connect, retry.current)
retry.current = Math.min(retry.current * 2, RECONNECT_MAX)
}