-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskTable.tsx
More file actions
177 lines (164 loc) · 5.56 KB
/
TaskTable.tsx
File metadata and controls
177 lines (164 loc) · 5.56 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
'use client';
import React, { useCallback, useState } from 'react';
import { useRouter } from 'next/navigation';
import {
CSmartTable,
CBadge,
CButton,
CCard,
CCardHeader,
CCardBody,
CCollapse,
} from '@coreui/react-pro';
import type { Task, TaskStatus } from '@/lib/types';
import { STATUS_COLORS } from '@/lib/types';
interface TaskTableProps {
tasks: Task[];
showRetired: boolean;
groupByRepo: boolean;
}
const columns = [
{ key: 'actions', label: '', _style: { width: '120px' }, sorter: false },
{ key: 'task_key', label: 'Key', _style: { width: '150px' } },
{ key: 'title', label: 'Title' },
{ key: 'repo_name', label: 'Repo', _style: { width: '120px' } },
{ key: 'status', label: 'Status', _style: { width: '110px' } },
{ key: 'max_turns', label: 'Turns', _style: { width: '90px' }, sorter: false },
{ key: 'created_at', label: 'Created', _style: { width: '140px' } },
];
const groupedColumns = [
{ key: 'actions', label: '', _style: { width: '120px' }, sorter: false },
{ key: 'task_key', label: 'Key', _style: { width: '150px' } },
{ key: 'title', label: 'Title' },
{ key: 'status', label: 'Status', _style: { width: '110px' } },
{ key: 'max_turns', label: 'Turns', _style: { width: '90px' }, sorter: false },
{ key: 'created_at', label: 'Created', _style: { width: '140px' } },
];
function statusBadgeColor(status: TaskStatus) {
return STATUS_COLORS[status] ?? 'secondary';
}
function getScopedColumns(router: ReturnType<typeof useRouter>, handleEnqueue: (id: number) => void, includeRepo: boolean) {
return {
status: (item: Task) => (
<td>
<CBadge color={statusBadgeColor(item.status as TaskStatus)}>
{item.status}
</CBadge>
</td>
),
...(includeRepo ? {
repo_name: (item: Task) => (
<td>{item.repo_name || '-'}</td>
),
} : {}),
max_turns: (item: Task) => (
<td>
<CBadge color="secondary" shape="rounded-pill">
{item.max_turns === null ? '∞' : (item.max_turns ?? 50)}
</CBadge>
</td>
),
created_at: (item: Task) => (
<td suppressHydrationWarning>{new Date(item.created_at).toLocaleDateString()}</td>
),
actions: (item: Task) => (
<td>
<CButton
size="sm"
color="outline-primary"
className="me-1"
onClick={() => router.push(`/tasks/${item.id}`)}
>
View
</CButton>
{item.status === 'pending' && (
<CButton
size="sm"
color="outline-success"
onClick={() => handleEnqueue(item.id)}
>
Enqueue
</CButton>
)}
</td>
),
};
}
export function TaskTable({ tasks, showRetired, groupByRepo }: TaskTableProps) {
const router = useRouter();
const [collapsedGroups, setCollapsedGroups] = useState<Record<string, boolean>>({});
const handleEnqueue = useCallback(async (taskId: number) => {
await fetch(`/api/tasks/${taskId}/enqueue`, { method: 'POST' });
router.refresh();
}, [router]);
const filteredTasks = showRetired ? tasks : tasks.filter((t) => t.status !== 'retired');
const toggleGroup = (repoName: string) => {
setCollapsedGroups((prev) => ({ ...prev, [repoName]: !prev[repoName] }));
};
if (groupByRepo) {
const groups: Record<string, Task[]> = {};
for (const task of filteredTasks) {
const key = task.repo_name || '(No Repository)';
if (!groups[key]) groups[key] = [];
groups[key].push(task);
}
const sortedGroupKeys = Object.keys(groups).sort((a, b) => {
if (a === '(No Repository)') return 1;
if (b === '(No Repository)') return -1;
return a.localeCompare(b);
});
return (
<>
{sortedGroupKeys.map((repoName) => {
const groupTasks = groups[repoName];
const isCollapsed = !!collapsedGroups[repoName];
return (
<CCard key={repoName} className="mb-3">
<CCardHeader
className="d-flex justify-content-between align-items-center"
style={{ cursor: 'pointer' }}
onClick={() => toggleGroup(repoName)}
>
<span className="fw-semibold">{repoName}</span>
<div className="d-flex align-items-center gap-2">
<CBadge color="secondary" shape="rounded-pill">
{groupTasks.length} task{groupTasks.length !== 1 ? 's' : ''}
</CBadge>
<span className="text-muted" style={{ fontSize: '0.75rem' }}>
{isCollapsed ? '▶' : '▼'}
</span>
</div>
</CCardHeader>
<CCollapse visible={!isCollapsed}>
<CCardBody className="p-0">
<CSmartTable
items={groupTasks}
columns={groupedColumns}
tableProps={{ hover: true, responsive: true, striped: true }}
columnSorter
pagination
itemsPerPage={10}
itemsPerPageSelect
scopedColumns={getScopedColumns(router, handleEnqueue, false)}
/>
</CCardBody>
</CCollapse>
</CCard>
);
})}
</>
);
}
return (
<CSmartTable
items={filteredTasks}
columns={columns}
tableProps={{ hover: true, responsive: true, striped: true }}
columnSorter
pagination
itemsPerPage={20}
itemsPerPageSelect
scopedColumns={getScopedColumns(router, handleEnqueue, true)}
/>
);
}