Add the flow editor: canvas, node panel and live values

The browser half of M3. Flows open on a full-bleed canvas with their chrome
floating over it: flow tabs top, dock bottom, node settings in a panel on the
right that leaves the graph visible and running behind it.

- Connections are derived, not stored. A node declares the messages it reads
  and publishes; every matching pair draws an edge, so two producers of one
  message converge on their consumer. Dragging output to input is shorthand
  for pointing that input at the producer's message, and asks before it
  replaces an existing one.
- Values land on the edges as they flow, over a websocket that feeds a store
  outside React, so a value arriving re-renders its own chip and nothing else.
  Clicking an edge shows the last payload and when it arrived.
- Node source is edited in Monaco, loaded only when a panel opens and themed
  from the design tokens.
- Edits autosave; identical documents are skipped server-side, so a quiet
  canvas writes nothing.
- Validation from the API shows on the node it belongs to and is summarised in
  the dock, where each entry pans to its node.
- Works on a phone: touch-connect, 44px dock targets, and the node panel
  becomes a full-screen sheet.

Two new tokens (--status-success, --font-mono) are mirrored in the website repo
and recorded in DESIGN-GUIDELINES.md.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016WzrvW7rjQbynnhF6pxh6i
This commit is contained in:
Melvin Strobl
2026-08-15 18:10:50 +02:00
co-authored by Claude Fable 5
parent 06a4506767
commit 8c82549cf6
40 changed files with 5027 additions and 66 deletions
+409
View File
@@ -0,0 +1,409 @@
import { useQuery } from "@tanstack/react-query"
import { X } from "lucide-react"
import { AnimatePresence, motion } from "motion/react"
import { lazy, Suspense, useEffect, useRef, useState } from "react"
import type { DType, MessageSpec, NodeDef_Input, NodeTypeInfo } from "@/client"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import { Sheet, SheetContent, SheetTitle } from "@/components/ui/sheet"
import { Switch } from "@/components/ui/switch"
import { useIsMobile } from "@/hooks/useMobile"
import { duration, easeEmphasized, easeStandard } from "@/lib/motion"
import { nodeSourceQueryOptions } from "./queries"
const NodeEditor = lazy(() => import("./NodeEditor"))
const DTYPES: DType[] = ["float", "int", "str", "bool", "json"]
const SECTION =
"text-xs font-medium uppercase tracking-[0.5px] text-muted-foreground"
/** Same grammar as the shared `slideUp`, on the axis this panel travels. */
const panelSlide = {
hidden: { opacity: 0, x: 16 },
visible: {
opacity: 1,
x: 0,
transition: { duration: duration.base, ease: easeEmphasized },
},
exit: {
opacity: 0,
x: 16,
transition: { duration: duration.fast, ease: easeStandard },
},
}
function PortList({
title,
specs,
flow,
emptyHint,
onChange,
}: {
title: string
specs: MessageSpec[]
flow: string
emptyHint: string
onChange: (next: MessageSpec[]) => void
}) {
const update = (index: number, patch: Partial<MessageSpec>) => {
const next = specs.map((spec, i) =>
i === index ? { ...spec, ...patch } : spec,
)
onChange(next)
}
return (
<div className="grid gap-2">
<div className="flex items-center justify-between">
<span className={SECTION}>{title}</span>
<Button
variant="ghost"
size="sm"
className="h-7 text-xs text-muted-foreground"
onClick={() => onChange([...specs, { name: "", dtype: "float" }])}
>
Add
</Button>
</div>
{specs.length === 0 ? (
<p className="text-sm text-muted-foreground">{emptyHint}</p>
) : null}
{specs.map((spec, index) => (
<div key={`port-${index}`} className="flex items-center gap-1.5">
<Input
value={spec.name ?? ""}
placeholder={`name in ${flow}`}
aria-label="Message name"
className="h-8 flex-1 font-mono text-sm"
onChange={(event) =>
update(index, { name: event.target.value, port: "" })
}
/>
<Select
value={spec.dtype ?? "float"}
onValueChange={(value) => update(index, { dtype: value as DType })}
>
<SelectTrigger className="!h-8 w-[92px] text-sm" aria-label="Type">
<SelectValue />
</SelectTrigger>
<SelectContent>
{DTYPES.map((dtype) => (
<SelectItem key={dtype} value={dtype}>
{dtype}
</SelectItem>
))}
</SelectContent>
</Select>
<Button
variant="ghost"
size="icon-sm"
className="text-muted-foreground"
aria-label="Remove port"
onClick={() => onChange(specs.filter((_, i) => i !== index))}
>
<X />
</Button>
</div>
))}
</div>
)
}
/** A small form built from the node type's declared parameters. */
function ParamsForm({
schema,
params,
onChange,
}: {
schema: Record<string, unknown> | undefined
params: Record<string, unknown>
onChange: (next: Record<string, unknown>) => void
}) {
const properties = (schema?.properties ?? {}) as Record<
string,
{ type?: string; title?: string; default?: unknown }
>
const entries = Object.entries(properties)
if (entries.length === 0) return null
const set = (key: string, value: unknown) =>
onChange({ ...params, [key]: value })
return (
<div className="grid gap-3">
<span className={SECTION}>Settings</span>
{entries.map(([key, property]) => {
const value = params[key] ?? property.default ?? ""
const label = property.title ?? key
if (property.type === "boolean") {
return (
<div key={key} className="flex items-center justify-between gap-2">
<Label htmlFor={`param-${key}`} className="text-sm font-normal">
{label}
</Label>
<Switch
id={`param-${key}`}
checked={Boolean(value)}
onCheckedChange={(checked) => set(key, checked)}
/>
</div>
)
}
if (property.type === "object" || property.type === "array") {
return null
}
const numeric =
property.type === "integer" || property.type === "number"
return (
<div key={key} className="grid gap-1.5">
<Label htmlFor={`param-${key}`} className="text-sm font-normal">
{label}
</Label>
<Input
id={`param-${key}`}
className="h-8 text-sm"
type={numeric ? "number" : "text"}
value={String(value)}
onChange={(event) =>
set(
key,
numeric ? Number(event.target.value) : event.target.value,
)
}
/>
</div>
)
})}
</div>
)
}
function PanelBody({
node,
flow,
nodeType,
onChange,
onSaveSource,
onClose,
onDelete,
}: {
node: NodeDef_Input
flow: string
nodeType: NodeTypeInfo | undefined
onChange: (next: NodeDef_Input) => void
onSaveSource: (code: string) => void
onClose: () => void
onDelete: () => void
}) {
const hasSource = nodeType?.has_source ?? node.type === "python"
const { data: source } = useQuery({
...nodeSourceQueryOptions(flow, node.id),
enabled: hasSource,
})
const [code, setCode] = useState<string | null>(null)
const timer = useRef<ReturnType<typeof setTimeout> | null>(null)
const pending = useRef<string | null>(null)
const save = useRef(onSaveSource)
save.current = onSaveSource
const editCode = (next: string) => {
setCode(next)
pending.current = next
if (timer.current) clearTimeout(timer.current)
timer.current = setTimeout(() => {
timer.current = null
save.current(next)
}, 1000)
}
// Closing the panel must not lose the last keystrokes.
useEffect(() => {
return () => {
if (timer.current) {
clearTimeout(timer.current)
if (pending.current !== null) save.current(pending.current)
}
}
}, [])
return (
<>
<div className="flex shrink-0 items-center gap-2 border-b border-border px-4 py-3">
<Input
value={node.title || node.id}
aria-label="Node name"
className="h-8 flex-1 text-sm font-medium"
onChange={(event) => onChange({ ...node, title: event.target.value })}
/>
<Button
variant="ghost"
size="icon-sm"
className="text-muted-foreground"
onClick={onClose}
aria-label="Close"
>
<X />
</Button>
</div>
<div className="flex min-h-0 flex-1 flex-col overflow-y-auto">
<div className="grid gap-5 p-4">
<PortList
title="Consumes"
specs={node.requires ?? []}
flow={flow}
emptyHint="Nothing yet. Add a message this node reads."
onChange={(requires) => onChange({ ...node, requires })}
/>
<PortList
title="Provides"
specs={node.provides ?? []}
flow={flow}
emptyHint="Nothing yet. Add a message this node publishes."
onChange={(provides) => onChange({ ...node, provides })}
/>
<ParamsForm
schema={nodeType?.params_schema}
params={node.params ?? {}}
onChange={(params) => onChange({ ...node, params })}
/>
</div>
{hasSource ? (
<div className="flex min-h-[280px] flex-1 flex-col gap-2 px-4 pb-4">
<span className={SECTION}>Code</span>
<div className="min-h-0 flex-1 overflow-hidden rounded-md border border-border">
<Suspense
fallback={
<div className="h-full w-full animate-pulse bg-muted" />
}
>
<NodeEditor
value={code ?? source?.code ?? ""}
onChange={editCode}
/>
</Suspense>
</div>
</div>
) : null}
</div>
<div className="shrink-0 border-t border-border px-4 py-3">
<Button
variant="ghost"
size="sm"
className="text-destructive hover:text-destructive"
onClick={onDelete}
>
Delete node
</Button>
</div>
</>
)
}
/**
* Node settings, floating over the canvas so the graph stays visible and live.
* On a phone there is no room for that, so it becomes a full-screen sheet.
*/
export function NodePanel({
node,
flow,
nodeTypes,
onChange,
onSaveSource,
onClose,
onDelete,
}: {
node: NodeDef_Input | null
flow: string
nodeTypes: NodeTypeInfo[]
onChange: (next: NodeDef_Input) => void
onSaveSource: (code: string) => void
onClose: () => void
onDelete: () => void
}) {
const isMobile = useIsMobile()
const nodeType = nodeTypes.find((entry) => entry.type === node?.type)
useEffect(() => {
if (!node) return
const onKey = (event: KeyboardEvent) => {
if (event.key === "Escape") onClose()
}
window.addEventListener("keydown", onKey)
return () => window.removeEventListener("keydown", onKey)
}, [node, onClose])
if (isMobile) {
return (
<Sheet open={Boolean(node)} onOpenChange={(open) => !open && onClose()}>
<SheetContent
side="right"
// The panel header carries its own close button, and opening should
// not drop the caret into the node's name.
className="flex h-dvh w-full max-w-none flex-col gap-0 rounded-none p-0 [&>button:last-of-type]:hidden"
onOpenAutoFocus={(event) => event.preventDefault()}
>
<SheetTitle className="sr-only">Node settings</SheetTitle>
{node ? (
<PanelBody
key={node.id}
node={node}
flow={flow}
nodeType={nodeType}
onChange={onChange}
onSaveSource={onSaveSource}
onClose={onClose}
onDelete={onDelete}
/>
) : null}
</SheetContent>
</Sheet>
)
}
return (
<AnimatePresence>
{node ? (
<motion.aside
key={node.id}
variants={panelSlide}
initial="hidden"
animate="visible"
exit="exit"
role="complementary"
aria-label="Node settings"
data-testid="node-panel"
className="pointer-events-auto absolute inset-y-4 right-4 z-10 flex w-[400px] flex-col overflow-hidden rounded-lg border border-border bg-card/80 shadow-e2 backdrop-blur-md"
>
<PanelBody
node={node}
flow={flow}
nodeType={nodeType}
onChange={onChange}
onSaveSource={onSaveSource}
onClose={onClose}
onDelete={onDelete}
/>
</motion.aside>
) : null}
</AnimatePresence>
)
}