-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchStatusUpdateDialog.tsx
More file actions
381 lines (344 loc) · 12.8 KB
/
Copy pathBatchStatusUpdateDialog.tsx
File metadata and controls
381 lines (344 loc) · 12.8 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { useState, useEffect, useCallback, useRef } from 'react';
import {
Loader2,
CheckCircle2,
XCircle,
} from 'lucide-react';
import { useTranslation } from 'react-i18next';
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from './ui/dialog';
import { Button } from './ui/button';
import { Label } from './ui/label';
import { Progress } from './ui/progress';
import { ScrollArea } from './ui/scroll-area';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from './ui/select';
import type { Task, TaskStatus } from '../../shared/types';
import { useQuickActionsStore } from '../stores/quick-actions-store';
import { useProjectStore } from '../stores/project-store';
/**
* Result for a single task in the bulk status update
*/
interface TaskStatusResult {
taskId: string;
taskTitle: string;
status: 'pending' | 'updating' | 'success' | 'error';
error?: string;
}
interface BatchStatusUpdateDialogProps {
open: boolean;
tasks: Task[];
onOpenChange: (open: boolean) => void;
onComplete?: () => void;
}
/**
* Dialog for updating status of multiple tasks in bulk
* Shows progress tracking and results per task
*/
export function BatchStatusUpdateDialog({
open,
tasks,
onOpenChange,
onComplete
}: BatchStatusUpdateDialogProps) {
const { t } = useTranslation(['tasks', 'common']);
const addRecentAction = useQuickActionsStore((state) => state.addRecentAction);
const selectedProjectId = useProjectStore((state) => (state.activeProjectId || state.selectedProjectId) ?? undefined);
// Common options for all status updates
const [newStatus, setNewStatus] = useState<TaskStatus | ''>('');
// Progress tracking
const [step, setStep] = useState<'options' | 'updating' | 'results'>('options');
const [taskResults, setTaskResults] = useState<TaskStatusResult[]>([]);
const [currentIndex, setCurrentIndex] = useState(0);
const isCancelledRef = useRef(false);
const prevOpenRef = useRef(open);
// Only reset when transitioning closed→open (not on tasks array changes during async operation)
useEffect(() => {
const wasOpen = prevOpenRef.current;
prevOpenRef.current = open;
if (open && !wasOpen) {
setNewStatus('');
setStep('options');
setCurrentIndex(0);
isCancelledRef.current = false;
setTaskResults(tasks.map(task => ({
taskId: task.id,
taskTitle: task.title,
status: 'pending'
})));
}
}, [open, tasks]);
const handleStatusUpdate = useCallback(async () => {
if (!newStatus) {
return;
}
setStep('updating');
isCancelledRef.current = false;
const results: TaskStatusResult[] = tasks.map(task => ({
taskId: task.id,
taskTitle: task.title,
status: 'pending' as const
}));
setTaskResults(results);
for (let i = 0; i < tasks.length; i++) {
if (isCancelledRef.current) break;
setCurrentIndex(i);
setTaskResults(prev => prev.map((r, idx) =>
idx === i ? { ...r, status: 'updating' as const } : r
));
try {
const updateResult = await window.electronAPI?.updateTaskStatus(tasks[i].id, newStatus);
if (isCancelledRef.current) break;
if (updateResult?.success) {
setTaskResults(prev => prev.map((r, idx) =>
idx === i ? {
...r,
status: 'success' as const
} : r
));
} else {
setTaskResults(prev => prev.map((r, idx) =>
idx === i ? {
...r,
status: 'error' as const,
error: updateResult?.error || t('tasks:batchStatusUpdate.errors.unknown')
} : r
));
}
} catch (err) {
if (isCancelledRef.current) break;
setTaskResults(prev => prev.map((r, idx) =>
idx === i ? {
...r,
status: 'error' as const,
error: err instanceof Error ? err.message : t('tasks:batchStatusUpdate.errors.unknown')
} : r
));
}
}
if (!isCancelledRef.current) {
// Add to recent actions when batch status update completes
addRecentAction({
type: 'batch_status_update',
label: 'Batch Status Update',
itemCount: tasks.length,
targetStatus: newStatus,
projectId: selectedProjectId
});
setStep('results');
}
}, [tasks, newStatus, t, addRecentAction, selectedProjectId]);
const handleClose = () => {
isCancelledRef.current = true;
if (step === 'results' && onComplete) {
onComplete();
}
onOpenChange(false);
};
// Calculate progress
const completedCount = taskResults.filter(r => r.status === 'success' || r.status === 'error').length;
const successCount = taskResults.filter(r => r.status === 'success').length;
const errorCount = taskResults.filter(r => r.status === 'error').length;
const progress = tasks.length > 0 ? (completedCount / tasks.length) * 100 : 0;
// Available status options (exclude 'error' as it's set automatically)
const statusOptions: { value: TaskStatus; label: string }[] = [
{ value: 'backlog', label: t('tasks:status.backlog') },
{ value: 'queue', label: t('tasks:status.queue') },
{ value: 'in_progress', label: t('tasks:status.in_progress') },
{ value: 'ai_review', label: t('tasks:status.ai_review') },
{ value: 'human_review', label: t('tasks:status.human_review') },
{ value: 'done', label: t('tasks:status.done') },
{ value: 'pr_created', label: t('tasks:status.pr_created') }
];
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[600px]">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
{t('tasks:batchStatusUpdate.title')}
</DialogTitle>
<DialogDescription>
{step === 'options' && t('tasks:batchStatusUpdate.description', { count: tasks.length })}
{step === 'updating' && t('tasks:batchStatusUpdate.updating', { current: currentIndex + 1, total: tasks.length })}
{step === 'results' && t('tasks:batchStatusUpdate.resultsDescription', { success: successCount, failed: errorCount })}
</DialogDescription>
</DialogHeader>
{/* Options Step */}
{step === 'options' && (
<div className="space-y-4">
{/* Task List Preview */}
<div className="space-y-2">
<Label>{t('tasks:batchStatusUpdate.tasksToUpdate')}</Label>
<ScrollArea className="h-32 rounded-md border border-border p-2">
<div className="space-y-1">
{tasks.map((task, idx) => (
<div
key={task.id}
className="flex items-center gap-2 text-sm py-1 px-2 rounded hover:bg-muted/50"
>
<span className="text-muted-foreground">{idx + 1}.</span>
<span className="truncate">{task.title}</span>
</div>
))}
</div>
</ScrollArea>
</div>
{/* Status Selection */}
<div className="space-y-4 pt-2">
<div className="space-y-2">
<Label htmlFor="batchStatus">{t('tasks:batchStatusUpdate.newStatus')}</Label>
<Select value={newStatus} onValueChange={(value) => setNewStatus(value as TaskStatus)}>
<SelectTrigger id="batchStatus">
<SelectValue placeholder={t('tasks:batchStatusUpdate.selectStatus')} />
</SelectTrigger>
<SelectContent>
{statusOptions.map(option => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
<p className="text-xs text-muted-foreground">
{t('tasks:batchStatusUpdate.statusHint')}
</p>
</div>
</div>
<DialogFooter>
<Button variant="outline" onClick={handleClose}>
{t('common:buttons.cancel')}
</Button>
<Button onClick={handleStatusUpdate} disabled={tasks.length === 0 || !newStatus || step !== 'options'}>
{t('tasks:batchStatusUpdate.updateAll', { count: tasks.length })}
</Button>
</DialogFooter>
</div>
)}
{/* Updating Step */}
{step === 'updating' && (
<div className="space-y-4">
<div className="flex flex-col items-center justify-center py-4 space-y-4">
<Loader2 className="h-10 w-10 text-primary animate-spin" />
<div className="text-center space-y-1">
<p className="text-sm font-medium">
{t('tasks:batchStatusUpdate.updatingStatus', { current: currentIndex + 1, total: tasks.length })}
</p>
<p className="text-xs text-muted-foreground truncate max-w-[400px]">
{tasks[currentIndex]?.title}
</p>
</div>
</div>
<div className="space-y-2">
<Progress value={progress} />
<p className="text-xs text-center text-muted-foreground">
{completedCount} / {tasks.length} {t('tasks:batchStatusUpdate.completed')}
</p>
</div>
{/* Task Status List */}
<ScrollArea className="h-40 rounded-md border border-border">
<div className="p-2 space-y-1">
{taskResults.map((result, idx) => (
<StatusResultRow key={result.taskId} result={result} index={idx} />
))}
</div>
</ScrollArea>
</div>
)}
{/* Results Step */}
{step === 'results' && (
<div className="space-y-4">
{/* Summary */}
<div className="flex items-center justify-center gap-6 py-4">
{successCount > 0 && (
<div className="flex items-center gap-2 text-success">
<CheckCircle2 className="h-5 w-5" />
<span className="font-medium">{successCount} {t('tasks:batchStatusUpdate.succeeded')}</span>
</div>
)}
{errorCount > 0 && (
<div className="flex items-center gap-2 text-destructive">
<XCircle className="h-5 w-5" />
<span className="font-medium">{errorCount} {t('tasks:batchStatusUpdate.failed')}</span>
</div>
)}
</div>
{/* Results List */}
<ScrollArea className="h-56 rounded-md border border-border">
<div className="p-2 space-y-2">
{taskResults.map((result, idx) => (
<StatusResultRow
key={result.taskId}
result={result}
index={idx}
showDetails
/>
))}
</div>
</ScrollArea>
<DialogFooter>
<Button onClick={handleClose}>
{t('common:buttons.close')}
</Button>
</DialogFooter>
</div>
)}
</DialogContent>
</Dialog>
);
}
/**
* Individual task result row component
*/
interface StatusResultRowProps {
result: TaskStatusResult;
index: number;
showDetails?: boolean;
}
function StatusResultRow({ result, index, showDetails }: StatusResultRowProps) {
const getStatusIcon = () => {
switch (result.status) {
case 'pending':
return <div className="h-4 w-4 rounded-full border-2 border-muted-foreground/30" />;
case 'updating':
return <Loader2 className="h-4 w-4 text-primary animate-spin" />;
case 'success':
return <CheckCircle2 className="h-4 w-4 text-success" />;
case 'error':
return <XCircle className="h-4 w-4 text-destructive" />;
}
};
return (
<div
className={`flex items-start gap-2 p-2 rounded text-sm ${
result.status === 'updating' ? 'bg-primary/5' : ''
}`}
>
<div className="flex-shrink-0 mt-0.5">
{getStatusIcon()}
</div>
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2">
<span className="text-muted-foreground text-xs">{index + 1}.</span>
<span className="truncate font-medium">{result.taskTitle}</span>
</div>
{showDetails && result.status === 'error' && result.error && (
<div className="flex items-start gap-1 mt-1">
<XCircle className="h-3 w-3 text-destructive flex-shrink-0 mt-0.5" />
<span className="text-xs text-destructive">{result.error}</span>
</div>
)}
</div>
</div>
);
}