|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useEffect } from 'react' |
| 4 | + |
| 5 | +const CONFIG = { |
| 6 | + /** Debounce window before flushing buffered events to the server. */ |
| 7 | + FLUSH_MS: 1500, |
| 8 | + /** Max buffered events before forcing an early flush. */ |
| 9 | + MAX_BUFFER: 500, |
| 10 | + /** Ancestor display-name pattern that defines the in-scope subtree. */ |
| 11 | + SCOPE: /copilot|panel/i, |
| 12 | + /** How far up the fiber tree to look for an in-scope ancestor. */ |
| 13 | + MAX_DEPTH: 80, |
| 14 | + /** Poll attempts while waiting for the React Scan global to load. */ |
| 15 | + HOOK_RETRIES: 50, |
| 16 | + HOOK_RETRY_MS: 200, |
| 17 | +} as const |
| 18 | + |
| 19 | +interface RenderEvent { |
| 20 | + t: number |
| 21 | + scope: string |
| 22 | + name: string |
| 23 | + phase?: string |
| 24 | + count: number |
| 25 | + time: number | null |
| 26 | + unnecessary: boolean |
| 27 | +} |
| 28 | + |
| 29 | +interface ScanFiber { |
| 30 | + type?: { displayName?: string; name?: string } | null |
| 31 | + return?: ScanFiber | null |
| 32 | +} |
| 33 | + |
| 34 | +interface ScanRender { |
| 35 | + phase?: string |
| 36 | + count?: number |
| 37 | + time?: number |
| 38 | + unnecessary?: boolean |
| 39 | +} |
| 40 | + |
| 41 | +type ScanFn = (options: { |
| 42 | + onRender?: (fiber: ScanFiber, renders: ScanRender | ScanRender[]) => void |
| 43 | +}) => void |
| 44 | + |
| 45 | +function fiberName(fiber: ScanFiber | null | undefined): string | null { |
| 46 | + const type = fiber?.type |
| 47 | + if (!type) return null |
| 48 | + return type.displayName || type.name || null |
| 49 | +} |
| 50 | + |
| 51 | +/** Returns the matching ancestor name if this fiber is within the copilot subtree. */ |
| 52 | +function scopeOf(fiber: ScanFiber): string | null { |
| 53 | + let current: ScanFiber | null | undefined = fiber |
| 54 | + let depth = 0 |
| 55 | + while (current && depth < CONFIG.MAX_DEPTH) { |
| 56 | + const name = fiberName(current) |
| 57 | + if (name && CONFIG.SCOPE.test(name)) return name |
| 58 | + current = current.return |
| 59 | + depth += 1 |
| 60 | + } |
| 61 | + return null |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Subscribes to React Scan's global render stream, filters to the copilot |
| 66 | + * panel subtree, and ships batched render telemetry to the dev-only collector |
| 67 | + * route. Rendered only when `isReactScanEnabled` (dev + `REACT_SCAN_ENABLED`), |
| 68 | + * so it is a no-op in production builds. |
| 69 | + */ |
| 70 | +export function ReactScanCollector() { |
| 71 | + useEffect(() => { |
| 72 | + let buffer: RenderEvent[] = [] |
| 73 | + let timer: number | null = null |
| 74 | + let stopped = false |
| 75 | + |
| 76 | + const flush = () => { |
| 77 | + timer = null |
| 78 | + if (buffer.length === 0) return |
| 79 | + const batch = buffer |
| 80 | + buffer = [] |
| 81 | + // boundary-raw-fetch: dev-only debug telemetry, schema-less, must not run through a contract |
| 82 | + void fetch('/api/_dev/react-scan', { |
| 83 | + method: 'POST', |
| 84 | + headers: { 'content-type': 'application/json' }, |
| 85 | + body: JSON.stringify({ events: batch }), |
| 86 | + keepalive: true, |
| 87 | + }).catch(() => {}) |
| 88 | + } |
| 89 | + |
| 90 | + const schedule = () => { |
| 91 | + if (buffer.length >= CONFIG.MAX_BUFFER) { |
| 92 | + if (timer !== null) window.clearTimeout(timer) |
| 93 | + flush() |
| 94 | + return |
| 95 | + } |
| 96 | + if (timer === null) timer = window.setTimeout(flush, CONFIG.FLUSH_MS) |
| 97 | + } |
| 98 | + |
| 99 | + const onRender = (fiber: ScanFiber, renders: ScanRender | ScanRender[]) => { |
| 100 | + if (stopped) return |
| 101 | + const scope = scopeOf(fiber) |
| 102 | + if (!scope) return |
| 103 | + const name = fiberName(fiber) ?? 'unknown' |
| 104 | + const list = Array.isArray(renders) ? renders : [renders] |
| 105 | + for (const render of list) { |
| 106 | + buffer.push({ |
| 107 | + t: Date.now(), |
| 108 | + scope, |
| 109 | + name, |
| 110 | + phase: render?.phase, |
| 111 | + count: render?.count ?? 1, |
| 112 | + time: typeof render?.time === 'number' ? Math.round(render.time * 100) / 100 : null, |
| 113 | + unnecessary: Boolean(render?.unnecessary), |
| 114 | + }) |
| 115 | + } |
| 116 | + schedule() |
| 117 | + } |
| 118 | + |
| 119 | + let attempts = 0 |
| 120 | + const hook = () => { |
| 121 | + if (stopped) return |
| 122 | + const scan = (window as unknown as { reactScan?: ScanFn }).reactScan |
| 123 | + if (typeof scan !== 'function') { |
| 124 | + if (attempts < CONFIG.HOOK_RETRIES) { |
| 125 | + attempts += 1 |
| 126 | + window.setTimeout(hook, CONFIG.HOOK_RETRY_MS) |
| 127 | + } |
| 128 | + return |
| 129 | + } |
| 130 | + scan({ onRender }) |
| 131 | + } |
| 132 | + hook() |
| 133 | + |
| 134 | + return () => { |
| 135 | + stopped = true |
| 136 | + if (timer !== null) window.clearTimeout(timer) |
| 137 | + flush() |
| 138 | + } |
| 139 | + }, []) |
| 140 | + |
| 141 | + return null |
| 142 | +} |
0 commit comments