|
| 1 | +<script lang="ts"> |
| 2 | + import type { createFlowState } from '$lib/stores/pgflow-state.svelte'; |
| 3 | + import { Card, CardContent, CardHeader, CardTitle } from '$lib/components/ui/card'; |
| 4 | + import { Play, CheckCircle2 } from '@lucide/svelte'; |
| 5 | + import { codeToHtml } from 'shiki'; |
| 6 | +
|
| 7 | + interface Props { |
| 8 | + flowState: ReturnType<typeof createFlowState>; |
| 9 | + } |
| 10 | +
|
| 11 | + let { flowState }: Props = $props(); |
| 12 | +
|
| 13 | + let expandedEventIdx = $state<number | null>(null); |
| 14 | + let highlightedEventJson = $state<Record<number, string>>({}); |
| 15 | +
|
| 16 | + // Helper to get short step name |
| 17 | + function getShortStepName(stepSlug: string): string { |
| 18 | + const shortNames: Record<string, string> = { |
| 19 | + fetchArticle: 'fetch', |
| 20 | + summarize: 'summ', |
| 21 | + extractKeywords: 'kwrds', |
| 22 | + publish: 'pub' |
| 23 | + }; |
| 24 | + return shortNames[stepSlug] || stepSlug.slice(0, 5); |
| 25 | + } |
| 26 | +
|
| 27 | + // Helper to get event badge info |
| 28 | + function getEventBadgeInfo(event: { |
| 29 | + event_type: string; |
| 30 | + step_slug?: string; |
| 31 | + }): { |
| 32 | + icon: typeof Play | typeof CheckCircle2; |
| 33 | + color: string; |
| 34 | + text: string; |
| 35 | + } | null { |
| 36 | + if (event.event_type === 'step:started' && event.step_slug) { |
| 37 | + return { |
| 38 | + icon: Play, |
| 39 | + color: 'blue', |
| 40 | + text: getShortStepName(event.step_slug) |
| 41 | + }; |
| 42 | + } |
| 43 | + if (event.event_type === 'step:completed' && event.step_slug) { |
| 44 | + return { |
| 45 | + icon: CheckCircle2, |
| 46 | + color: 'green', |
| 47 | + text: getShortStepName(event.step_slug) |
| 48 | + }; |
| 49 | + } |
| 50 | + return null; |
| 51 | + } |
| 52 | +
|
| 53 | + // Get displayable events (started/completed steps only) |
| 54 | + const displayableEvents = $derived( |
| 55 | + flowState.timeline |
| 56 | + .map((e, idx) => ({ event: e, badge: getEventBadgeInfo(e), idx })) |
| 57 | + .filter((e) => e.badge !== null) |
| 58 | + ); |
| 59 | +
|
| 60 | + // Truncate deep function |
| 61 | + function truncateDeep(obj: unknown, maxLength = 80): unknown { |
| 62 | + if (typeof obj === 'string') { |
| 63 | + if (obj.length > maxLength) { |
| 64 | + return `<long string: ${obj.length} chars>`; |
| 65 | + } |
| 66 | + return obj; |
| 67 | + } |
| 68 | + if (Array.isArray(obj)) { |
| 69 | + return obj.map((item) => truncateDeep(item, maxLength)); |
| 70 | + } |
| 71 | + if (obj !== null && typeof obj === 'object') { |
| 72 | + const truncated: Record<string, unknown> = {}; |
| 73 | + for (const [key, value] of Object.entries(obj)) { |
| 74 | + truncated[key] = truncateDeep(value, maxLength); |
| 75 | + } |
| 76 | + return truncated; |
| 77 | + } |
| 78 | + return obj; |
| 79 | + } |
| 80 | +
|
| 81 | + async function toggleEventExpanded(idx: number, event: unknown) { |
| 82 | + if (expandedEventIdx === idx) { |
| 83 | + expandedEventIdx = null; |
| 84 | + } else { |
| 85 | + // Generate syntax-highlighted JSON if not already cached |
| 86 | + if (!highlightedEventJson[idx]) { |
| 87 | + const truncated = truncateDeep(event); |
| 88 | + const jsonString = JSON.stringify(truncated, null, 2); |
| 89 | + const html = await codeToHtml(jsonString, { |
| 90 | + lang: 'json', |
| 91 | + theme: 'night-owl' |
| 92 | + }); |
| 93 | + highlightedEventJson = { ...highlightedEventJson, [idx]: html }; |
| 94 | + } |
| 95 | + // Set expanded after HTML is ready |
| 96 | + expandedEventIdx = idx; |
| 97 | + } |
| 98 | + } |
| 99 | +</script> |
| 100 | + |
| 101 | +<Card class="h-full flex flex-col"> |
| 102 | + <CardHeader class="pb-0 pt-2 px-3 flex-shrink-0 relative"> |
| 103 | + <div class="flex items-center justify-between"> |
| 104 | + <CardTitle class="text-sm">Event Stream ({displayableEvents.length})</CardTitle> |
| 105 | + </div> |
| 106 | + </CardHeader> |
| 107 | + <CardContent class="flex-1 overflow-auto pt-1 pb-2 px-3 min-h-0"> |
| 108 | + <div class="space-y-1"> |
| 109 | + {#if displayableEvents.length === 0} |
| 110 | + <p class="text-sm text-muted-foreground text-center py-8"> |
| 111 | + No events yet. Start a flow to see events. |
| 112 | + </p> |
| 113 | + {:else} |
| 114 | + {#each displayableEvents as { badge, event, idx } (idx)} |
| 115 | + {#if badge} |
| 116 | + <button |
| 117 | + onclick={() => toggleEventExpanded(idx, event)} |
| 118 | + class="w-full text-left rounded event-badge-row event-badge-{badge.color} cursor-pointer hover:opacity-80 transition-opacity" |
| 119 | + > |
| 120 | + <div class="flex items-center gap-2 px-2 py-1.5"> |
| 121 | + <svelte:component this={badge.icon} class="w-4 h-4 flex-shrink-0" /> |
| 122 | + <div class="flex-1 min-w-0"> |
| 123 | + <div class="font-medium text-sm">{event.step_slug || 'Unknown'}</div> |
| 124 | + <div class="text-xs opacity-70"> |
| 125 | + {event.event_type.replace('step:', '')} |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + <div class="flex items-center gap-2 flex-shrink-0"> |
| 129 | + <div class="text-xs opacity-70 font-mono text-muted-foreground"> |
| 130 | + {event.deltaDisplay} |
| 131 | + </div> |
| 132 | + <div class="text-xs opacity-50"> |
| 133 | + {expandedEventIdx === idx ? '▼' : '▶'} |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + </div> |
| 137 | + |
| 138 | + {#if expandedEventIdx === idx && highlightedEventJson[idx]} |
| 139 | + <div class="px-2 pb-1.5" onclick={(e) => e.stopPropagation()}> |
| 140 | + <div class="event-json-display rounded overflow-x-auto"> |
| 141 | + <!-- eslint-disable-next-line svelte/no-at-html-tags --> |
| 142 | + {@html highlightedEventJson[idx]} |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + {/if} |
| 146 | + </button> |
| 147 | + {/if} |
| 148 | + {/each} |
| 149 | + {/if} |
| 150 | + </div> |
| 151 | + </CardContent> |
| 152 | +</Card> |
| 153 | + |
| 154 | +<style> |
| 155 | + /* Event badge rows */ |
| 156 | + .event-badge-row { |
| 157 | + border: 1px solid transparent; |
| 158 | + } |
| 159 | +
|
| 160 | + .event-badge-row.event-badge-blue { |
| 161 | + background-color: rgba(59, 91, 219, 0.2); |
| 162 | + border-color: rgba(91, 141, 239, 0.5); |
| 163 | + color: #7ba3f0; |
| 164 | + } |
| 165 | +
|
| 166 | + .event-badge-row.event-badge-green { |
| 167 | + background-color: rgba(23, 122, 81, 0.2); |
| 168 | + border-color: rgba(32, 165, 111, 0.5); |
| 169 | + color: #2ec184; |
| 170 | + } |
| 171 | +
|
| 172 | + /* Event JSON display */ |
| 173 | + .event-json-display { |
| 174 | + max-height: 300px; |
| 175 | + } |
| 176 | +
|
| 177 | + .event-json-display :global(pre) { |
| 178 | + margin: 0 !important; |
| 179 | + padding: 8px 10px !important; |
| 180 | + background: #0d1117 !important; |
| 181 | + border-radius: 4px; |
| 182 | + font-size: 10px; |
| 183 | + line-height: 1.5; |
| 184 | + display: table; |
| 185 | + min-width: 100%; |
| 186 | + } |
| 187 | +
|
| 188 | + .event-json-display :global(code) { |
| 189 | + font-family: 'Fira Code', 'Monaco', 'Menlo', 'Courier New', monospace; |
| 190 | + white-space: pre; |
| 191 | + display: block; |
| 192 | + } |
| 193 | +</style> |
0 commit comments