|
| 1 | +import React from 'react'; |
| 2 | +import './WebhookEventsPanel.css'; |
| 3 | +import './WebhookInspector.css'; |
| 4 | + |
| 5 | +type EventItem = { |
| 6 | + id?: string; |
| 7 | + eventType: string; |
| 8 | + timestamp: string; |
| 9 | + data?: any; |
| 10 | + raw?: any; |
| 11 | + expanded?: boolean; |
| 12 | +}; |
| 13 | + |
| 14 | +interface Props { |
| 15 | + userPath: string; |
| 16 | + selectedBayDbId?: number | null; |
| 17 | + selectedBayId?: string | null; |
| 18 | +} |
| 19 | + |
| 20 | +const getBayIdFromEvent = (e: EventItem) => { |
| 21 | + try { |
| 22 | + const raw = e.raw as any; |
| 23 | + const data = e.data as any; |
| 24 | + if (raw && raw.common && raw.common.Bay && (raw.common.Bay.Id || raw.common.Bay.id)) return raw.common.Bay.Id ?? raw.common.Bay.id; |
| 25 | + if (raw && raw.common && raw.common.BayId) return raw.common.BayId; |
| 26 | + if (raw && raw.data && raw.data.Bay && (raw.data.Bay.Id || raw.data.Bay.id)) return raw.data.Bay.Id ?? raw.data.Bay.id; |
| 27 | + if (raw && raw.data && (raw.data.BayId || raw.data.bayId)) return raw.data.BayId ?? raw.data.bayId; |
| 28 | + if (raw && raw.Bay && (raw.Bay.Id || raw.Bay.id)) return raw.Bay.Id ?? raw.Bay.id; |
| 29 | + if (data && data.Bay && (data.Bay.Id || data.Bay.id)) return data.Bay.Id ?? data.Bay.id; |
| 30 | + if (data && (data.BayId || data.bayId)) return data.BayId ?? data.bayId; |
| 31 | + if (data && data.common && data.common.Bay && (data.common.Bay.Id || data.common.Bay.id)) return data.common.Bay.Id ?? data.common.Bay.id; |
| 32 | + return null; |
| 33 | + } catch (err) { |
| 34 | + return null; |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +const WebhookInspector: React.FC<Props> = ({ userPath, selectedBayDbId = null, selectedBayId = null }) => { |
| 39 | + const [allEvents, setAllEvents] = React.useState<EventItem[]>([]); |
| 40 | + const [connected, setConnected] = React.useState(false); |
| 41 | + const [selectedIndex, setSelectedIndex] = React.useState<number | null>(null); |
| 42 | + const listRef = React.useRef<HTMLUListElement | null>(null); |
| 43 | + |
| 44 | + // Fetch initial events |
| 45 | + React.useEffect(() => { |
| 46 | + if (!userPath) return; |
| 47 | + let cancelled = false; |
| 48 | + (async () => { |
| 49 | + try { |
| 50 | + const r = await fetch(`/api/webhook/${encodeURIComponent(userPath)}/events`); |
| 51 | + if (!r.ok) throw new Error(await r.text()); |
| 52 | + const j = await r.json(); |
| 53 | + if (!cancelled && Array.isArray(j.events)) { |
| 54 | + setAllEvents(j.events.map((e: any) => ({ id: e.id, eventType: e.eventType, timestamp: e.timestamp, data: e.data, raw: e.raw, expanded: false }))); |
| 55 | + } |
| 56 | + } catch (err) { |
| 57 | + console.warn('Failed to load events', err); |
| 58 | + } |
| 59 | + })(); |
| 60 | + return () => { cancelled = true; }; |
| 61 | + }, [userPath]); |
| 62 | + |
| 63 | + // SSE subscription |
| 64 | + React.useEffect(() => { |
| 65 | + if (!userPath) return; |
| 66 | + let es: EventSource | null = null; |
| 67 | + let reconnectTimer: number | null = null; |
| 68 | + |
| 69 | + const connect = () => { |
| 70 | + es = new EventSource(`/api/webhook/${encodeURIComponent(userPath)}/stream`); |
| 71 | + es.onopen = () => setConnected(true); |
| 72 | + es.onerror = () => { |
| 73 | + setConnected(false); |
| 74 | + if (es) es.close(); |
| 75 | + reconnectTimer = window.setTimeout(() => connect(), 3000); |
| 76 | + }; |
| 77 | + es.onmessage = (ev) => { |
| 78 | + try { |
| 79 | + const data = JSON.parse(ev.data); |
| 80 | + setAllEvents(prev => [{ id: data.id, eventType: data.eventType, timestamp: data.timestamp, data: data.data, raw: data.raw, expanded: false }, ...prev]); |
| 81 | + } catch (err) { |
| 82 | + console.warn('Invalid SSE payload', err); |
| 83 | + } |
| 84 | + }; |
| 85 | + }; |
| 86 | + |
| 87 | + connect(); |
| 88 | + |
| 89 | + return () => { |
| 90 | + if (reconnectTimer) window.clearTimeout(reconnectTimer); |
| 91 | + if (es) es.close(); |
| 92 | + setConnected(false); |
| 93 | + }; |
| 94 | + }, [userPath]); |
| 95 | + |
| 96 | + const filtered = React.useMemo(() => { |
| 97 | + if (!selectedBayDbId && !selectedBayId) return allEvents; |
| 98 | + return allEvents.filter(e => { |
| 99 | + const bayId = getBayIdFromEvent(e); |
| 100 | + if (!bayId) return false; |
| 101 | + if (selectedBayId && String(bayId) === String(selectedBayId)) return true; |
| 102 | + if (selectedBayDbId && String(bayId) === String(selectedBayDbId)) return true; |
| 103 | + return false; |
| 104 | + }); |
| 105 | + }, [allEvents, selectedBayDbId, selectedBayId]); |
| 106 | + |
| 107 | + // ensure selected item is visible |
| 108 | + React.useEffect(() => { |
| 109 | + if (selectedIndex === null) return; |
| 110 | + const el = listRef.current?.children[selectedIndex] as HTMLElement | undefined; |
| 111 | + if (el && typeof el.scrollIntoView === 'function') { |
| 112 | + el.scrollIntoView({ block: 'nearest', inline: 'nearest' }); |
| 113 | + } |
| 114 | + }, [selectedIndex, filtered]); |
| 115 | + |
| 116 | + const select = (idx: number) => { |
| 117 | + setSelectedIndex(idx); |
| 118 | + }; |
| 119 | + |
| 120 | + const onListKeyDown = (ev: React.KeyboardEvent) => { |
| 121 | + if (filtered.length === 0) return; |
| 122 | + if (ev.key === 'ArrowDown') { |
| 123 | + ev.preventDefault(); |
| 124 | + if (selectedIndex === null) setSelectedIndex(0); |
| 125 | + else setSelectedIndex(Math.min(filtered.length - 1, selectedIndex + 1)); |
| 126 | + } else if (ev.key === 'ArrowUp') { |
| 127 | + ev.preventDefault(); |
| 128 | + if (selectedIndex === null) setSelectedIndex(filtered.length - 1); |
| 129 | + else setSelectedIndex(Math.max(0, selectedIndex - 1)); |
| 130 | + } |
| 131 | + }; |
| 132 | + |
| 133 | + const selectedEvent = selectedIndex === null ? null : filtered[selectedIndex]; |
| 134 | + |
| 135 | + return ( |
| 136 | + <div className="webhook-inspector"> |
| 137 | + <div className="webhook-inspector-list" tabIndex={0} onKeyDown={onListKeyDown}> |
| 138 | + <div className="webhook-events-header"> |
| 139 | + <strong>Events</strong> |
| 140 | + <span className={`webhook-events-status ${connected ? 'live' : ''}`}>{connected ? 'live' : 'disconnected'}</span> |
| 141 | + </div> |
| 142 | + <ul className="webhook-events-ul" ref={listRef}> |
| 143 | + {filtered.length === 0 ? ( |
| 144 | + <li className="no-events">No events yet.</li> |
| 145 | + ) : ( |
| 146 | + filtered.map((e, idx) => ( |
| 147 | + <li key={e.id || idx} className={`webhook-event-item ${selectedIndex === idx ? 'selected' : ''}`} onClick={() => select(idx)}> |
| 148 | + <div className="event-type">{e.eventType}</div> |
| 149 | + <div className="event-meta">{new Date(e.timestamp).toLocaleString()}</div> |
| 150 | + <div className="event-bay">{getBayIdFromEvent(e) ? `Bay: ${getBayIdFromEvent(e)}` : ''}</div> |
| 151 | + </li> |
| 152 | + )) |
| 153 | + )} |
| 154 | + </ul> |
| 155 | + </div> |
| 156 | + <div className="webhook-inspector-preview"> |
| 157 | + {selectedEvent ? ( |
| 158 | + <div> |
| 159 | + <h4 className="preview-title">{selectedEvent.eventType}</h4> |
| 160 | + <div className="preview-time">{new Date(selectedEvent.timestamp).toLocaleString()}</div> |
| 161 | + {/* Version 1: render JSON fallback of event.data or raw */} |
| 162 | + <pre className="preview-json">{JSON.stringify(selectedEvent.data || selectedEvent.raw || {}, null, 2)}</pre> |
| 163 | + </div> |
| 164 | + ) : ( |
| 165 | + <div className="preview-empty">Select an event to preview</div> |
| 166 | + )} |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + ); |
| 170 | +}; |
| 171 | + |
| 172 | +export default WebhookInspector; |
0 commit comments