|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { CaptureResult } from 'posthog-js' |
| 4 | +import { useState, useEffect } from 'react' |
| 5 | + |
| 6 | +interface EventDisplayProps { |
| 7 | + events: CaptureResult[] |
| 8 | +} |
| 9 | + |
| 10 | +interface EventWithTimestamp extends CaptureResult { |
| 11 | + capturedAt: number |
| 12 | +} |
| 13 | + |
| 14 | +export function EventDisplay({ events }: EventDisplayProps) { |
| 15 | + const [expandedEvents, setExpandedEvents] = useState<Set<string>>(new Set()) |
| 16 | + const [eventsWithTimestamp, setEventsWithTimestamp] = useState<EventWithTimestamp[]>([]) |
| 17 | + const [, setTick] = useState(0) |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + const newEvents = events.filter((e) => !eventsWithTimestamp.find((existing) => existing.uuid === e.uuid)) |
| 21 | + if (newEvents.length > 0) { |
| 22 | + setEventsWithTimestamp((prev) => [...prev, ...newEvents.map((e) => ({ ...e, capturedAt: Date.now() }))]) |
| 23 | + } |
| 24 | + }, [events, eventsWithTimestamp]) |
| 25 | + |
| 26 | + useEffect(() => { |
| 27 | + const interval = setInterval(() => setTick((t) => t + 1), 1000) |
| 28 | + return () => clearInterval(interval) |
| 29 | + }, []) |
| 30 | + |
| 31 | + const toggleExpanded = (uuid: string) => { |
| 32 | + setExpandedEvents((prev) => { |
| 33 | + const next = new Set(prev) |
| 34 | + if (next.has(uuid)) { |
| 35 | + next.delete(uuid) |
| 36 | + } else { |
| 37 | + next.add(uuid) |
| 38 | + } |
| 39 | + return next |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + const getTimeAgo = (timestamp: number) => { |
| 44 | + const seconds = Math.floor((Date.now() - timestamp) / 1000) |
| 45 | + if (seconds < 60) return `${seconds}s ago` |
| 46 | + const minutes = Math.floor(seconds / 60) |
| 47 | + if (minutes < 60) return `${minutes}m ago` |
| 48 | + const hours = Math.floor(minutes / 60) |
| 49 | + return `${hours}h ago` |
| 50 | + } |
| 51 | + |
| 52 | + return ( |
| 53 | + <div |
| 54 | + style={{ |
| 55 | + position: 'fixed', |
| 56 | + top: '1rem', |
| 57 | + right: '1rem', |
| 58 | + width: '320px', |
| 59 | + maxHeight: '400px', |
| 60 | + overflowY: 'scroll', |
| 61 | + backgroundColor: 'white', |
| 62 | + border: '2px solid #d1d5db', |
| 63 | + borderRadius: '8px', |
| 64 | + boxShadow: '0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05)', |
| 65 | + padding: '1rem', |
| 66 | + zIndex: 50, |
| 67 | + }} |
| 68 | + > |
| 69 | + <h2 style={{ fontSize: '1.125rem', fontWeight: 'bold', marginBottom: '0.75rem', color: '#1f2937' }}> |
| 70 | + PostHog Events |
| 71 | + </h2> |
| 72 | + {eventsWithTimestamp.length === 0 ? ( |
| 73 | + <p style={{ color: '#6b7280', fontSize: '0.875rem' }}>No events captured yet...</p> |
| 74 | + ) : ( |
| 75 | + <div style={{ display: 'flex', flexDirection: 'column', gap: '0.5rem' }}> |
| 76 | + {eventsWithTimestamp.map((event) => ( |
| 77 | + <div |
| 78 | + key={event.uuid} |
| 79 | + style={{ |
| 80 | + backgroundColor: '#f9fafb', |
| 81 | + border: '1px solid #e5e7eb', |
| 82 | + borderRadius: '4px', |
| 83 | + padding: '0.5rem', |
| 84 | + fontSize: '0.75rem', |
| 85 | + cursor: 'pointer', |
| 86 | + }} |
| 87 | + onClick={() => toggleExpanded(event.uuid)} |
| 88 | + > |
| 89 | + <div |
| 90 | + style={{ |
| 91 | + display: 'flex', |
| 92 | + justifyContent: 'space-between', |
| 93 | + alignItems: 'center', |
| 94 | + }} |
| 95 | + > |
| 96 | + <div style={{ fontWeight: '600', color: '#2563eb' }}>{event.event}</div> |
| 97 | + <div style={{ color: '#9ca3af', fontSize: '0.65rem' }}> |
| 98 | + {getTimeAgo(event.capturedAt)} |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + {expandedEvents.has(event.uuid) && ( |
| 102 | + <div |
| 103 | + style={{ |
| 104 | + marginTop: '0.5rem', |
| 105 | + paddingTop: '0.5rem', |
| 106 | + borderTop: '1px solid #e5e7eb', |
| 107 | + }} |
| 108 | + > |
| 109 | + <pre |
| 110 | + style={{ |
| 111 | + fontSize: '0.65rem', |
| 112 | + color: '#374151', |
| 113 | + whiteSpace: 'pre-wrap', |
| 114 | + wordBreak: 'break-word', |
| 115 | + }} |
| 116 | + > |
| 117 | + {JSON.stringify(event.properties, null, 2)} |
| 118 | + </pre> |
| 119 | + </div> |
| 120 | + )} |
| 121 | + </div> |
| 122 | + ))} |
| 123 | + </div> |
| 124 | + )} |
| 125 | + </div> |
| 126 | + ) |
| 127 | +} |
0 commit comments