-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTaskBoardView.tsx
More file actions
445 lines (403 loc) · 15.8 KB
/
TaskBoardView.tsx
File metadata and controls
445 lines (403 loc) · 15.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
'use client';
import { useState, useCallback, useMemo } from 'react';
import Link from 'next/link';
import { useRouter } from 'next/navigation';
import useSWR from 'swr';
import { TaskBoardContent } from './TaskBoardContent';
import { TaskDetailModal } from './TaskDetailModal';
import { TaskFilters } from './TaskFilters';
import { BatchActionsBar } from './BatchActionsBar';
import { BulkActionConfirmDialog, type BulkActionType } from './BulkActionConfirmDialog';
import { Cancel01Icon, Task01Icon } from '@hugeicons/react';
import { Button } from '@/components/ui/button';
import { tasksApi, prdApi } from '@/lib/api';
import { useRequirementsLookup } from '@/hooks/useRequirementsLookup';
import type {
TaskStatus,
TaskListResponse,
BatchStrategy,
ApiError,
PrdListResponse,
} from '@/types';
interface TaskBoardViewProps {
workspacePath: string;
}
export function TaskBoardView({ workspacePath }: TaskBoardViewProps) {
const router = useRouter();
// ─── Data fetching ─────────────────────────────────────────────
const { data, isLoading, error, mutate } = useSWR<TaskListResponse, ApiError>(
`/api/v2/tasks?path=${workspacePath}`,
() => tasksApi.getAll(workspacePath)
);
const { requirementsMap } = useRequirementsLookup(workspacePath);
// PRD existence check — drives empty state context message
const { data: prdData } = useSWR<PrdListResponse>(
`/api/v2/prd?path=${workspacePath}`,
() => prdApi.getAll(workspacePath)
);
const hasPrd = (prdData?.total ?? 0) > 0;
// ─── Filter state ──────────────────────────────────────────────
const [searchQuery, setSearchQuery] = useState('');
const [statusFilter, setStatusFilter] = useState<TaskStatus | null>(null);
// ─── Batch execution state ─────────────────────────────────────
const [selectionMode, setSelectionMode] = useState(false);
const [selectedTaskIds, setSelectedTaskIds] = useState<Set<string>>(new Set());
const [batchStrategy, setBatchStrategy] = useState<BatchStrategy>('serial');
const [isExecuting, setIsExecuting] = useState(false);
const [isStoppingBatch, setIsStoppingBatch] = useState(false);
const [isResettingBatch, setIsResettingBatch] = useState(false);
const [confirmAction, setConfirmAction] = useState<{
type: BulkActionType;
count: number;
taskIds: string[];
} | null>(null);
// ─── Detail modal state ────────────────────────────────────────
const [detailTaskId, setDetailTaskId] = useState<string | null>(null);
// ─── Error state for actions ───────────────────────────────────
const [actionError, setActionError] = useState<string | null>(null);
// ─── Per-task loading state ──────────────────────────────────────
const [loadingTaskIds, setLoadingTaskIds] = useState<Set<string>>(new Set());
// ─── Filtered tasks ────────────────────────────────────────────
const filteredTasks = useMemo(() => {
if (!data?.tasks) return [];
let tasks = data.tasks;
if (statusFilter) {
tasks = tasks.filter((t) => t.status === statusFilter);
}
if (searchQuery.trim()) {
const q = searchQuery.toLowerCase();
tasks = tasks.filter(
(t) =>
t.title.toLowerCase().includes(q) ||
t.description.toLowerCase().includes(q)
);
}
return tasks;
}, [data?.tasks, statusFilter, searchQuery]);
// ─── Selected tasks (for batch actions) ───────────────────────
// Derive from full task list (not filteredTasks) so bulk actions
// include all selected tasks even when filters hide some of them.
const selectedTasks = useMemo(
() => (data?.tasks ?? []).filter((t) => selectedTaskIds.has(t.id)),
[data?.tasks, selectedTaskIds]
);
// ─── Handlers ──────────────────────────────────────────────────
const handleToggleSelectionMode = useCallback(() => {
setSelectionMode((prev) => {
if (prev) setSelectedTaskIds(new Set());
return !prev;
});
}, []);
const handleToggleSelect = useCallback((taskId: string) => {
setSelectedTaskIds((prev) => {
const next = new Set(prev);
if (next.has(taskId)) {
next.delete(taskId);
} else {
next.add(taskId);
}
return next;
});
}, []);
const handleClearSelection = useCallback(() => {
setSelectedTaskIds(new Set());
}, []);
const handleSelectAll = useCallback((taskIds: string[]) => {
setSelectedTaskIds((prev) => {
const next = new Set(prev);
for (const id of taskIds) next.add(id);
return next;
});
}, []);
const handleDeselectAll = useCallback((taskIds: string[]) => {
setSelectedTaskIds((prev) => {
const next = new Set(prev);
for (const id of taskIds) next.delete(id);
return next;
});
}, []);
const handleTaskClick = useCallback((taskId: string) => {
setDetailTaskId(taskId);
}, []);
const handleCloseDetail = useCallback(() => {
setDetailTaskId(null);
}, []);
const handleExecute = useCallback(
async (taskId: string) => {
setActionError(null);
try {
await tasksApi.startExecution(workspacePath, taskId);
setDetailTaskId(null);
router.push(`/execution/${taskId}`);
} catch (err) {
const apiErr = err as ApiError;
setActionError(apiErr.detail || 'Failed to start execution');
}
},
[workspacePath, router]
);
const handleMarkReady = useCallback(
async (taskId: string) => {
setActionError(null);
try {
await tasksApi.updateStatus(workspacePath, taskId, 'READY');
await mutate();
} catch (err) {
const apiErr = err as ApiError;
setActionError(apiErr.detail || 'Failed to update task status');
}
},
[workspacePath, mutate]
);
const handleStop = useCallback(
async (taskId: string) => {
setActionError(null);
setLoadingTaskIds((prev) => new Set(prev).add(taskId));
try {
await tasksApi.stopExecution(workspacePath, taskId);
await mutate();
} catch (err) {
const apiErr = err as ApiError;
setActionError(apiErr.detail || 'Failed to stop task');
} finally {
setLoadingTaskIds((prev) => {
const next = new Set(prev);
next.delete(taskId);
return next;
});
}
},
[workspacePath, mutate]
);
const handleReset = useCallback(
async (taskId: string) => {
setActionError(null);
setLoadingTaskIds((prev) => new Set(prev).add(taskId));
try {
await tasksApi.updateStatus(workspacePath, taskId, 'READY');
await mutate();
} catch (err) {
const apiErr = err as ApiError;
setActionError(apiErr.detail || 'Failed to reset task');
} finally {
setLoadingTaskIds((prev) => {
const next = new Set(prev);
next.delete(taskId);
return next;
});
}
},
[workspacePath, mutate]
);
const handleExecuteBatch = useCallback(async () => {
if (selectedTaskIds.size === 0) return;
setIsExecuting(true);
setActionError(null);
try {
const result = await tasksApi.executeBatch(workspacePath, {
task_ids: Array.from(selectedTaskIds),
strategy: batchStrategy,
});
setSelectionMode(false);
setSelectedTaskIds(new Set());
router.push(`/execution?batch=${result.batch_id}`);
} catch (err) {
const apiErr = err as ApiError;
setActionError(apiErr.detail || 'Failed to start batch execution');
} finally {
setIsExecuting(false);
}
}, [workspacePath, selectedTaskIds, batchStrategy, router]);
const handleStopBatch = useCallback(() => {
const inProgressTasks = selectedTasks.filter((t) => t.status === 'IN_PROGRESS');
setConfirmAction({ type: 'stop', count: inProgressTasks.length, taskIds: inProgressTasks.map((t) => t.id) });
}, [selectedTasks]);
const handleResetBatch = useCallback(() => {
const failedTasks = selectedTasks.filter((t) => t.status === 'FAILED');
setConfirmAction({ type: 'reset', count: failedTasks.length, taskIds: failedTasks.map((t) => t.id) });
}, [selectedTasks]);
const handleConfirmAction = useCallback(async () => {
if (!confirmAction) return;
setActionError(null);
try {
if (confirmAction.type === 'stop') {
setIsStoppingBatch(true);
const results = await Promise.allSettled(
confirmAction.taskIds.map((id) => tasksApi.stopExecution(workspacePath, id))
);
const failures = results.filter((r) => r.status === 'rejected');
if (failures.length > 0) {
setActionError(`Failed to stop ${failures.length} task(s)`);
}
} else if (confirmAction.type === 'reset') {
setIsResettingBatch(true);
const results = await Promise.allSettled(
confirmAction.taskIds.map((id) => tasksApi.updateStatus(workspacePath, id, 'READY'))
);
const failures = results.filter((r) => r.status === 'rejected');
if (failures.length > 0) {
setActionError(`Failed to reset ${failures.length} task(s)`);
}
}
} finally {
setIsStoppingBatch(false);
setIsResettingBatch(false);
setConfirmAction(null);
handleClearSelection();
await mutate();
}
}, [confirmAction, workspacePath, mutate, handleClearSelection]);
const handleStatusChange = useCallback(() => {
mutate();
}, [mutate]);
// ─── Render ────────────────────────────────────────────────────
if (isLoading) {
return (
<div className="space-y-4">
<div className="h-9 w-64 animate-pulse rounded bg-muted" />
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-6">
{Array.from({ length: 6 }).map((_, i) => (
<div key={i} className="h-64 animate-pulse rounded-lg bg-muted/30" />
))}
</div>
</div>
);
}
if (error) {
return (
<div className="rounded-lg border border-destructive bg-destructive/10 p-6">
<h2 className="text-lg font-semibold text-destructive">Error</h2>
<p className="mt-2 text-sm text-muted-foreground">
{error.detail || 'Failed to load tasks'}
</p>
</div>
);
}
const tasks = data?.tasks ?? [];
return (
<div className="space-y-4">
{/* Header: page title */}
<div className="flex items-center justify-between">
<div>
<h1 className="text-2xl font-bold tracking-tight">Task Board</h1>
<p className="text-sm text-muted-foreground">
{tasks.length} task{tasks.length !== 1 ? 's' : ''} total
</p>
</div>
</div>
{/* Filters + batch actions */}
<div className="flex flex-wrap items-start justify-between gap-4">
<TaskFilters
searchQuery={searchQuery}
onSearchChange={setSearchQuery}
statusFilter={statusFilter}
onStatusFilter={setStatusFilter}
/>
<BatchActionsBar
selectionMode={selectionMode}
onToggleSelectionMode={handleToggleSelectionMode}
selectedCount={selectedTaskIds.size}
strategy={batchStrategy}
onStrategyChange={setBatchStrategy}
onExecuteBatch={handleExecuteBatch}
onClearSelection={handleClearSelection}
isExecuting={isExecuting}
selectedTasks={selectedTasks}
onStopBatch={handleStopBatch}
onResetBatch={handleResetBatch}
isStoppingBatch={isStoppingBatch}
isResettingBatch={isResettingBatch}
/>
</div>
{/* Action error banner */}
{actionError && (
<div role="alert" className="flex items-center justify-between rounded-md bg-destructive/10 px-4 py-2 text-sm text-destructive">
<span>{actionError}</span>
<button
onClick={() => setActionError(null)}
aria-label="Dismiss error"
className="ml-2 rounded p-0.5 text-destructive hover:text-destructive/80 focus-visible:outline-none focus-visible:ring-[3px] focus-visible:ring-ring"
>
<Cancel01Icon className="h-4 w-4" />
</button>
</div>
)}
{/* Empty state — shown when no tasks exist after loading completes */}
{tasks.length === 0 && (
<div className="flex flex-col items-center justify-center rounded-lg border bg-muted/30 py-16 text-center">
<Task01Icon className="mb-4 h-10 w-10 text-muted-foreground/50" />
<h2 className="text-base font-semibold text-foreground">No tasks yet</h2>
<p className="mt-1 max-w-sm text-sm text-muted-foreground">
Generate tasks from your PRD or create them manually to start building.
</p>
<p className="mt-2 text-xs text-muted-foreground">
{hasPrd ? (
<span className="flex items-center gap-1 justify-center">
<span className="h-1.5 w-1.5 rounded-full bg-green-500" />
PRD found — ready to generate tasks
</span>
) : (
<span className="flex items-center gap-1 justify-center">
<span className="h-1.5 w-1.5 rounded-full bg-muted-foreground/40" />
No PRD yet — create one first
</span>
)}
</p>
<div className="mt-6 flex gap-3">
{hasPrd ? (
<>
<Button asChild>
<Link href="/prd">Generate from PRD →</Link>
</Button>
<Button variant="outline" asChild>
<Link href="/prd">View PRD</Link>
</Button>
</>
) : (
<Button asChild>
<Link href="/prd">Create PRD →</Link>
</Button>
)}
</div>
</div>
)}
{/* Kanban board — hidden when no tasks */}
{tasks.length > 0 && <TaskBoardContent
tasks={filteredTasks}
selectionMode={selectionMode}
selectedTaskIds={selectedTaskIds}
onTaskClick={handleTaskClick}
onToggleSelect={handleToggleSelect}
onExecute={handleExecute}
onMarkReady={handleMarkReady}
onStop={handleStop}
onReset={handleReset}
onSelectAll={handleSelectAll}
onDeselectAll={handleDeselectAll}
loadingTaskIds={loadingTaskIds}
requirementsMap={requirementsMap}
/>}
{/* Task detail modal */}
<TaskDetailModal
taskId={detailTaskId}
workspacePath={workspacePath}
open={detailTaskId !== null}
onClose={handleCloseDetail}
onExecute={handleExecute}
onStatusChange={handleStatusChange}
onOpenTask={handleTaskClick}
/>
{/* Bulk action confirmation */}
<BulkActionConfirmDialog
open={confirmAction !== null}
onOpenChange={(open) => {
if (!open) setConfirmAction(null);
}}
actionType={confirmAction?.type ?? 'stop'}
taskCount={confirmAction?.count ?? 0}
onConfirm={handleConfirmAction}
isLoading={isStoppingBatch || isResettingBatch || isExecuting}
/>
</div>
);
}