|
| 1 | +/** |
| 2 | + * EventSparkline — inline-SVG sparkline of events/hour for the tray. |
| 3 | + * |
| 4 | + * Takes a `TimelineEntry[]` (newest-first, from `getSessionTimeline()`), |
| 5 | + * buckets into hourly counts over the last 48 hours, and draws a |
| 6 | + * 220×36 SVG line against the `--accent` token so theme swaps repaint |
| 7 | + * automatically. No chart library — the tray binary already carries |
| 8 | + * enough weight. |
| 9 | + */ |
| 10 | + |
| 11 | +import type { TimelineEntry } from '../api'; |
| 12 | + |
| 13 | +const W = 220; |
| 14 | +const H = 36; |
| 15 | +const BUCKETS = 48; // hours |
| 16 | + |
| 17 | +function bucketize(entries: TimelineEntry[]): number[] { |
| 18 | + const now = Date.now(); |
| 19 | + const hourMs = 60 * 60 * 1000; |
| 20 | + const buckets = new Array<number>(BUCKETS).fill(0); |
| 21 | + for (const e of entries) { |
| 22 | + const ts = Date.parse(e.timestamp); |
| 23 | + if (!Number.isFinite(ts)) continue; |
| 24 | + const age = now - ts; |
| 25 | + if (age < 0 || age >= BUCKETS * hourMs) continue; |
| 26 | + const idx = BUCKETS - 1 - Math.floor(age / hourMs); |
| 27 | + if (idx >= 0 && idx < BUCKETS) buckets[idx] += 1; |
| 28 | + } |
| 29 | + return buckets; |
| 30 | +} |
| 31 | + |
| 32 | +function buildPath(series: number[]): string { |
| 33 | + if (series.length === 0) return ''; |
| 34 | + const max = Math.max(...series, 1); |
| 35 | + const stepX = W / Math.max(series.length - 1, 1); |
| 36 | + return series |
| 37 | + .map((v, i) => { |
| 38 | + const x = i * stepX; |
| 39 | + const y = H - (v / max) * (H - 2) - 1; |
| 40 | + return `${i === 0 ? 'M' : 'L'}${x.toFixed(1)},${y.toFixed(1)}`; |
| 41 | + }) |
| 42 | + .join(' '); |
| 43 | +} |
| 44 | + |
| 45 | +function buildArea(series: number[]): string { |
| 46 | + const line = buildPath(series); |
| 47 | + if (line.length === 0) return ''; |
| 48 | + return `${line} L${W.toFixed(1)},${H.toFixed(1)} L0,${H.toFixed(1)} Z`; |
| 49 | +} |
| 50 | + |
| 51 | +export interface EventSparklineProps { |
| 52 | + entries: TimelineEntry[]; |
| 53 | +} |
| 54 | + |
| 55 | +export function EventSparkline({ entries }: EventSparklineProps) { |
| 56 | + const series = bucketize(entries); |
| 57 | + const total = series.reduce((s, v) => s + v, 0); |
| 58 | + const hasData = total > 0; |
| 59 | + const peak = Math.max(...series); |
| 60 | + |
| 61 | + return ( |
| 62 | + <div |
| 63 | + style={{ display: 'flex', flexDirection: 'column', gap: 4 }} |
| 64 | + role="group" |
| 65 | + aria-label={`${total} events in the last 48 hours`} |
| 66 | + > |
| 67 | + <div |
| 68 | + style={{ |
| 69 | + display: 'flex', |
| 70 | + alignItems: 'baseline', |
| 71 | + gap: 8, |
| 72 | + fontSize: 12, |
| 73 | + }} |
| 74 | + > |
| 75 | + <span |
| 76 | + style={{ |
| 77 | + fontFamily: 'var(--font-mono)', |
| 78 | + fontSize: 16, |
| 79 | + fontWeight: 600, |
| 80 | + color: 'var(--fg)', |
| 81 | + }} |
| 82 | + > |
| 83 | + {total.toLocaleString()} |
| 84 | + </span> |
| 85 | + <span |
| 86 | + style={{ |
| 87 | + color: 'var(--fg-muted)', |
| 88 | + textTransform: 'uppercase', |
| 89 | + letterSpacing: '0.06em', |
| 90 | + }} |
| 91 | + > |
| 92 | + events · 48h |
| 93 | + </span> |
| 94 | + {hasData ? ( |
| 95 | + <span |
| 96 | + style={{ marginLeft: 'auto', color: 'var(--fg-dim)', fontSize: 11 }} |
| 97 | + > |
| 98 | + peak {peak}/h |
| 99 | + </span> |
| 100 | + ) : null} |
| 101 | + </div> |
| 102 | + {hasData ? ( |
| 103 | + <svg |
| 104 | + width={W} |
| 105 | + height={H} |
| 106 | + viewBox={`0 0 ${W} ${H}`} |
| 107 | + style={{ display: 'block', width: '100%', maxWidth: W }} |
| 108 | + role="img" |
| 109 | + aria-label={`Hourly event count over the last ${BUCKETS} hours`} |
| 110 | + > |
| 111 | + <path d={buildArea(series)} fill="var(--accent-soft)" stroke="none" /> |
| 112 | + <path |
| 113 | + d={buildPath(series)} |
| 114 | + fill="none" |
| 115 | + stroke="var(--accent)" |
| 116 | + strokeWidth={1.5} |
| 117 | + strokeLinecap="round" |
| 118 | + strokeLinejoin="round" |
| 119 | + /> |
| 120 | + </svg> |
| 121 | + ) : ( |
| 122 | + <div |
| 123 | + style={{ height: H, background: 'var(--surface-2)', borderRadius: 4 }} |
| 124 | + aria-hidden="true" |
| 125 | + /> |
| 126 | + )} |
| 127 | + </div> |
| 128 | + ); |
| 129 | +} |
0 commit comments