Panels become devices: per-device documents in the flow repo, a Devices tab

- flow/devices.py: DeviceDef in FLOWS_DIR/_devices/<id>.json, committed under
  the flow store's write lock, 409 on a stale version; nonces in DEVICES_FILE
  outside git; in-memory presence announced as device_status
- api/routes/devices.py: CRUD, pairing with an optional {name, board,
  firmware}, a pending list adopted without a code from the LAN only, unpair;
  /panels/pair, /panels/pair/{code} and GET /panels/{id} kept as aliases,
  PUT /panels/ gone
- _device_may replaces _panel_may, JWT claims unchanged
- panels.json moves into devices once at startup, then .migrated
- MCP list_devices, get_device; search category device
- Devices tab (list, detail with presence, pairing, ordered dashboards,
  touch, I/O), PanelsDialog removed, serialised optimistic saves
This commit is contained in:
Melvin Strobl
2026-10-16 15:48:36 +02:00
parent 3c0912a3fe
commit 28a565e501
56 changed files with 3539 additions and 2404 deletions
@@ -1,6 +1,6 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
import { createFileRoute, useNavigate } from "@tanstack/react-router"
import { LayoutDashboard, MonitorSmartphone } from "lucide-react"
import { LayoutDashboard } from "lucide-react"
import { useState } from "react"
import { DashboardsService } from "@/client"
@@ -12,7 +12,6 @@ import {
useDuplicateSelected,
usePublishAll,
} from "@/components/Common/OverviewToolbar"
import { PanelsDialog } from "@/components/Dashboard/PanelsDialog"
import {
dashboardKeys,
dashboardsQueryOptions,
@@ -28,25 +27,11 @@ import {
} from "@/components/ui/dialog"
import { Input } from "@/components/ui/input"
import { Skeleton } from "@/components/ui/skeleton"
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip"
import useCustomToast from "@/hooks/useCustomToast"
import { handleError } from "@/utils"
type Search = { panels?: boolean; panel?: string }
export const Route = createFileRoute("/_layout/dashboards/")({
component: Dashboards,
// The panels dialog has no route of its own, so the address is how anything
// else — the global search among them — arrives at it.
validateSearch: (search: Record<string, unknown>): Search => ({
panels: search.panels === true || search.panels === "true" || undefined,
// Which panel to scroll to, for a link that is about one of them.
panel: typeof search.panel === "string" ? search.panel : undefined,
}),
})
function Dashboards() {
@@ -58,11 +43,6 @@ function Dashboards() {
const [search, setSearch] = useState("")
const [dialogOpen, setDialogOpen] = useState(false)
const [deleteOpen, setDeleteOpen] = useState(false)
// Which screens exist is a question with an address, so anything can link to
// it — the global search lands a panel here.
const { panels: panelsOpen, panel: panelFocus } = Route.useSearch()
const setPanelsOpen = (open: boolean) =>
navigate({ to: "/dashboards", search: open ? { panels: true } : {} })
const create = useMutation({
mutationFn: (dashboard: string) =>
@@ -131,12 +111,6 @@ function Dashboards() {
</p>
</div>
{/* Its own root rather than a nested one: which screens exist is a
different question from which dashboards do. */}
<Dialog open={panelsOpen ?? false} onOpenChange={setPanelsOpen}>
<PanelsDialog focus={panelFocus} />
</Dialog>
<Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
<OverviewToolbar
search={search}
@@ -152,23 +126,7 @@ function Dashboards() {
onDeleteSelected={() => setDeleteOpen(true)}
duplicating={duplicate.isPending}
onDuplicateSelected={() => duplicate.mutate(selection.selected)}
>
<Tooltip>
<TooltipTrigger asChild>
<Button
variant="ghost"
size="icon-touch"
className="text-muted-foreground"
aria-label="Panels"
data-testid="open-panels"
onClick={() => setPanelsOpen(true)}
>
<MonitorSmartphone />
</Button>
</TooltipTrigger>
<TooltipContent>Panels</TooltipContent>
</Tooltip>
</OverviewToolbar>
/>
<DialogContent>
<form
className="grid gap-4"
@@ -0,0 +1,13 @@
import { createFileRoute } from "@tanstack/react-router"
import { DeviceDetail } from "@/components/Devices/DeviceDetail"
export const Route = createFileRoute("/_layout/devices/$id")({
component: Device,
head: ({ params }) => ({ meta: [{ title: `${params.id} - Fluksio` }] }),
})
function Device() {
const { id } = Route.useParams()
return <DeviceDetail id={id} />
}
@@ -0,0 +1,122 @@
import { useQuery, useQueryClient } from "@tanstack/react-query"
import { createFileRoute } from "@tanstack/react-router"
import { MonitorSmartphone } from "lucide-react"
import { useState } from "react"
import { DevicesService } from "@/client"
import { OverviewCard, useSelection } from "@/components/Common/OverviewCard"
import {
ConfirmDelete,
OverviewToolbar,
useDeleteSelected,
} from "@/components/Common/OverviewToolbar"
import { AddDeviceDialog } from "@/components/Devices/AddDeviceDialog"
import { presenceText } from "@/components/Devices/DeviceDetail"
import { deviceKeys, devicesQueryOptions } from "@/components/Devices/queries"
import { Dialog } from "@/components/ui/dialog"
import { Skeleton } from "@/components/ui/skeleton"
import useAuth from "@/hooks/useAuth"
export const Route = createFileRoute("/_layout/devices/")({
component: Devices,
head: () => ({ meta: [{ title: "Devices - Fluksio" }] }),
})
function Devices() {
const { data, isPending } = useQuery(devicesQueryOptions())
const queryClient = useQueryClient()
const { user } = useAuth()
const [search, setSearch] = useState("")
const [addOpen, setAddOpen] = useState(false)
const [deleteOpen, setDeleteOpen] = useState(false)
const needle = search.trim().toLowerCase()
const all = data?.data ?? []
const devices = all.filter((device) =>
`${device.id} ${device.title ?? ""}`.toLowerCase().includes(needle),
)
const selection = useSelection(devices.map((device) => device.id))
const remove = useDeleteSelected(
(id) => DevicesService.deleteDevice({ deviceId: id }),
"device",
() => queryClient.invalidateQueries({ queryKey: deviceKeys.all }),
)
return (
<div className="grid gap-6">
<div className="grid gap-1">
<h1 className="text-2xl">Devices</h1>
<p className="text-sm text-muted-foreground">
The screens and boards paired with this instance, and what each shows.
</p>
</div>
<Dialog open={addOpen} onOpenChange={setAddOpen}>
{/* Adding and removing devices is a superuser's, like pairing them. */}
{user?.is_superuser ? (
<OverviewToolbar
search={search}
onSearch={setSearch}
searchLabel="Search devices"
searchTestId="search-devices"
createLabel="Add device"
createTestId="add-device"
selectedCount={selection.selected.length}
onDeleteSelected={() => setDeleteOpen(true)}
/>
) : null}
<AddDeviceDialog
taken={all.map((device) => device.id)}
host={data?.frontend_host ?? ""}
/>
</Dialog>
<ConfirmDelete
open={deleteOpen}
onOpenChange={setDeleteOpen}
names={selection.selected}
noun="device"
pending={remove.isPending}
description="Its screen is signed out at once and asks for a new code. The dashboards it showed stay."
onConfirm={() => {
setDeleteOpen(false)
remove.mutate(selection.selected)
}}
/>
{isPending ? (
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
{Array.from({ length: 3 }).map((_, index) => (
<Skeleton key={index} className="h-[5.5rem] rounded-lg" />
))}
</div>
) : devices.length === 0 ? (
<p className="text-sm text-muted-foreground">
{needle
? "No device matches that."
: "No devices yet. Add one, then pair the screen with it."}
</p>
) : (
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
{devices.map((device) => {
const count = (device.dashboards ?? []).length
return (
<OverviewCard
key={device.id}
link={{ to: "/devices/$id", params: { id: device.id } }}
icon={MonitorSmartphone}
title={device.title || device.id}
online={device.online}
detail={`${presenceText(device)} · ${count} dashboard${count === 1 ? "" : "s"}`}
selecting={selection.selecting}
selected={selection.selected.includes(device.id)}
onToggle={() => selection.toggle(device.id)}
testId="device-card"
/>
)
})}
</div>
)}
</div>
)
}
+5 -6
View File
@@ -5,21 +5,20 @@ import { useCallback, useMemo, useState } 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 { dashboardQueryOptions } from "@/components/Dashboard/queries"
import {
LookProvider,
useCanvasRoot,
} from "@/components/Dashboard/ui/core/look"
import { deviceQueryOptions } from "@/components/Devices/queries"
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.
* What a paired device shows: the dashboards it was assigned. The address
* stays `/panel/{id}`, which is what screens already hanging were pointed at.
*
* 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
@@ -50,7 +49,7 @@ function PanelRoute() {
const navigate = Route.useNavigate()
useFlowSocket()
const { data: panel } = useQuery(panelQueryOptions(id))
const { data: panel } = useQuery(deviceQueryOptions(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.
+10 -10
View File
@@ -3,7 +3,7 @@ import { createFileRoute, redirect } from "@tanstack/react-router"
import { Check, Copy } from "lucide-react"
import { useEffect } from "react"
import { ApiError, PanelsService } from "@/client"
import { ApiError, DevicesService } from "@/client"
import { Button } from "@/components/ui/button"
import { useCopyToClipboard } from "@/hooks/useCopyToClipboard"
import { apiToken, appPath, isPortal, openPortalSession } from "@/lib/portal"
@@ -42,8 +42,9 @@ function pairedPanel(): string {
*
* A wall tablet cannot be asked to type an email address and a password, so it
* asks for a code instead and shows it. Somebody with an account types that
* code into the panels dialog to say which panel this device is, and the
* credential it mints arrives here on the next poll.
* code into the device's page in the Devices tab, or picks this screen from
* the ones waiting on their network, and the credential it mints arrives here
* on the next poll.
*
* Only once, though: a screen that already holds a credential goes straight to
* the panel it names, so a reboot comes back to the dashboard rather than
@@ -75,7 +76,7 @@ function PairPanel() {
// A code lives ten minutes. Asking for one is the mount, and asking again is
// what happens when this one is no longer recognised.
const start = useMutation({
mutationFn: () => PanelsService.startPairing(),
mutationFn: () => DevicesService.startPairing(),
// A screen hung while the link is down has nobody to tell, so it keeps
// asking rather than showing six dots until someone power-cycles it.
retry: true,
@@ -92,7 +93,7 @@ function PairPanel() {
const { data: status, error } = useQuery({
queryKey: ["pairing", started?.code],
queryFn: () =>
PanelsService.pollPairing({
DevicesService.pollPairing({
code: started?.code ?? "",
secret: started?.secret ?? "",
}),
@@ -111,9 +112,9 @@ function PairPanel() {
}, [error, request])
useEffect(() => {
if (!status?.access_token || !status.panel) return
const panel = status?.device ?? status?.panel
if (!status?.access_token || !panel) return
const token = status.access_token
const panel = status.panel
const land = () => {
// A full load rather than a route change: everything this page asked for
// was asked without a credential, and the socket has to dial again
@@ -137,9 +138,8 @@ function PairPanel() {
<div className="grid gap-2">
<h1 className="text-2xl">Pair this panel</h1>
<p className="text-sm text-muted-foreground">
On a computer, open <span className="font-medium">Dashboards</span>{" "}
→ <span className="font-medium">Panels</span>, pick which panel this
is, and enter this code.
On a computer, open <span className="font-medium">Devices</span>,
pick which device this is, and enter this code.
</p>
</div>