* docs: add React Flow migration design for flow editor canvas Replaces hand-built CSS flexbox canvas with @xyflow/react for zoom/pan, dagre auto-layout, collapsible minimap, and side-panel editing. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * docs: add React Flow migration implementation plan 12 tasks across 8 phases covering dagre layout, custom nodes, side panel editor, and full canvas integration. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: install @xyflow/react and @dagrejs/dagre Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add dagre layout utility for React Flow node positioning Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add FlowCanvasNode compact card for React Flow canvas Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add FlowCanvasAnswerNode stub card for React Flow canvas Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add useTreeLayout hook for tree-to-ReactFlow conversion with dagre Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add NodeEditorPanel side panel for React Flow canvas editing Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add FlowCanvas main React Flow component with zoom/pan/minimap Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: wire FlowCanvas and NodeEditorPanel into TreeEditorLayout Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: add panel state management for node editor in TreeEditorPage Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * style: add React Flow dark theme overrides for canvas Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * chore: export new React Flow canvas components from barrel Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: enable scrolling in node editor panel sidebar Add min-h-0 to flex containers in the ancestor chain so overflow-y-auto actually triggers instead of content overflowing off-screen. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: constrain tree editor page height to prevent panel overflow Add overflow-hidden to TreeEditorPage root and NodeEditorPanel container so the flex height chain is properly constrained by the CSS Grid cell, preventing the node editor sidebar from growing beyond the viewport. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: resolve lint errors in NodeEditorPanel and useTreeLayout - Fix unused 'children' destructuring with _children prefix - Move handleClose declaration above the useEffect that references it - Use handleClose as proper dependency instead of eslint-disable - Fix unused _parentId parameter type in useTreeLayout Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: use viewport-based height for node editor panel Replace h-full with calc(100vh - 105px) to bypass the CSS height chain that fails to constrain the panel across browsers. The 105px accounts for the topbar (56px) and editor toolbar (49px). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * style: fix canvas controls visibility and enhance dot grid background - Add !important to all React Flow dark theme overrides to ensure they win over library default styles (fixes white controls rectangle) - Add SVG fill inheritance for control button icons - Use slightly lighter canvas background (bg-accent/30) so dot grid is more visible - Increase dot size and use muted-foreground color for better contrast Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * feat: collapse sidebar categories with show more/less toggle Show only the first 4 categories by default with a "N more" button to expand the full list. Reduces sidebar clutter when many categories exist. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
200 lines
6.8 KiB
TypeScript
200 lines
6.8 KiB
TypeScript
import { useMemo, useCallback, useState, useRef, useEffect } from 'react'
|
|
import type { Node, Edge } from '@xyflow/react'
|
|
import type { TreeStructure } from '@/types'
|
|
import { getLayoutedElements, NODE_WIDTH } from '@/lib/dagreLayout'
|
|
import type { FlowCanvasNodeData } from './FlowCanvasNode'
|
|
import type { FlowCanvasAnswerNodeData } from './FlowCanvasAnswerNode'
|
|
import { useTreeEditorStore } from '@/store/treeEditorStore'
|
|
|
|
const MAX_EDGE_LABEL_LENGTH = 35
|
|
|
|
function truncateLabel(label: string): string {
|
|
if (label.length <= MAX_EDGE_LABEL_LENGTH) return label
|
|
return label.slice(0, MAX_EDGE_LABEL_LENGTH).trimEnd() + '…'
|
|
}
|
|
|
|
function estimateNodeHeight(node: TreeStructure): number {
|
|
let height = 52 // header baseline
|
|
if (node.type === 'decision' && node.options) {
|
|
height += 24 // options header line
|
|
height += Math.min(node.options.length, 3) * 18 // option rows (max 3 shown)
|
|
if (node.options.length > 3) height += 18 // "+N more" row
|
|
}
|
|
if ((node.type === 'action' || node.type === 'solution') && node.description) {
|
|
height += 36 // description preview (2 lines)
|
|
}
|
|
if (node.type === 'answer') {
|
|
return 70 // fixed height for answer stubs
|
|
}
|
|
return height
|
|
}
|
|
|
|
interface UseTreeLayoutResult {
|
|
nodes: Node[]
|
|
edges: Edge[]
|
|
collapsedNodeIds: Set<string>
|
|
toggleCollapse: (nodeId: string) => void
|
|
onNodesMeasured: (measuredNodes: Node[]) => void
|
|
}
|
|
|
|
export function useTreeLayout(): UseTreeLayoutResult {
|
|
const treeStructure = useTreeEditorStore(s => s.treeStructure)
|
|
const validationErrors = useTreeEditorStore(s => s.validationErrors)
|
|
const [collapsedNodeIds, setCollapsedNodeIds] = useState<Set<string>>(new Set())
|
|
const [measuredHeights, setMeasuredHeights] = useState<Map<string, number>>(new Map())
|
|
const correctionDone = useRef(false)
|
|
|
|
const toggleCollapse = useCallback((nodeId: string) => {
|
|
setCollapsedNodeIds(prev => {
|
|
const next = new Set(prev)
|
|
if (next.has(nodeId)) next.delete(nodeId)
|
|
else next.add(nodeId)
|
|
return next
|
|
})
|
|
}, [])
|
|
|
|
// Convert tree structure to flat nodes and edges
|
|
const { rawNodes, rawEdges } = useMemo(() => {
|
|
const nodes: Node[] = []
|
|
const edges: Edge[] = []
|
|
|
|
if (!treeStructure) return { rawNodes: nodes, rawEdges: edges }
|
|
|
|
function walk(node: TreeStructure, _parentId?: string | null) {
|
|
const isCollapsed = collapsedNodeIds.has(node.id)
|
|
const hasChildren = (node.children?.length ?? 0) > 0
|
|
const hasErrors = validationErrors.some(e => e.nodeId === node.id && e.severity === 'error')
|
|
|
|
const estimatedHeight = measuredHeights.get(node.id) ?? estimateNodeHeight(node)
|
|
|
|
if (node.type === 'answer') {
|
|
nodes.push({
|
|
id: node.id,
|
|
type: 'answerStub',
|
|
position: { x: 0, y: 0 }, // dagre will set this
|
|
data: {
|
|
node,
|
|
onSelectType: () => {}, // placeholder — set by FlowCanvas
|
|
} satisfies FlowCanvasAnswerNodeData,
|
|
style: { width: NODE_WIDTH },
|
|
measured: { width: NODE_WIDTH, height: estimatedHeight },
|
|
})
|
|
} else {
|
|
nodes.push({
|
|
id: node.id,
|
|
type: 'flowNode',
|
|
position: { x: 0, y: 0 },
|
|
data: {
|
|
node,
|
|
hasChildren,
|
|
isCollapsed,
|
|
hasValidationErrors: hasErrors,
|
|
isNew: false,
|
|
onToggleCollapse: () => {}, // placeholder — set by FlowCanvas
|
|
} satisfies FlowCanvasNodeData,
|
|
style: { width: NODE_WIDTH },
|
|
measured: { width: NODE_WIDTH, height: estimatedHeight },
|
|
})
|
|
}
|
|
|
|
// Skip children if collapsed
|
|
if (isCollapsed) return
|
|
|
|
// Create edges and recurse into children
|
|
if (node.children) {
|
|
// For decision nodes: order children by option link, then unlinked
|
|
const orderedChildren = orderChildren(node)
|
|
|
|
for (const { child, optionLabel } of orderedChildren) {
|
|
const edgeLabel = optionLabel ? truncateLabel(optionLabel) : undefined
|
|
|
|
edges.push({
|
|
id: `${node.id}->${child.id}`,
|
|
source: node.id,
|
|
target: child.id,
|
|
type: 'smoothstep',
|
|
label: edgeLabel,
|
|
labelStyle: { fill: 'hsl(var(--muted-foreground))', fontSize: 11 },
|
|
labelBgStyle: { fill: 'hsl(var(--card))', fillOpacity: 0.9 },
|
|
labelBgPadding: [4, 2] as [number, number],
|
|
style: { stroke: 'hsl(var(--border))' },
|
|
})
|
|
|
|
walk(child, node.id)
|
|
}
|
|
}
|
|
}
|
|
|
|
walk(treeStructure, null)
|
|
|
|
return { rawNodes: nodes, rawEdges: edges }
|
|
}, [treeStructure, collapsedNodeIds, validationErrors, measuredHeights])
|
|
|
|
// Run dagre layout
|
|
const { nodes, edges } = useMemo(() => {
|
|
if (rawNodes.length === 0) return { nodes: rawNodes, edges: rawEdges }
|
|
const layouted = getLayoutedElements(rawNodes, rawEdges)
|
|
return { nodes: layouted, edges: rawEdges }
|
|
}, [rawNodes, rawEdges])
|
|
|
|
// Height measurement correction callback
|
|
const onNodesMeasured = useCallback((measuredNodes: Node[]) => {
|
|
if (correctionDone.current) return
|
|
|
|
let needsCorrection = false
|
|
const newHeights = new Map(measuredHeights)
|
|
|
|
for (const mNode of measuredNodes) {
|
|
const actual = mNode.measured?.height
|
|
if (!actual) continue
|
|
const estimated = measuredHeights.get(mNode.id) ?? estimateNodeHeight(
|
|
(mNode.data as unknown as FlowCanvasNodeData)?.node ?? (mNode.data as unknown as FlowCanvasAnswerNodeData)?.node
|
|
)
|
|
if (Math.abs(actual - estimated) > 10) {
|
|
newHeights.set(mNode.id, actual)
|
|
needsCorrection = true
|
|
}
|
|
}
|
|
|
|
if (needsCorrection) {
|
|
correctionDone.current = true
|
|
setMeasuredHeights(newHeights)
|
|
}
|
|
}, [measuredHeights])
|
|
|
|
// Reset correction flag when tree structure changes
|
|
useEffect(() => {
|
|
correctionDone.current = false
|
|
}, [treeStructure, collapsedNodeIds])
|
|
|
|
return { nodes, edges, collapsedNodeIds, toggleCollapse, onNodesMeasured }
|
|
}
|
|
|
|
// Helper: order children by decision option links
|
|
function orderChildren(node: TreeStructure): Array<{ child: TreeStructure; optionLabel?: string }> {
|
|
if (!node.children || node.children.length === 0) return []
|
|
|
|
if (node.type === 'decision' && node.options) {
|
|
const linked: Array<{ child: TreeStructure; optionLabel: string }> = []
|
|
const linkedIds = new Set<string>()
|
|
|
|
for (const opt of node.options) {
|
|
if (opt.next_node_id) {
|
|
const child = node.children.find(c => c.id === opt.next_node_id)
|
|
if (child) {
|
|
linked.push({ child, optionLabel: opt.label })
|
|
linkedIds.add(child.id)
|
|
}
|
|
}
|
|
}
|
|
|
|
const unlinked = node.children
|
|
.filter(c => !linkedIds.has(c.id))
|
|
.map(child => ({ child, optionLabel: undefined }))
|
|
|
|
return [...linked, ...unlinked]
|
|
}
|
|
|
|
return node.children.map(child => ({ child, optionLabel: undefined }))
|
|
}
|