Files
app/frontend/src/components/Flow/FlowNode.tsx
T
Melvin StroblandClaude Fable 5 8c82549cf6 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
2026-08-15 18:10:50 +02:00

156 lines
4.2 KiB
TypeScript

import { Handle, type NodeProps, Position } from "@xyflow/react"
import {
AlertCircle,
Braces,
Clock,
Code2,
Database,
Globe,
Radio,
} from "lucide-react"
import { memo } from "react"
import type { MessageSpec, NodeDef_Input } from "@/client"
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip"
import { cn } from "@/lib/utils"
import { portOf } from "./deriveEdges"
import { useNodeStatus } from "./liveStore"
const NODE_ICONS = {
python: Code2,
mqtt: Radio,
http: Globe,
influxdb: Database,
delay: Clock,
mlp: Braces,
} as const
const STATUS_STYLES = {
running: { dot: "bg-primary animate-pulse", label: "Running" },
success: { dot: "bg-status-success", label: "Last run succeeded" },
error: { dot: "bg-destructive", label: "Failed" },
} as const
export type FlowNodeData = {
definition: NodeDef_Input
flow: string
typeLabel: string
issues: number
issueText: string
[key: string]: unknown
}
/** Vertically distribute handles so several ports stay reachable. */
function handleOffset(index: number, total: number): string {
if (total <= 1) return "50%"
const span = 60
return `${50 - span / 2 + (span / (total - 1)) * index}%`
}
function PortHandles({
specs,
type,
position,
}: {
specs: MessageSpec[]
type: "source" | "target"
position: Position
}) {
return (
<>
{specs.map((spec, index) => {
const port = portOf(spec)
return (
<Handle
key={`${type}-${port}`}
id={port}
type={type}
position={position}
className={cn(
"!bg-card !border-muted-foreground/60",
!spec.name && "unbound",
)}
style={{ top: handleOffset(index, specs.length) }}
/>
)
})}
</>
)
}
function FlowNodeComponent({ data, selected }: NodeProps) {
const { definition, flow, typeLabel, issues, issueText } =
data as FlowNodeData
const live = useNodeStatus(`${flow}.${definition.id}`)
const Icon = NODE_ICONS[definition.type as keyof typeof NODE_ICONS] ?? Code2
const status = live?.status === "active" ? undefined : live?.status
const style = status
? STATUS_STYLES[status as keyof typeof STATUS_STYLES]
: undefined
return (
<div
className={cn(
"relative min-w-[168px] max-w-[220px] rounded-lg border border-border bg-card px-3 py-2.5 shadow-e1 transition-shadow",
selected && "border-primary shadow-e2",
)}
>
<PortHandles
specs={definition.requires ?? []}
type="target"
position={Position.Left}
/>
<div className="flex items-center gap-2.5">
<span className="flex size-7 shrink-0 items-center justify-center rounded-sm bg-primary/10 text-primary">
<Icon className="size-4" />
</span>
<span className="min-w-0 flex-1">
<span className="block truncate text-sm font-medium">
{definition.title || definition.id}
</span>
<span className="block truncate text-xs font-medium uppercase tracking-[0.5px] text-muted-foreground">
{typeLabel}
</span>
</span>
{style ? (
<Tooltip>
<TooltipTrigger asChild>
<span
role="img"
className={cn("size-2 shrink-0 rounded-full", style.dot)}
aria-label={style.label}
/>
</TooltipTrigger>
<TooltipContent>{live?.error ?? style.label}</TooltipContent>
</Tooltip>
) : null}
</div>
{issues > 0 ? (
<Tooltip>
<TooltipTrigger asChild>
<span className="absolute -right-1.5 -top-1.5 flex size-4 items-center justify-center rounded-full bg-destructive text-primary-foreground">
<AlertCircle className="size-3" />
</span>
</TooltipTrigger>
<TooltipContent className="max-w-xs">{issueText}</TooltipContent>
</Tooltip>
) : null}
<PortHandles
specs={definition.provides ?? []}
type="source"
position={Position.Right}
/>
</div>
)
}
export const FlowNode = memo(FlowNodeComponent)