()
| 23 | } |
| 24 | |
| 25 | export function useDebugLog() { |
| 26 | const [debugOpen, setDebugOpenState] = useState(false); |
| 27 | const [debugEntries, setDebugEntries] = useState<DebugEntry[]>([]); |
| 28 | const [hasDebugAlerts, setHasDebugAlerts] = useState(false); |
| 29 | const [debugPinned, setDebugPinned] = useState(false); |
| 30 | const debugOpenRef = useRef(debugOpen); |
| 31 | debugOpenRef.current = debugOpen; |
| 32 | |
| 33 | const isAlertEntry = useCallback((entry: DebugEntry) => { |
| 34 | if (entry.source === "error" || entry.source === "stderr") { |
| 35 | return true; |
| 36 | } |
| 37 | const label = entry.label.toLowerCase(); |
| 38 | if (label.includes("warn") || label.includes("warning")) { |
| 39 | return true; |
| 40 | } |
| 41 | if (typeof entry.payload === "string") { |
| 42 | const payload = entry.payload.toLowerCase(); |
| 43 | return payload.includes("warn") || payload.includes("warning"); |
| 44 | } |
| 45 | return false; |
| 46 | }, []); |
| 47 | |
| 48 | const addDebugEntry = useCallback( |
| 49 | (entry: DebugEntry) => { |
| 50 | const isAlert = isAlertEntry(entry); |
| 51 | if (!debugOpenRef.current && !isAlert) { |
| 52 | return; |
| 53 | } |
| 54 | if (isAlert) { |
| 55 | setHasDebugAlerts(true); |
| 56 | } |
| 57 | const compactEntry = { ...entry, payload: summarizePayload(entry.payload) }; |
| 58 | setDebugEntries((prev) => [...prev, compactEntry].slice(-MAX_DEBUG_ENTRIES)); |
| 59 | }, |
| 60 | [isAlertEntry], |
| 61 | ); |
| 62 | |
| 63 | const handleCopyDebug = useCallback(async () => { |
| 64 | const text = debugEntries |
| 65 | .map((entry) => { |
| 66 | const timestamp = new Date(entry.timestamp).toLocaleTimeString(); |
| 67 | const payload = |
| 68 | entry.payload !== undefined |
| 69 | ? typeof entry.payload === "string" |
| 70 | ? entry.payload |
| 71 | : JSON.stringify(entry.payload, null, 2) |
| 72 | : ""; |
| 73 | return [entry.source.toUpperCase(), timestamp, entry.label, payload] |
| 74 | .filter(Boolean) |
| 75 | .join("\n"); |
| 76 | }) |
| 77 | .join("\n\n"); |
| 78 | if (text) { |
| 79 | await navigator.clipboard.writeText(text); |
| 80 | } |
| 81 | }, [debugEntries]); |
| 82 |
no test coverage detected