|
| 1 | +/** |
| 2 | + * FlowStatePanel Component |
| 3 | + * |
| 4 | + * Displays live onFlowChange data and saved flow snapshots |
| 5 | + * in a dark-themed, resizable side panel with copy support. |
| 6 | + */ |
| 7 | + |
| 8 | +import { useCallback, useEffect, useRef, useState } from 'react' |
| 9 | + |
| 10 | +import type { FlowData } from '@flowiseai/agentflow' |
| 11 | + |
| 12 | +type FlowStatePanelTab = 'live' | 'saved' |
| 13 | + |
| 14 | +interface FlowStatePanelProps { |
| 15 | + currentFlow: FlowData | null |
| 16 | + savedFlow: FlowData | null |
| 17 | + changeCount: number |
| 18 | +} |
| 19 | + |
| 20 | +export function FlowStatePanel({ currentFlow, savedFlow, changeCount }: FlowStatePanelProps) { |
| 21 | + const [tab, setTab] = useState<FlowStatePanelTab>('live') |
| 22 | + const [copied, setCopied] = useState(false) |
| 23 | + const [width, setWidth] = useState(300) |
| 24 | + const dragging = useRef(false) |
| 25 | + const flow = tab === 'live' ? currentFlow : savedFlow |
| 26 | + |
| 27 | + const startX = useRef(0) |
| 28 | + const startWidth = useRef(0) |
| 29 | + |
| 30 | + const resizeBy = useCallback((delta: number) => { |
| 31 | + setWidth((w) => Math.max(200, Math.min(800, w + delta))) |
| 32 | + }, []) |
| 33 | + |
| 34 | + const onMouseMove = useCallback((moveEvent: MouseEvent) => { |
| 35 | + if (!dragging.current) return |
| 36 | + const newWidth = Math.max(200, Math.min(800, startWidth.current + (startX.current - moveEvent.clientX))) |
| 37 | + setWidth(newWidth) |
| 38 | + }, []) |
| 39 | + |
| 40 | + const onMouseUp = useCallback(() => { |
| 41 | + dragging.current = false |
| 42 | + document.removeEventListener('mousemove', onMouseMove) |
| 43 | + document.removeEventListener('mouseup', onMouseUp) |
| 44 | + document.body.style.cursor = '' |
| 45 | + document.body.style.userSelect = '' |
| 46 | + }, [onMouseMove]) |
| 47 | + |
| 48 | + // Clean up global listeners on unmount to prevent memory leaks |
| 49 | + useEffect(() => { |
| 50 | + return () => { |
| 51 | + document.removeEventListener('mousemove', onMouseMove) |
| 52 | + document.removeEventListener('mouseup', onMouseUp) |
| 53 | + document.body.style.cursor = '' |
| 54 | + document.body.style.userSelect = '' |
| 55 | + } |
| 56 | + }, [onMouseMove, onMouseUp]) |
| 57 | + |
| 58 | + const handleMouseDown = useCallback( |
| 59 | + (e: React.MouseEvent) => { |
| 60 | + e.preventDefault() |
| 61 | + dragging.current = true |
| 62 | + startX.current = e.clientX |
| 63 | + startWidth.current = width |
| 64 | + |
| 65 | + document.addEventListener('mousemove', onMouseMove) |
| 66 | + document.addEventListener('mouseup', onMouseUp) |
| 67 | + document.body.style.cursor = 'col-resize' |
| 68 | + document.body.style.userSelect = 'none' |
| 69 | + }, |
| 70 | + [width, onMouseMove, onMouseUp] |
| 71 | + ) |
| 72 | + |
| 73 | + const handleKeyDown = useCallback( |
| 74 | + (e: React.KeyboardEvent) => { |
| 75 | + if (e.key === 'ArrowLeft') { |
| 76 | + e.preventDefault() |
| 77 | + resizeBy(-20) |
| 78 | + } else if (e.key === 'ArrowRight') { |
| 79 | + e.preventDefault() |
| 80 | + resizeBy(20) |
| 81 | + } |
| 82 | + }, |
| 83 | + [resizeBy] |
| 84 | + ) |
| 85 | + |
| 86 | + return ( |
| 87 | + <div |
| 88 | + style={{ |
| 89 | + width: `${width}px`, |
| 90 | + minHeight: 0, |
| 91 | + background: '#1e1e2e', |
| 92 | + color: '#cdd6f4', |
| 93 | + display: 'flex', |
| 94 | + flexDirection: 'column', |
| 95 | + fontSize: '13px', |
| 96 | + fontFamily: 'monospace', |
| 97 | + borderLeft: '1px solid #313244', |
| 98 | + overflow: 'hidden', |
| 99 | + position: 'relative' |
| 100 | + }} |
| 101 | + > |
| 102 | + {/* Drag handle */} |
| 103 | + <button |
| 104 | + aria-label='Resize panel' |
| 105 | + onMouseDown={handleMouseDown} |
| 106 | + onKeyDown={handleKeyDown} |
| 107 | + style={{ |
| 108 | + position: 'absolute', |
| 109 | + left: 0, |
| 110 | + top: 0, |
| 111 | + bottom: 0, |
| 112 | + width: '4px', |
| 113 | + cursor: 'col-resize', |
| 114 | + zIndex: 10, |
| 115 | + padding: 0, |
| 116 | + border: 'none', |
| 117 | + background: 'transparent' |
| 118 | + }} |
| 119 | + /> |
| 120 | + {/* Tabs */} |
| 121 | + <div style={{ display: 'flex', borderBottom: '1px solid #313244' }}> |
| 122 | + <button |
| 123 | + onClick={() => setTab('live')} |
| 124 | + style={{ |
| 125 | + flex: 1, |
| 126 | + padding: '10px', |
| 127 | + background: tab === 'live' ? '#313244' : 'transparent', |
| 128 | + color: tab === 'live' ? '#cba6f7' : '#6c7086', |
| 129 | + border: 'none', |
| 130 | + cursor: 'pointer', |
| 131 | + fontFamily: 'monospace', |
| 132 | + fontSize: '12px', |
| 133 | + fontWeight: 600 |
| 134 | + }} |
| 135 | + > |
| 136 | + onFlowChange ({changeCount}) |
| 137 | + </button> |
| 138 | + <button |
| 139 | + onClick={() => setTab('saved')} |
| 140 | + style={{ |
| 141 | + flex: 1, |
| 142 | + padding: '10px', |
| 143 | + background: tab === 'saved' ? '#313244' : 'transparent', |
| 144 | + color: tab === 'saved' ? '#a6e3a1' : '#6c7086', |
| 145 | + border: 'none', |
| 146 | + cursor: 'pointer', |
| 147 | + fontFamily: 'monospace', |
| 148 | + fontSize: '12px', |
| 149 | + fontWeight: 600 |
| 150 | + }} |
| 151 | + > |
| 152 | + onSave {savedFlow ? '(1)' : '(0)'} |
| 153 | + </button> |
| 154 | + </div> |
| 155 | + |
| 156 | + {/* Summary stats + copy button */} |
| 157 | + {flow && ( |
| 158 | + <div |
| 159 | + style={{ display: 'flex', alignItems: 'center', gap: '12px', padding: '10px 14px', borderBottom: '1px solid #313244' }} |
| 160 | + > |
| 161 | + <span> |
| 162 | + <span style={{ color: '#89b4fa' }}>nodes:</span> {flow.nodes.length} |
| 163 | + </span> |
| 164 | + <span> |
| 165 | + <span style={{ color: '#f9e2af' }}>edges:</span> {flow.edges.length} |
| 166 | + </span> |
| 167 | + <button |
| 168 | + onClick={() => { |
| 169 | + navigator.clipboard.writeText(JSON.stringify(flow, null, 2)) |
| 170 | + setCopied(true) |
| 171 | + setTimeout(() => setCopied(false), 1500) |
| 172 | + }} |
| 173 | + style={{ |
| 174 | + marginLeft: 'auto', |
| 175 | + padding: '3px 10px', |
| 176 | + background: copied ? '#a6e3a1' : '#45475a', |
| 177 | + color: copied ? '#1e1e2e' : '#cdd6f4', |
| 178 | + border: 'none', |
| 179 | + borderRadius: '4px', |
| 180 | + cursor: 'pointer', |
| 181 | + fontFamily: 'monospace', |
| 182 | + fontSize: '11px', |
| 183 | + transition: 'all 0.15s' |
| 184 | + }} |
| 185 | + > |
| 186 | + {copied ? 'Copied!' : 'Copy'} |
| 187 | + </button> |
| 188 | + </div> |
| 189 | + )} |
| 190 | + |
| 191 | + {/* JSON payload */} |
| 192 | + <div style={{ flex: 1, overflow: 'auto', padding: '10px 14px' }}> |
| 193 | + {flow ? ( |
| 194 | + <pre style={{ margin: 0, whiteSpace: 'pre-wrap', wordBreak: 'break-word', lineHeight: 1.5 }}> |
| 195 | + {JSON.stringify(flow, null, 2)} |
| 196 | + </pre> |
| 197 | + ) : ( |
| 198 | + <div style={{ color: '#6c7086', padding: '20px 0', textAlign: 'center' }}> |
| 199 | + {tab === 'live' |
| 200 | + ? 'Interact with the canvas to see live flow data.' |
| 201 | + : 'Click Save or press Cmd+S to capture a snapshot.'} |
| 202 | + </div> |
| 203 | + )} |
| 204 | + </div> |
| 205 | + </div> |
| 206 | + ) |
| 207 | +} |
0 commit comments