|
| 1 | +/** |
| 2 | + * CliMonitor — 画布右下角 CLI 全流程监控折叠面板 |
| 3 | + * |
| 4 | + * 订阅 logBus 实时显示用户左右面板动作 + Aletheia 进度 + 本地任务 + yjs 状态。 |
| 5 | + * 折叠时只露一个小条, 展开后是全屏右侧 384px 宽抽屉, 黑底等宽字体。 |
| 6 | + * |
| 7 | + * 操作: |
| 8 | + * - 点条头 toggle 展开/折叠 |
| 9 | + * - 清空 / 复制全部 / 下载 .txt |
| 10 | + * - 过滤等级 (info/warn/error/all) |
| 11 | + * - 跟随最新 (auto-scroll) |
| 12 | + */ |
| 13 | + |
| 14 | +import { useEffect, useRef, useState } from 'react' |
| 15 | +import { onAppend, getAll, clear, exportText, pushLog } from '../utils/logBus' |
| 16 | + |
| 17 | +const LEVEL_COLOR = { |
| 18 | + debug: '#888', |
| 19 | + info: '#9ec3d8', |
| 20 | + warn: '#e8d5c0', |
| 21 | + error: '#d27b7b', |
| 22 | +} |
| 23 | +const SOURCE_COLOR = { |
| 24 | + action: '#c8a882', |
| 25 | + aletheia: '#a78bfa', |
| 26 | + task: '#7bc47f', |
| 27 | + yjs: '#7c9eb2', |
| 28 | + console: '#888', |
| 29 | + bus: '#555', |
| 30 | + net: '#d27b7b', |
| 31 | + misc: '#bbb', |
| 32 | +} |
| 33 | + |
| 34 | +export default function CliMonitor() { |
| 35 | + const [open, setOpen] = useState(false) |
| 36 | + const [logs, setLogs] = useState(() => getAll()) |
| 37 | + const [filter, setFilter] = useState('all') // all | info | warn | error |
| 38 | + const [autoScroll, setAutoScroll] = useState(true) |
| 39 | + const [paused, setPaused] = useState(false) |
| 40 | + const scrollRef = useRef(null) |
| 41 | + |
| 42 | + // 订阅 logBus |
| 43 | + useEffect(() => { |
| 44 | + const off = onAppend((e) => { |
| 45 | + if (paused) return |
| 46 | + setLogs((prev) => { |
| 47 | + // ring buffer 同步(最多 1000 条) |
| 48 | + const next = prev.length >= 1000 ? prev.slice(-999).concat([e]) : prev.concat([e]) |
| 49 | + return next |
| 50 | + }) |
| 51 | + }) |
| 52 | + return off |
| 53 | + }, [paused]) |
| 54 | + |
| 55 | + // auto-scroll |
| 56 | + useEffect(() => { |
| 57 | + if (autoScroll && scrollRef.current) { |
| 58 | + scrollRef.current.scrollTop = scrollRef.current.scrollHeight |
| 59 | + } |
| 60 | + }, [logs, autoScroll]) |
| 61 | + |
| 62 | + // 过滤后日志 |
| 63 | + const filtered = filter === 'all' ? logs : logs.filter((e) => e.level === filter) |
| 64 | + |
| 65 | + // 错误数 / 警告数(折叠时显示徽章) |
| 66 | + const errCount = logs.filter((e) => e.level === 'error').length |
| 67 | + const warnCount = logs.filter((e) => e.level === 'warn').length |
| 68 | + |
| 69 | + const handleClear = () => { clear(); setLogs([]) } |
| 70 | + const handleCopy = () => { |
| 71 | + navigator.clipboard?.writeText(exportText()).then( |
| 72 | + () => pushLog({ level: 'info', source: 'bus', msg: '已复制 ' + logs.length + ' 行到剪贴板' }), |
| 73 | + () => pushLog({ level: 'warn', source: 'bus', msg: '复制失败(剪贴板权限被拒)' }), |
| 74 | + ) |
| 75 | + } |
| 76 | + const handleDownload = () => { |
| 77 | + const blob = new Blob([exportText()], { type: 'text/plain;charset=utf-8' }) |
| 78 | + const url = URL.createObjectURL(blob) |
| 79 | + const a = document.createElement('a') |
| 80 | + a.href = url |
| 81 | + a.download = `know-canvas-log-${new Date().toISOString().slice(0, 19).replace(/:/g, '-')}.txt` |
| 82 | + a.click() |
| 83 | + URL.revokeObjectURL(url) |
| 84 | + } |
| 85 | + |
| 86 | + // 折叠态 — 右下角小条 |
| 87 | + if (!open) { |
| 88 | + return ( |
| 89 | + <button |
| 90 | + onClick={() => setOpen(true)} |
| 91 | + className="absolute bottom-3 right-3 z-40 flex items-center gap-2 px-3 py-1.5 rounded-md shadow-lg transition-all hover:translate-y-[-2px]" |
| 92 | + style={{ |
| 93 | + background: '#1a1a1a', |
| 94 | + color: '#fafafa', |
| 95 | + border: '1px solid #2d2d2d', |
| 96 | + fontFamily: '"Courier New", monospace', |
| 97 | + fontSize: 11, |
| 98 | + letterSpacing: '0.05em', |
| 99 | + }} |
| 100 | + title="展开 CLI 监控面板" |
| 101 | + > |
| 102 | + <span |
| 103 | + className="inline-block w-1.5 h-1.5 rounded-full" |
| 104 | + style={{ backgroundColor: errCount > 0 ? '#d27b7b' : warnCount > 0 ? '#e8d5c0' : '#7bc47f' }} |
| 105 | + /> |
| 106 | + <span>CLI 监控</span> |
| 107 | + <span style={{ color: '#888' }}>{logs.length}</span> |
| 108 | + {errCount > 0 && <span style={{ color: '#d27b7b' }}>!{errCount}</span>} |
| 109 | + </button> |
| 110 | + ) |
| 111 | + } |
| 112 | + |
| 113 | + return ( |
| 114 | + <div |
| 115 | + className="absolute top-0 right-0 bottom-0 z-40 flex flex-col" |
| 116 | + style={{ |
| 117 | + width: 420, |
| 118 | + background: '#0f0f0f', |
| 119 | + color: '#e0e0e0', |
| 120 | + borderLeft: '1px solid #2d2d2d', |
| 121 | + fontFamily: '"Courier New", "Consolas", monospace', |
| 122 | + fontSize: 11, |
| 123 | + }} |
| 124 | + > |
| 125 | + {/* Header */} |
| 126 | + <div |
| 127 | + className="flex items-center justify-between px-3 py-2" |
| 128 | + style={{ background: '#1a1a1a', borderBottom: '1px solid #2d2d2d' }} |
| 129 | + > |
| 130 | + <div className="flex items-center gap-2"> |
| 131 | + <span |
| 132 | + className="inline-block w-1.5 h-1.5 rounded-full animate-pulse" |
| 133 | + style={{ backgroundColor: paused ? '#888' : '#c8a882' }} |
| 134 | + /> |
| 135 | + <span style={{ color: '#c8a882', letterSpacing: '0.25em', fontSize: 10 }}>CLI 全流程监控</span> |
| 136 | + <span style={{ color: '#666', fontSize: 10 }}>({logs.length}/1000)</span> |
| 137 | + </div> |
| 138 | + <button |
| 139 | + onClick={() => setOpen(false)} |
| 140 | + className="px-2 py-0.5 rounded" |
| 141 | + style={{ color: '#888', fontSize: 14 }} |
| 142 | + title="折叠" |
| 143 | + > |
| 144 | + — |
| 145 | + </button> |
| 146 | + </div> |
| 147 | + |
| 148 | + {/* Toolbar */} |
| 149 | + <div |
| 150 | + className="flex items-center gap-1 px-2 py-1.5" |
| 151 | + style={{ background: '#161616', borderBottom: '1px solid #2d2d2d', fontSize: 10 }} |
| 152 | + > |
| 153 | + {['all', 'info', 'warn', 'error'].map((lv) => ( |
| 154 | + <button |
| 155 | + key={lv} |
| 156 | + onClick={() => setFilter(lv)} |
| 157 | + className="px-2 py-0.5 rounded" |
| 158 | + style={{ |
| 159 | + background: filter === lv ? '#2d2d2d' : 'transparent', |
| 160 | + color: filter === lv ? '#fafafa' : '#888', |
| 161 | + border: '1px solid ' + (filter === lv ? '#3a3a3a' : 'transparent'), |
| 162 | + }} |
| 163 | + > |
| 164 | + {lv} |
| 165 | + </button> |
| 166 | + ))} |
| 167 | + <span style={{ color: '#444', margin: '0 4px' }}>|</span> |
| 168 | + <button onClick={() => setPaused((p) => !p)} className="px-2 py-0.5 rounded" style={{ color: paused ? '#e8d5c0' : '#888' }}> |
| 169 | + {paused ? '▶' : '⏸'} |
| 170 | + </button> |
| 171 | + <button onClick={() => setAutoScroll((a) => !a)} className="px-2 py-0.5 rounded" style={{ color: autoScroll ? '#7bc47f' : '#888' }}> |
| 172 | + {autoScroll ? '↓ 跟随' : '↓ 暂停'} |
| 173 | + </button> |
| 174 | + <span style={{ flex: 1 }} /> |
| 175 | + <button onClick={handleCopy} className="px-2 py-0.5 rounded" style={{ color: '#888' }} title="复制全部">⎘</button> |
| 176 | + <button onClick={handleDownload} className="px-2 py-0.5 rounded" style={{ color: '#888' }} title="下载 .txt">⬇</button> |
| 177 | + <button onClick={handleClear} className="px-2 py-0.5 rounded" style={{ color: '#d27b7b' }} title="清空">×</button> |
| 178 | + </div> |
| 179 | + |
| 180 | + {/* Logs */} |
| 181 | + <div ref={scrollRef} className="flex-1 overflow-y-auto px-2 py-1" style={{ scrollbarWidth: 'thin' }}> |
| 182 | + {filtered.length === 0 ? ( |
| 183 | + <div style={{ color: '#444', padding: 16, textAlign: 'center', fontSize: 11 }}> |
| 184 | + {logs.length === 0 ? '暂无日志…\n左右面板任意操作都会出现在这里' : '<当前过滤无匹配>'} |
| 185 | + </div> |
| 186 | + ) : ( |
| 187 | + filtered.map((e, i) => { |
| 188 | + const t = new Date(e.ts).toISOString().slice(11, 23) |
| 189 | + return ( |
| 190 | + <div key={i} style={{ marginBottom: 2, lineHeight: 1.5, wordBreak: 'break-word' }}> |
| 191 | + <span style={{ color: '#555' }}>{t}</span> |
| 192 | + {' '} |
| 193 | + <span style={{ color: LEVEL_COLOR[e.level] || '#bbb' }}>{e.level.toUpperCase().padEnd(5)}</span> |
| 194 | + {' '} |
| 195 | + <span style={{ color: SOURCE_COLOR[e.source] || '#888' }}>[{e.source}]</span> |
| 196 | + {' '} |
| 197 | + <span style={{ color: '#e0e0e0' }}>{e.msg}</span> |
| 198 | + {e.data && ( |
| 199 | + <pre style={{ color: '#7c9eb2', marginLeft: 16, fontSize: 10, whiteSpace: 'pre-wrap', marginTop: 1 }}> |
| 200 | + {JSON.stringify(e.data, null, 0)} |
| 201 | + </pre> |
| 202 | + )} |
| 203 | + </div> |
| 204 | + ) |
| 205 | + }) |
| 206 | + )} |
| 207 | + </div> |
| 208 | + </div> |
| 209 | + ) |
| 210 | +} |
0 commit comments