-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeline.tsx
More file actions
251 lines (218 loc) · 8.3 KB
/
timeline.tsx
File metadata and controls
251 lines (218 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
'use client'
import { useVirtualizer } from '@tanstack/react-virtual'
import { CircleDot, GitBranch } from 'lucide-react'
import { useEffect, useMemo, useRef, useState } from 'react'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import type { Job, JobStep } from '@/lib/api'
import { calculateDurationSeconds, formatDurationSeconds } from '@/lib/duration'
interface TimelineItem {
id: string
name: string
type: 'job' | 'step'
startedAt: Date | string | number | null
finishedAt: Date | string | number | null | undefined
status: string
level: number
}
interface TimelineProps {
job: Job | null
steps: Omit<JobStep, 'output'>[]
selectedStepId?: string | null
onStepSelect?: (stepId: string) => void
}
const ROW_HEIGHT = 48
export function Timeline({ job, steps, selectedStepId, onStepSelect }: TimelineProps) {
const parentRef = useRef<HTMLDivElement>(null)
// Build timeline items from job and steps
const timelineItems = useMemo<TimelineItem[]>(() => {
if (!job) {
return []
}
const items: TimelineItem[] = []
// Add job as root item
items.push({
id: job.id,
name: job.actionName,
type: 'job',
startedAt: job.startedAt,
finishedAt: job.finishedAt,
status: job.status,
level: 0,
})
// Add steps as children
const sortedSteps = [...steps].sort((a, b) => {
const aStart = a.startedAt ? new Date(a.startedAt).getTime() : 0
const bStart = b.startedAt ? new Date(b.startedAt).getTime() : 0
return aStart - bStart
})
sortedSteps.forEach((step) => {
items.push({
id: step.id,
name: step.name,
type: 'step',
startedAt: step.startedAt,
finishedAt: step.finishedAt,
status: step.status,
level: 1,
})
})
return items
}, [job, steps])
// Calculate timeline bounds (earliest start, latest end)
const timelineBounds = useMemo(() => {
if (timelineItems.length === 0 || !job?.startedAt) {
return { startTime: 0, endTime: 1, totalDuration: 1 }
}
const jobStartTime = new Date(job.startedAt).getTime()
let earliestStart = jobStartTime
let latestEnd = jobStartTime
timelineItems.forEach((item) => {
if (item.startedAt) {
const startTime = new Date(item.startedAt).getTime()
if (startTime < earliestStart) {
earliestStart = startTime
}
const endTime = item.finishedAt ? new Date(item.finishedAt).getTime() : Date.now()
if (endTime > latestEnd) {
latestEnd = endTime
}
}
})
const totalDuration = (latestEnd - earliestStart) / 1000 // Convert to seconds
return {
startTime: earliestStart,
endTime: latestEnd,
totalDuration: totalDuration || 1,
}
}, [timelineItems, job])
const virtualizer = useVirtualizer({
count: timelineItems.length,
getScrollElement: () => parentRef.current,
estimateSize: () => ROW_HEIGHT,
overscan: 10,
})
// Update durations for active items
const [_, setNow] = useState(Date.now())
useEffect(() => {
const hasActiveItems = timelineItems.some((item) => item.startedAt && !item.finishedAt && item.status === 'active')
if (!hasActiveItems) {
return
}
const interval = setInterval(() => {
setNow(Date.now())
}, 100)
return () => clearInterval(interval)
}, [timelineItems])
if (timelineItems.length === 0) {
return (
<div className="flex items-center justify-center h-full text-muted-foreground">No timeline data available</div>
)
}
return (
<div className="h-full flex flex-col">
<div className="flex-1 overflow-auto" ref={parentRef}>
<div
style={{
height: `${virtualizer.getTotalSize()}px`,
width: '100%',
position: 'relative',
}}
>
{virtualizer.getVirtualItems().map((virtualItem) => {
const item = timelineItems[virtualItem.index]!
const duration = calculateDurationSeconds(item.startedAt, item.finishedAt)
const isActive = item.startedAt && !item.finishedAt && item.status === 'active'
// Calculate relative position and width
let leftPercentage = 0
let widthPercentage = 0
if (item.startedAt && timelineBounds.totalDuration > 0) {
const itemStartTime = new Date(item.startedAt).getTime()
const relativeStart = (itemStartTime - timelineBounds.startTime) / 1000 // seconds from timeline start
leftPercentage = (relativeStart / timelineBounds.totalDuration) * 100
// Width is based on duration relative to total timeline duration
widthPercentage = (duration / timelineBounds.totalDuration) * 100
// Ensure bar doesn't go outside bounds
if (leftPercentage < 0) {
widthPercentage += leftPercentage
leftPercentage = 0
}
if (leftPercentage + widthPercentage > 100) {
widthPercentage = 100 - leftPercentage
}
}
const isSelected = item.type === 'step' && item.id === selectedStepId
const isClickable = item.type === 'step' && onStepSelect
const content = (
<div key={`content-${item.id}`} className="flex items-center w-full px-6 py-3 min-w-0 gap-4">
{/* Left side: Tree structure with icons and labels - fixed width */}
<div
className="flex items-center gap-3 min-w-0 flex-[0_0_300px]"
style={{ paddingLeft: `${item.level * 20}px` }}
>
{item.type === 'job' ? (
<GitBranch className="h-4 w-4 text-teal-500 shrink-0" />
) : (
<CircleDot className="h-4 w-4 text-teal-500 shrink-0" />
)}
<Tooltip>
<TooltipTrigger asChild={true}>
<span className="text-sm font-medium text-foreground truncate block min-w-0">{item.name}</span>
</TooltipTrigger>
<TooltipContent>
<p>{item.name}</p>
</TooltipContent>
</Tooltip>
</div>
{/* Right side: Duration and progress bar - takes remaining space */}
<div className="flex items-center gap-4 flex-1 min-w-0">
<div className="text-sm text-muted-foreground min-w-[90px] text-right font-mono shrink-0">
{formatDurationSeconds(duration)}
</div>
<div className="flex-1 h-3 bg-muted/50 rounded-sm overflow-hidden relative min-w-0">
{widthPercentage > 0 && (
<div
className={`h-full absolute transition-all duration-100 ${
isActive ? 'bg-teal-500' : duration > 0 ? 'bg-teal-500/80' : 'bg-muted'
}`}
style={{
left: `${leftPercentage}%`,
width: `${Math.max(widthPercentage, 0.5)}%`,
}}
/>
)}
</div>
</div>
</div>
)
const containerStyle = {
position: 'absolute' as const,
top: 0,
left: 0,
width: '100%',
height: `${virtualItem.size}px`,
transform: `translateY(${virtualItem.start}px)`,
}
const containerClassName = `flex items-center border-b border-border/50 transition-colors ${
isSelected ? 'bg-muted' : 'hover:bg-muted/50'
}`
return isClickable ? (
<button
key={item.id}
type="button"
style={containerStyle}
onClick={() => onStepSelect(item.id)}
className={`${containerClassName} cursor-pointer w-full text-left`}
>
{content}
</button>
) : (
<div key={item.id} style={containerStyle} className={containerClassName}>
{content}
</div>
)
})}
</div>
</div>
</div>
)
}