Files
app/frontend/src/routes/panel.$id.tsx
T
Melvin StroblandClaude Opus 5.5 603635f947
Docs / docs (push) Successful in 24s
Playwright Tests / test-playwright (1, 2) (push) Failing after 4m14s
Playwright Tests / test-playwright (2, 2) (push) Successful in 2m11s
pre-commit / pre-commit (push) Failing after 2m2s
Test Backend / test-backend (push) Failing after 3m23s
Compose Smoke Test / test-compose (push) Successful in 34s
Playwright Tests / merge-reports (push) Successful in 1m33s
Round paging follows the finger: faces and dashboards on a track, swiped one to one with the next beside it, turned or sent back on release, rubber-banded at the ends; keys and dots slide the same track; the reading dot a smaller companion of the handle
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 17:08:51 +02:00

158 lines
5.9 KiB
TypeScript

import { keepPreviousData, useQuery } from "@tanstack/react-query"
import { createFileRoute, redirect } from "@tanstack/react-router"
import { useCallback, useMemo } from "react"
import type { Dashboard } from "@/components/Dashboard/DashboardView"
import { PanelRail } from "@/components/Dashboard/PanelRail"
import { PanelSurface } from "@/components/Dashboard/PanelSurface"
import {
dashboardQueryOptions,
panelQueryOptions,
} from "@/components/Dashboard/queries"
import {
LookProvider,
useCanvasRoot,
} from "@/components/Dashboard/ui/core/look"
import { useFlowSocket } from "@/components/Flow/useFlowSocket"
import { isLoggedIn } from "@/hooks/useAuth"
import { useIsMobile } from "@/hooks/useMobile"
import { cn } from "@/lib/utils"
/**
* What a paired device shows: the dashboards this panel was assigned.
*
* The same full-bleed surface as `/view/{name}`, with a rail down the left
* (along the bottom on a phone) when there is more than one to switch between — so a hallway tablet and a
* workshop tablet can carry different sets without either dashboard knowing
* anything about the other.
*
* Which one is open lives in the URL, so a panel that reboots comes back where
* it was rather than at the first one. A round dashboard has no room for a
* rail: its panel is swiped up and down between them instead.
*/
export const Route = createFileRoute("/panel/$id")({
component: PanelRoute,
validateSearch: (search: Record<string, unknown>): { d?: string } =>
typeof search.d === "string" ? { d: search.d } : {},
beforeLoad: async () => {
// Unpaired, or a credential the server stopped honouring. Either way this
// device needs a new code, not a login form it cannot type into.
if (!isLoggedIn()) {
throw redirect({ to: "/panel" })
}
},
head: ({ params }) => ({ meta: [{ title: `${params.id} - Fluksio` }] }),
})
function PanelRoute() {
const { id } = Route.useParams()
const { d } = Route.useSearch()
const navigate = Route.useNavigate()
useFlowSocket()
const { data: panel } = useQuery(panelQueryOptions(id))
const dashboards = panel?.dashboards ?? []
// A name in the URL that this panel no longer carries falls back to the
// first, which is what a dashboard removed from under a running panel does.
const current = d && dashboards.includes(d) ? d : dashboards[0]
const { data: dashboard } = useQuery({
...dashboardQueryOptions(current ?? ""),
enabled: Boolean(current),
// The one showing stays until the next has loaded, rather than the
// screen going blank between them.
placeholderData: keepPreviousData,
})
const stacked = useIsMobile()
const rail = dashboards.length > 1
const round = dashboard?.shape === "round"
// The ones either side, which a round one is swiped to and shows the
// names of on the way. Stable, so the canvas is not rebuilt for it.
const at = dashboards.indexOf(current ?? "")
const before = dashboards[at - 1]
const after = dashboards[at + 1]
const around = useMemo(
(): [string | undefined, string | undefined] => [before, after],
[before, after],
)
// Stops at either end, as the faces of one do.
const step = useCallback(
(by: number) => {
const all = panel?.dashboards ?? []
const next = all[all.indexOf(current ?? "") + by]
if (next) navigate({ search: { d: next } })
},
[panel, current, navigate],
)
// Said by the panel rather than by the dashboard: this is the screen, and
// whether it is touched or pointed at is a fact about the screen. A document
// hanging on a hallway tablet and on a desk browser is one document either
// way, and only one of the two has fingers on it.
const touch = panel?.touch === true
// The screen is the dashboard here, so its appearance covers the whole of
// it — the rail and the letterbox around a scaled canvas included. This is
// the surface these settings exist for: a panel in a room has no other way
// to be told what to wear.
const root = useCanvasRoot(dashboard as Dashboard | undefined, touch)
if (panel && dashboards.length === 0) {
return (
<main className="grid h-svh w-full place-items-center p-8">
<p className="text-sm text-muted-foreground">
No dashboards are assigned to this panel yet.
</p>
</main>
)
}
return (
<main
data-look={root["data-look"]}
data-touch={root["data-touch"]}
data-palette={root["data-palette"]}
className={cn(
"relative h-svh w-full bg-background text-foreground",
root.className,
// Padding only when stacked. A canvas is already held off its own
// edges by the grid inset, and a second margin outside it costs the
// scale: a dashboard drawn for this screen would come out at 95% of
// the screen it was drawn for.
// A round one fills the screen as it does a knob's, stacked or not.
stacked && !round ? "overflow-y-auto p-4" : "overflow-hidden",
)}
style={root.style}
>
<LookProvider
dashboard={dashboard as Dashboard | undefined}
touch={touch}
>
{dashboard ? (
<PanelSurface
dashboard={dashboard as Dashboard}
stacked={stacked}
touch={touch}
onDashboard={rail && round ? step : undefined}
around={rail && round ? around : undefined}
// Drawn on the panel rather than beside it: a screen showing four
// dashboards shows the way between them too.
rail={
rail && current && !round ? (
<PanelRail
dashboards={dashboards}
current={current}
horizontal={stacked}
linkFor={(name) => ({
to: "/panel/$id",
params: { id },
search: { d: name },
})}
/>
) : null
}
/>
) : null}
</LookProvider>
</main>
)
}