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
102 lines
3.0 KiB
TypeScript
102 lines
3.0 KiB
TypeScript
import {
|
|
type UseMutationResult,
|
|
useMutation,
|
|
useQueryClient,
|
|
} from "@tanstack/react-query"
|
|
import { useCallback, useEffect, useRef } from "react"
|
|
|
|
import { type FlowDef_Input, FlowsService } from "@/client"
|
|
|
|
export const flowKeys = {
|
|
all: ["flows"] as const,
|
|
detail: (name: string) => ["flows", name] as const,
|
|
source: (name: string, nodeId: string) =>
|
|
["flows", name, "source", nodeId] as const,
|
|
nodeTypes: ["flows", "node-types"] as const,
|
|
}
|
|
|
|
export const flowsQueryOptions = () => ({
|
|
queryKey: flowKeys.all,
|
|
queryFn: () => FlowsService.readFlows(),
|
|
})
|
|
|
|
export const flowQueryOptions = (name: string) => ({
|
|
queryKey: flowKeys.detail(name),
|
|
queryFn: () => FlowsService.readFlow({ name }),
|
|
})
|
|
|
|
export const nodeTypesQueryOptions = () => ({
|
|
queryKey: flowKeys.nodeTypes,
|
|
queryFn: () => FlowsService.readNodeTypes(),
|
|
staleTime: Number.POSITIVE_INFINITY,
|
|
})
|
|
|
|
export const nodeSourceQueryOptions = (name: string, nodeId: string) => ({
|
|
queryKey: flowKeys.source(name, nodeId),
|
|
queryFn: () => FlowsService.readNodeSource({ name, nodeId }),
|
|
})
|
|
|
|
const AUTOSAVE_DELAY = 800
|
|
|
|
/**
|
|
* Saves the flow a moment after the last edit, and immediately when the editor
|
|
* needs the server to be current (closing a panel, switching flow, running).
|
|
*
|
|
* Identical documents are skipped server-side, so a quiet canvas writes nothing.
|
|
*/
|
|
export function useAutosave(name: string): {
|
|
save: (definition: FlowDef_Input) => void
|
|
flush: () => void
|
|
mutation: UseMutationResult<unknown, unknown, FlowDef_Input, unknown>
|
|
} {
|
|
const queryClient = useQueryClient()
|
|
const pending = useRef<FlowDef_Input | null>(null)
|
|
const timer = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
|
|
const mutation = useMutation({
|
|
mutationFn: (definition: FlowDef_Input) =>
|
|
FlowsService.saveFlow({ name, requestBody: definition }),
|
|
onSuccess: (detail) => {
|
|
// Write the server's answer straight into the cache: invalidating would
|
|
// pull the document back out from under edits still in flight.
|
|
queryClient.setQueryData(flowKeys.detail(name), detail)
|
|
queryClient.invalidateQueries({ queryKey: flowKeys.all, exact: true })
|
|
},
|
|
})
|
|
|
|
const flush = useCallback(() => {
|
|
if (timer.current) {
|
|
clearTimeout(timer.current)
|
|
timer.current = null
|
|
}
|
|
const definition = pending.current
|
|
pending.current = null
|
|
if (definition) {
|
|
mutation.mutate(definition)
|
|
}
|
|
}, [mutation])
|
|
|
|
const save = useCallback(
|
|
(definition: FlowDef_Input) => {
|
|
pending.current = definition
|
|
if (timer.current) clearTimeout(timer.current)
|
|
timer.current = setTimeout(flush, AUTOSAVE_DELAY)
|
|
},
|
|
[flush],
|
|
)
|
|
|
|
// Leaving the tab is the last chance to persist what is still queued.
|
|
useEffect(() => {
|
|
const onHidden = () => {
|
|
if (document.visibilityState === "hidden") flush()
|
|
}
|
|
document.addEventListener("visibilitychange", onHidden)
|
|
return () => {
|
|
document.removeEventListener("visibilitychange", onHidden)
|
|
if (timer.current) clearTimeout(timer.current)
|
|
}
|
|
}, [flush])
|
|
|
|
return { save, flush, mutation }
|
|
}
|