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