/** * NodeGraph/Edges — the four kinds side by side, plus a refused connection with * its real message. * * The lanes render through `NodeGraphWeb`, the canvas a consumer mounts, so the * pane, the nodes and the edges all take the house theme (LC-18). A bare * `ReactFlow` here painted React Flow's own grey canvas and nodes under the * page's tinted ramp (FZ-8939). */ import { View, YStack } from "tamagui"; // Relative, not `@multiplatform.one/node-graph/model`: the subpath resolves // into `dist/`, which a source-consumed workspace package never builds. import { createGraphSchema, defaultRules, resolvePorts, validateConnection, type GraphDocument, type GraphNodeRenderProps, } from "../model/index"; import { legalTargets } from "../interaction/legalTargets"; import { connectionPuckLabel } from "../react-flow/ConnectionPuck"; import { NodeGraphWeb } from "../react-flow/NodeGraphWeb"; import { demoGraphSchema } from "../react-flow/demoNodeBody"; import { demoDocument } from "../react-flow/fixtures"; import { GraphNode } from "../nodes/GraphNode"; import { GraphPort } from "../nodes/GraphPort"; import type { GraphNodePort } from "../nodes/nodeMeasure"; import { builtinEdgeKindNames, builtinEdgeKinds } from "./edgeKinds"; import type { BuiltinEdgeData } from "./edgeKinds"; import { SizableText } from "@multiplatform.one/components"; export default { title: "NodeGraph/Edges", parameters: { status: { type: "beta" }, nativeGallery: { exempt: { from: "native", reason: "These stories mount the web canvas, which draws DOM, to build one lane per edge kind. On device the edges render inside the canvas's WebView.", readInstead: "NodeGraph → NodeGraph, where the same typed edges render in a real document", }, }, }, }; const rowHeight = 110; type LaneNode = { readonly label: string }; type LaneNodes = { readonly source: LaneNode; readonly target: LaneNode }; function LaneBody(props: GraphNodeRenderProps) { return ( } /> ); } const laneMeasure = { width: 172, minHeight: 48 }; const laneSchema = createGraphSchema({ id: "edges-story-lanes", nodes: { source: { kind: "source", render: LaneBody, defaultData: { label: "" }, measure: laneMeasure, ports: [{ id: "out", side: "right", direction: "out" }], }, target: { kind: "target", render: LaneBody, defaultData: { label: "" }, measure: laneMeasure, ports: [{ id: "in", side: "left", direction: "in" }], }, }, edges: builtinEdgeKinds, rules: defaultRules, }); /** One source/target pair per kind, stacked, so all four read at once. */ function laneDocument(withLabels: boolean): GraphDocument { return { version: 1, nodes: builtinEdgeKindNames.flatMap((kind, row) => [ { id: `s${row}`, kind: "source" as const, position: { x: 0, y: row * rowHeight }, data: { label: kind }, }, { id: `t${row}`, kind: "target" as const, position: { x: 320, y: row * rowHeight }, data: { label: "target" }, }, ]), edges: builtinEdgeKindNames.map((kind, row) => ({ id: `e${row}`, kind, source: { node: `s${row}`, port: "out" }, target: { node: `t${row}`, port: "in" }, data: {}, ...(withLabels ? { label: kind } : null), })) as GraphDocument["edges"], }; } function Canvas({ withLabels = false }: Readonly<{ withLabels?: boolean }>) { return ( ); } export const main = { name: "Main", render: () => , }; export const labels = { name: "Labels", render: () => , }; // ── Refused ──────────────────────────────────────────────────── // The message is the model's, not the story's: `validateConnection` produces // the code and the wording, and this story only shows them. A refusal invented // here would drift from the one a user actually sees on a drop. type Nodes = { readonly numberOut: Record; readonly textIn: Record; }; const refusalSchema = createGraphSchema({ id: "edges-story-refusal", nodes: { numberOut: { kind: "numberOut", render: () => null, defaultData: {}, measure: { width: 172, minHeight: 48 }, ports: [ { id: "out", side: "right", direction: "out", dataType: "number" }, // An input on the SAME node, so the self-loop case below really is a // self loop. Without it `a.out -> a.out` reports `wrong-direction`, // because refuseDirection runs first — correct, but not what a story // captioned "onto itself" claims to be showing. { id: "in", side: "left", direction: "in", dataType: "number" }, ], }, textIn: { kind: "textIn", render: () => null, defaultData: {}, measure: { width: 172, minHeight: 48 }, ports: [{ id: "in", side: "left", direction: "in", dataType: "string" }], }, }, edges: builtinEdgeKinds, rules: defaultRules, }); const refusalDoc: GraphDocument = { version: 1, nodes: [ { id: "a", kind: "numberOut", position: { x: 0, y: 0 }, data: {} }, { id: "b", kind: "textIn", position: { x: 320, y: 0 }, data: {} }, ], edges: [], }; const refusals = [ { title: "number → string", source: { node: "a", port: "out" }, target: { node: "b", port: "in" }, }, { title: "backwards", source: { node: "b", port: "in" }, target: { node: "a", port: "out" } }, { title: "onto itself", source: { node: "a", port: "out" }, target: { node: "a", port: "in" } }, ] as const; export const refused = { name: "Refused", render: () => ( {refusals.map(({ title, source, target }) => { const verdict = validateConnection({ document: refusalDoc, schema: refusalSchema, source, target, }); return ( {title} {verdict.ok ? "allowed" : `${verdict.code} — ${verdict.message}`} ); })} ), }; /** * `Connecting` — legal-target magnetism and the puck that names what you are * over (design §7.4, task 3.5). * * DRAG from a port. Two things to watch, and they are the whole task: * * 1. The puck at the top of the canvas names the candidate — but only while * the candidate is one `validateConnection` accepts. `connectionRadius` is * the 44 px hit diameter (task 3.4 H5), so React Flow's magnetism will snap * the drag onto a refused port quite happily; the puck refuses to promise * it and falls back to naming what you are dragging FROM. * 2. The puck does not grow or shrink when you zoom. It renders as a sibling * of `.react-flow__viewport`, so the pan/zoom matrix never reaches it. * * The table underneath is the same `connectionPuckLabel` call, run over every * port in the document at once, so what the puck will say is readable without * holding a mouse button down. */ const connectDoc = demoDocument(); const connectSchema = demoGraphSchema(); function everyTarget(): { node: string; port: string }[] { return connectDoc.nodes.flatMap((node) => resolvePorts(connectSchema, node).map((spec) => ({ node: node.id, port: spec.id })), ); } export const connecting = { name: "Connecting", render: () => { const source = { node: "a", port: "rows" }; const legal = new Set( legalTargets({ document: connectDoc, schema: connectSchema, source }).map( (ref) => `${ref.node}:${ref.port}`, ), ); return ( Dragging from a / rows — what the puck says over each port {everyTarget().map((target) => { const key = `${target.node}:${target.port}`; const label = connectionPuckLabel({ document: connectDoc, schema: connectSchema, source, candidate: target, }); return ( {key} — {label?.legal ? "MAGNETIC" : "refused"} — {label?.text} ); })} ); }, };