/** * `NodeGraph/NodeGraph` — the whole editor (design §12.1). * * Every story mounts the same `demoGraphSchema()` the two task-1.11 specs * mount, so a story that renders and a spec that passes are looking at one * tree. The canvas brings its own `ReactFlowProvider` and both stylesheets, so * a story is exactly what a consumer writes. * * `AutoLayout` is milestone 4's and is not here yet. */ import { Preset } from "@multiplatform.one/theme"; import type { Knobs } from "@multiplatform.one/theme"; import type { Meta, StoryObj } from "@storybook/react-native-web-vite"; import { useState } from "react"; import { Theme, XStack, YStack } from "tamagui"; import type { BuiltinEdgeData } from "../edges/edgeKinds"; import type { GraphDocument } from "../model/index"; import { NodeGraph } from "../NodeGraph"; import { demoGraphSchema } from "./demoNodeBody"; import type { DemoNodeData } from "./fixtures"; import { demoDocument, demoDocumentLarge } from "./fixtures"; import { Background, Controls, MiniMap } from "./xy"; import { SizableText } from "@multiplatform.one/components"; type Doc = GraphDocument; const schema = demoGraphSchema(); const meta: Meta = { title: "NodeGraph/NodeGraph", component: NodeGraph, parameters: { status: { type: "beta" }, layout: "fullscreen", docs: { description: { component: "The web editor: GraphVars writes the --xy-* token layer, KnobEnvelopeProvider rebuilds the Preset/Surface/Intent stack, and one registered node type and one registered edge type switch on the model kind internally. Nodes are Tamagui bodies resolved through the real useResolvedKnobs; culling is on by default, which is measured (node drag 25.71ms to 10.58ms at 200 nodes).", }, }, }, argTypes: { readOnly: { control: "boolean" }, coarsePointer: { control: "boolean", description: "Touch surface — widens the port pitch" }, onlyRenderVisibleElements: { control: "boolean" }, }, }; export default meta; type Story = StoryObj; /** Every story needs a real box: React Flow measures its parent, not the window. */ function Canvas({ children, height = 560 }: { children: React.ReactNode; height?: number }) { return ( {children} ); } /** * The controls are named one by one rather than spread from `args`. A * `{...args}` spread pins `NodeGraph`'s kind parameters to their defaults * before `schema` can infer them, and the demo document then stops type-checking * against its own schema — inference through a spread is worth nothing here. */ export const Main: Story = { render: (args) => ( ), }; /** * The four edge kinds on one canvas. `NodeGraph/Edges` shows them isolated; * this is the same table under real Tamagui nodes, which is where a stroke that * disappears against the node fill actually shows up. */ export const TypedEdges: Story = { render: () => ( ), }; /** * Five sources feeding two sinks across one gap, with the sinks' fans * crossing (FZ-9291). Before the router planned tracks, every flow edge took * the gap's middle line and the two fans read as one bus. */ function fanInDocument(): Doc { const sources = Array.from({ length: 5 }, (_, index) => ({ id: `s${index}`, kind: "source", position: { x: 0, y: index * 160 }, data: { table: `orders_${index}` }, })); const sinks = [ { id: "a", kind: "sink", position: { x: 360, y: 80 }, data: { target: "warehouse_a" } }, { id: "b", kind: "sink", position: { x: 360, y: 400 }, data: { target: "warehouse_b" } }, ]; const into: readonly [string, string][] = [ ["s0", "a"], ["s1", "a"], ["s3", "a"], ["s2", "b"], ["s4", "b"], ]; return { version: 1, nodes: [...sources, ...sinks] as Doc["nodes"], edges: into.map( ([source, target]) => ({ id: `${source}->${target}`, kind: "flow", source: { node: source, port: "rows" }, target: { node: target, port: "in" }, data: {}, }) as Doc["edges"][number], ), }; } export const FanIn: Story = { render: () => ( ), }; /** FanIn plus a third rank, with two edges that skip the sinks' rank and pass it between them. */ function rankSkipDocument(): Doc { const base = fanInDocument(); const flow = (source: string, port: string, target: string) => ({ id: `${source}->${target}`, kind: "flow", source: { node: source, port }, target: { node: target, port: "in" }, data: {}, }) as Doc["edges"][number]; return { version: 1, nodes: [ ...base.nodes, { id: "c", kind: "sink", position: { x: 720, y: 240 }, data: { target: "report" } }, ] as Doc["nodes"], edges: [...base.edges, flow("s0", "rows", "c"), flow("s4", "rows", "c")], }; } export const RankSkip: Story = { render: () => ( ), }; /** * Two hundred nodes. This is where `onlyRenderVisibleElements` earns its * default: count `.react-flow__node` in the inspector against the 200 in the * document, then flip the control off and count again. */ export const Large200: Story = { render: () => ( ), }; export const Empty: Story = { render: () => ( ), }; export const ReadOnly: Story = { render: () => ( ), }; /** * Controlled: the story owns the document and writes it back on every change. * The counter is the proof a 30-frame drag is ONE change, not thirty. */ export const Controlled: Story = { render: () => { const [document, setDocument] = useState(() => demoDocument()); const [changes, setChanges] = useState(0); return ( {`onDocumentChange calls: ${changes}`} {`node a at ${Math.round(document.nodes[0]?.position.x ?? 0)}, ${Math.round( document.nodes[0]?.position.y ?? 0, )}`} { setDocument(next); setChanges((count) => count + 1); }} > ); }, }; /** * The knob fuzz, WITH EDGES (design §11). `node scripts/knob-fuzz.mjs * MPO-191-graphnode 3` prints these three combos; they are transcribed here * rather than generated, so the story shows the same thing on every machine. * * Combo 1 has `borderWidth: none` and `elevation: none`, which paints a node * with no visible frame at all. That is the knobs OBEYING, not a defect — the * canvas background differs from the node fill, which is exactly what a graph * on a real canvas looks like and why this combo is fuzzed with edges. */ const fuzzCombos: readonly { readonly label: string; readonly knobs: Partial }[] = [ { label: "combo 1 · radius medium · borderWidth none · elevation none", knobs: { fillStyle: "filled", borderRadius: "medium", borderWidth: "none", elevation: "none", textAccent: "medium", headingFont: "geometric", bodyFont: "sans-serif", fontWeight: "bold", animation: "slow", }, }, { label: "combo 2 · radius none · animation none", knobs: { fillStyle: "filled", borderRadius: "none", borderWidth: "none", elevation: "small", textAccent: "medium", headingFont: "rounded", bodyFont: "cursive", fontWeight: "regular", animation: "none", }, }, { label: "combo 3 · radius full · elevation large", knobs: { fillStyle: "filled", borderRadius: "full", borderWidth: "small", elevation: "large", textAccent: "high", headingFont: "mono", bodyFont: "serif", fontWeight: "bold", animation: "bouncy", }, }, ]; export const KnobFuzz: Story = { render: () => ( {(["light", "dark"] as const).map((scheme) => ( {scheme} {fuzzCombos.map((combo) => ( {combo.label} ))} ))} ), };