-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhaseProgressIndicator.tsx
More file actions
337 lines (311 loc) · 13 KB
/
Copy pathPhaseProgressIndicator.tsx
File metadata and controls
337 lines (311 loc) · 13 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { motion, AnimatePresence } from 'motion/react';
import { useTranslation } from 'react-i18next';
import { memo, useRef, useState, useEffect } from 'react';
import { cn } from '../lib/utils';
import type { ExecutionPhase, TaskLogs, Subtask } from '../../shared/types';
interface PhaseProgressIndicatorProps {
phase?: ExecutionPhase;
subtasks: Subtask[];
phaseLogs?: TaskLogs | null;
/** Fallback progress percentage (0-100) when phaseLogs unavailable */
phaseProgress?: number;
isStuck?: boolean;
isRunning?: boolean;
className?: string;
}
// Phase display configuration (colors only - labels are translated)
const PHASE_COLORS: Record<ExecutionPhase, { color: string; bgColor: string }> = {
idle: { color: 'bg-muted-foreground', bgColor: 'bg-muted' },
planning: { color: 'bg-amber-500', bgColor: 'bg-amber-500/20' },
coding: { color: 'bg-info', bgColor: 'bg-info/20' },
test_generation: { color: 'bg-cyan-500', bgColor: 'bg-cyan-500/20' },
qa_review: { color: 'bg-purple-500', bgColor: 'bg-purple-500/20' },
qa_fixing: { color: 'bg-orange-500', bgColor: 'bg-orange-500/20' },
complete: { color: 'bg-success', bgColor: 'bg-success/20' },
failed: { color: 'bg-destructive', bgColor: 'bg-destructive/20' },
};
// Phase label translation keys
const PHASE_LABEL_KEYS: Record<ExecutionPhase, string> = {
idle: 'execution.phases.idle',
planning: 'execution.phases.planning',
coding: 'execution.phases.coding',
test_generation: 'execution.phases.testGeneration',
qa_review: 'execution.phases.reviewing',
qa_fixing: 'execution.phases.fixing',
complete: 'execution.phases.complete',
failed: 'execution.phases.failed',
};
/**
* Smart progress indicator that adapts based on execution phase:
* - Planning/Validation: Shows animated activity bar with entry count
* - Coding: Shows subtask-based percentage progress
* - Stuck: Shows warning state with interrupted animation
*
* Performance: Uses IntersectionObserver to pause animations when not visible
*/
export const PhaseProgressIndicator = memo(function PhaseProgressIndicator({
phase: rawPhase,
subtasks,
phaseLogs,
phaseProgress,
isStuck = false,
isRunning = false,
className,
}: PhaseProgressIndicatorProps) {
const { t } = useTranslation('tasks');
const phase = rawPhase || 'idle';
const containerRef = useRef<HTMLDivElement>(null);
const [isVisible, setIsVisible] = useState(true);
const prevVisibleRef = useRef(true);
// Use IntersectionObserver to pause animations when component is not visible
useEffect(() => {
const element = containerRef.current;
if (!element) return;
const observer = new IntersectionObserver(
([entry]) => {
const nowVisible = entry.isIntersecting;
if (prevVisibleRef.current !== nowVisible && window.DEBUG) {
console.log(`[PhaseProgress] Visibility changed: ${prevVisibleRef.current} -> ${nowVisible}, animations ${nowVisible ? 'resumed' : 'paused'}`);
}
prevVisibleRef.current = nowVisible;
setIsVisible(nowVisible);
},
{ threshold: 0.1 }
);
observer.observe(element);
return () => observer.disconnect();
}, []);
// Only animate when visible and running
const shouldAnimate = isVisible && isRunning && !isStuck;
// Calculate subtask-based progress (for coding phase)
const completedSubtasks = subtasks.filter((c) => c.status === 'completed').length;
const totalSubtasks = subtasks.length;
const subtaskProgress = totalSubtasks > 0 ? Math.round((completedSubtasks / totalSubtasks) * 100) : 0;
// Get log entry counts for activity indication
const planningEntries = phaseLogs?.phases?.planning?.entries?.length || 0;
const codingEntries = phaseLogs?.phases?.coding?.entries?.length || 0;
const validationEntries = phaseLogs?.phases?.validation?.entries?.length || 0;
// Determine which phase log to show activity for
const getActivePhaseEntries = () => {
if (phase === 'planning') return planningEntries;
if (phase === 'qa_review' || phase === 'qa_fixing') return validationEntries;
return codingEntries;
};
// Determine if we should show indeterminate (activity) vs determinate (%) progress
const isIndeterminatePhase = phase === 'planning' || phase === 'qa_review' || phase === 'qa_fixing';
// Show subtask progress whenever subtasks exist (stops pulsing animation when spec completes)
const showSubtaskProgress = totalSubtasks > 0;
const colors = PHASE_COLORS[phase] || PHASE_COLORS.idle;
const phaseLabel = t(PHASE_LABEL_KEYS[phase] || PHASE_LABEL_KEYS.idle);
const activeEntries = getActivePhaseEntries();
return (
<div ref={containerRef} className={cn('space-y-1.5', className)}>
{/* Progress label row */}
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<span className="text-xs text-muted-foreground">
{isStuck ? t('execution.labels.interrupted') : showSubtaskProgress ? t('execution.labels.progress') : phaseLabel}
</span>
{/* Activity indicator dot for non-coding phases - only animate when visible */}
{isRunning && !isStuck && isIndeterminatePhase && (
<motion.div
className={cn('h-1.5 w-1.5 rounded-full', colors.color)}
animate={shouldAnimate ? {
scale: [1, 1.5, 1],
opacity: [1, 0.5, 1],
} : { scale: 1, opacity: 1 }}
transition={shouldAnimate ? {
duration: 1,
repeat: Infinity,
ease: 'easeInOut',
} : undefined}
/>
)}
</div>
<span className="text-xs font-medium text-foreground">
{showSubtaskProgress ? (
`${subtaskProgress}%`
) : activeEntries > 0 ? (
<span className="text-muted-foreground">
{activeEntries} {activeEntries === 1 ? t('execution.labels.entry') : t('execution.labels.entries')}
</span>
) : isRunning && isIndeterminatePhase && (phaseProgress ?? 0) > 0 ? (
`${Math.round(Math.min(phaseProgress as number, 100))}%`
) : (
'—'
)}
</span>
</div>
{/* Progress bar */}
<div
className={cn(
'relative h-1.5 w-full overflow-hidden rounded-full',
isStuck ? 'bg-warning/20' : 'bg-border'
)}
>
<AnimatePresence mode="wait">
{isStuck ? (
// Stuck/Interrupted state - pulsing warning bar (only animate when visible)
<motion.div
key="stuck"
className="absolute inset-0 bg-warning/40"
initial={{ opacity: 0 }}
animate={isVisible ? { opacity: [0.3, 0.6, 0.3] } : { opacity: 0.45 }}
transition={isVisible ? { duration: 2, repeat: Infinity, ease: 'easeInOut' } : undefined}
/>
) : showSubtaskProgress ? (
// Determinate progress for coding phase
<motion.div
key="determinate"
className={cn('h-full rounded-full', colors.color)}
initial={{ width: 0 }}
animate={{ width: `${subtaskProgress}%` }}
transition={{ duration: 0.5, ease: 'easeOut' }}
/>
) : shouldAnimate && isIndeterminatePhase ? (
// Indeterminate animated progress for planning/validation (only when visible)
<motion.div
key="indeterminate"
className={cn('absolute h-full w-1/3 rounded-full', colors.color)}
animate={{
x: ['-100%', '400%'],
}}
transition={{
duration: 1.5,
repeat: Infinity,
ease: 'easeInOut',
}}
/>
) : isRunning && isIndeterminatePhase && !isVisible ? (
// Static placeholder when not visible but running
<motion.div
key="indeterminate-static"
className={cn('absolute h-full w-1/3 rounded-full left-1/3', colors.color)}
/>
) : null}
</AnimatePresence>
</div>
{/* Subtask indicators (only show when subtasks exist) */}
{totalSubtasks > 0 && (
<div className="flex flex-wrap gap-1.5 mt-2">
{subtasks.slice(0, 10).map((subtask, index) => {
const isInProgress = subtask.status === 'in_progress';
const shouldPulse = isInProgress && isVisible;
return (
<motion.div
key={subtask.id || `subtask-${index}`}
className={cn(
'h-2 w-2 rounded-full',
subtask.status === 'completed' && 'bg-success',
isInProgress && 'bg-info',
subtask.status === 'failed' && 'bg-destructive',
subtask.status === 'pending' && 'bg-muted-foreground/30'
)}
initial={{ scale: 0, opacity: 0 }}
animate={{
scale: 1,
opacity: 1,
// Only animate boxShadow when visible to save GPU cycles
...(shouldPulse && {
boxShadow: [
'0 0 0 0 rgba(var(--info), 0.4)',
'0 0 0 4px rgba(var(--info), 0)',
],
}),
}}
transition={{
scale: { delay: index * 0.03, duration: 0.2 },
opacity: { delay: index * 0.03, duration: 0.2 },
// Only repeat animation when visible
boxShadow: shouldPulse
? { duration: 1, repeat: Infinity, ease: 'easeOut' }
: undefined,
}}
title={`${subtask.title || subtask.id}: ${subtask.status}`}
/>
);
})}
{totalSubtasks > 10 && (
<span key="overflow-count" className="text-[10px] text-muted-foreground font-medium ml-0.5">
+{totalSubtasks - 10}
</span>
)}
</div>
)}
{/* Phase steps indicator (shows overall flow) */}
{(isRunning || phase !== 'idle') && (
<PhaseStepsIndicator currentPhase={phase} isStuck={isStuck} isVisible={isVisible} />
)}
</div>
);
});
/**
* Mini phase steps indicator showing the overall flow
*/
const PhaseStepsIndicator = memo(function PhaseStepsIndicator({
currentPhase,
isStuck,
isVisible = true,
}: {
currentPhase: ExecutionPhase;
isStuck: boolean;
isVisible?: boolean;
}) {
const { t } = useTranslation('tasks');
const phases: { key: ExecutionPhase; labelKey: string }[] = [
{ key: 'planning', labelKey: 'execution.shortPhases.plan' },
{ key: 'coding', labelKey: 'execution.shortPhases.code' },
{ key: 'qa_review', labelKey: 'execution.shortPhases.qa' },
];
const getPhaseState = (phaseKey: ExecutionPhase) => {
const phaseOrder = ['planning', 'coding', 'qa_review', 'qa_fixing', 'complete'];
const currentIndex = phaseOrder.indexOf(currentPhase);
const phaseIndex = phaseOrder.indexOf(phaseKey);
if (currentPhase === 'failed') return 'failed';
if (currentPhase === 'complete') return 'complete';
if (phaseKey === currentPhase || (phaseKey === 'qa_review' && currentPhase === 'qa_fixing')) {
return isStuck ? 'stuck' : 'active';
}
if (phaseIndex < currentIndex) return 'complete';
return 'pending';
};
return (
<div className="flex items-center gap-1 mt-2">
{phases.map((phase, index) => {
const state = getPhaseState(phase.key);
const shouldAnimate = state === 'active' && !isStuck && isVisible;
return (
<div key={phase.key} className="flex items-center">
<motion.div
className={cn(
'flex items-center gap-1 px-1.5 py-0.5 rounded text-[9px] font-medium',
state === 'complete' && 'bg-success/10 text-success',
state === 'active' && 'bg-primary/10 text-primary',
state === 'stuck' && 'bg-warning/10 text-warning',
state === 'failed' && 'bg-destructive/10 text-destructive',
state === 'pending' && 'bg-muted text-muted-foreground'
)}
animate={shouldAnimate ? { opacity: [1, 0.6, 1] } : { opacity: 1 }}
transition={shouldAnimate ? { duration: 1.5, repeat: Infinity, ease: 'easeInOut' } : undefined}
>
{state === 'complete' && (
<svg className="h-2.5 w-2.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={3} d="M5 13l4 4L19 7" />
</svg>
)}
{t(phase.labelKey)}
</motion.div>
{index < phases.length - 1 && (
<div
className={cn(
'w-2 h-px mx-0.5',
getPhaseState(phases[index + 1].key) !== 'pending' ? 'bg-success/50' : 'bg-border'
)}
/>
)}
</div>
);
})}
</div>
);
});