File size: 15,955 Bytes
8c3e275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState } from 'react';
import type { Task } from '../services/api';
import { PageParseAPI } from '../services/api';
import {
  Search, Calendar, AlertTriangle, CheckCircle,
  Circle, Trash2, Download, Check, X, Edit2, Copy, Database
} from 'lucide-react';

interface TaskListProps {
  tasks: Task[];
  onTasksChanged: () => void;
  variant?: 'full' | 'compact';
}

export const TaskList = ({ tasks, onTasksChanged, variant = 'full' }: TaskListProps) => {
  const [searchQuery, setSearchQuery] = useState('');
  const [filterPriority, setFilterPriority] = useState('all');
  const [filterStatus, setFilterStatus] = useState('all');
  const [editingTaskId, setEditingTaskId] = useState<number | null>(null);
  const [copiedMD, setCopiedMD] = useState(false);
  const [copiedTaskId, setCopiedTaskId] = useState<number | null>(null);
  const [editName, setEditName] = useState('');
  const [editDueDate, setEditDueDate] = useState('');
  const [editPriority, setEditPriority] = useState<'high' | 'medium' | 'low'>('medium');
  const [editCategory, setEditCategory] = useState('');

  const filteredTasks = tasks.filter(t => {
    const q = searchQuery.toLowerCase();
    const matchesSearch = t.task.toLowerCase().includes(q) || (t.category?.toLowerCase().includes(q) ?? false);
    const matchesPriority = filterPriority === 'all' || t.priority === filterPriority;
    const matchesStatus = filterStatus === 'all' || t.status === filterStatus;
    return matchesSearch && matchesPriority && matchesStatus;
  });

  const handleToggleStatus = async (task: Task) => {
    const newStatus = task.status === 'todo' ? 'done' : 'todo';
    try { await PageParseAPI.updateTask(task.id, { status: newStatus }); onTasksChanged(); }
    catch (err) { console.error(err); }
  };

  const handleDelete = async (id: number) => {
    try { await PageParseAPI.deleteTask(id); onTasksChanged(); }
    catch (err) { console.error(err); }
  };

  const startEditing = (task: Task) => {
    setEditingTaskId(task.id);
    setEditName(task.task);
    setEditDueDate(task.due_date || '');
    setEditPriority(task.priority || 'medium');
    setEditCategory(task.category || '');
  };

  const saveEditing = async (id: number) => {
    try {
      await PageParseAPI.updateTask(id, { task: editName, due_date: editDueDate || null, priority: editPriority, category: editCategory || null });
      setEditingTaskId(null);
      onTasksChanged();
    } catch (err) { console.error(err); }
  };

  const handleCopyMD = async () => {
    const md = filteredTasks.map(t => `- [${t.status === 'done' ? 'x' : ' '}] ${t.task}${t.due_date ? ` (due: ${t.due_date})` : ''}`).join('\n');
    try { await navigator.clipboard.writeText(md); setCopiedMD(true); setTimeout(() => setCopiedMD(false), 2000); }
    catch (err) { console.error(err); }
  };

  const handleCopyTask = async (task: Task) => {
    try { await navigator.clipboard.writeText(task.task); setCopiedTaskId(task.id); setTimeout(() => setCopiedTaskId(null), 2000); }
    catch (err) { console.error(err); }
  };

  const handleExportJSON = () => {
    const blob = new Blob([JSON.stringify(filteredTasks, null, 2)], { type: 'application/json' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url; a.download = `pageparse_${new Date().toISOString().split('T')[0]}.json`;
    a.click(); URL.revokeObjectURL(url);
  };

  const handleExportCSV = () => {
    const headers = ['ID', 'Task', 'Due Date', 'Priority', 'Category', 'Status', 'Confidence'];
    const rows = filteredTasks.map(t => [t.id, `"${t.task.replace(/"/g, '""')}"`, t.due_date || '', t.priority || '', t.category || '', t.status || '', t.ocr_confidence.toFixed(2)]);
    const csv = [headers.join(','), ...rows.map(r => r.join(','))].join('\n');
    const blob = new Blob([csv], { type: 'text/csv' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url; a.download = `pageparse_${new Date().toISOString().split('T')[0]}.csv`;
    a.click(); URL.revokeObjectURL(url);
  };

  if (variant === 'compact') {
    return (
      <div className="section">
        <div className="section-header">
          <h3><Database size={16} className="text-secondary" /> Recent Records ({filteredTasks.length})</h3>
          <div className="flex gap-2 items-center">
            <button className="btn btn-sm btn-ghost" onClick={handleCopyMD} disabled={!filteredTasks.length}>{copiedMD ? <Check size={12} /> : <Copy size={12} />}</button>
            <button className="btn btn-sm btn-ghost" onClick={handleExportJSON} disabled={!filteredTasks.length}><Download size={12} /> JSON</button>
            <button className="btn btn-sm btn-ghost" onClick={handleExportCSV} disabled={!filteredTasks.length}><Download size={12} /> CSV</button>
          </div>
        </div>
        <div className="section-body">
          <div className="flex gap-3 mb-3">
            <div style={{ position: 'relative', flex: 1 }}>
              <Search size={14} style={{ position: 'absolute', left: 10, top: '50%', transform: 'translateY(-50%)', color: 'var(--text-tertiary)' }} />
              <input className="input" style={{ paddingLeft: 32 }} placeholder="Search..." value={searchQuery} onChange={e => setSearchQuery(e.target.value)} />
            </div>
            <select value={filterPriority} onChange={e => setFilterPriority(e.target.value)} className="select">
              <option value="all">All Priority</option>
              <option value="high">High</option>
              <option value="medium">Medium</option>
              <option value="low">Low</option>
            </select>
            <select value={filterStatus} onChange={e => setFilterStatus(e.target.value)} className="select">
              <option value="all">All Status</option>
              <option value="todo">To Do</option>
              <option value="done">Done</option>
            </select>
          </div>
          <div className="table-wrap">
            <table>
              <thead>
                <tr>
                  <th style={{ width: 32 }}></th>
                  <th style={{ width: 80 }}>Type</th>
                  <th>Task</th>
                  <th style={{ width: 100 }}>Due</th>
                  <th style={{ width: 80 }}>Priority</th>
                  <th style={{ width: 80 }}>Confidence</th>
                  <th style={{ width: 60 }}></th>
                </tr>
              </thead>
              <tbody>
                {filteredTasks.slice(0, 8).map(task => (
                  <tr key={task.id} className={`${task.status === 'done' ? 'done' : ''} ${(task.ocr_confidence || 0) < 0.6 ? 'low-confidence' : ''}`}>
                    <td>
                      <button className="btn-icon" onClick={() => handleToggleStatus(task)}>
                        {task.status === 'done' ? <CheckCircle size={16} style={{ color: 'var(--success)' }} /> : <Circle size={16} />}
                      </button>
                    </td>
                    <td><span className="text-xs text-tertiary font-semibold" style={{ textTransform: 'uppercase' }}>{(task.type || 'task').replace('_', ' ')}</span></td>
                    <td>
                      <div className="flex gap-2 items-center">
                        <span className="task-text">{task.task}</span>
                        {(task.ocr_confidence || 0) < 0.6 && <span className="badge badge-medium"><AlertTriangle size={10} /> Low Conf</span>}
                      </div>
                    </td>
                    <td><span className="text-xs text-tertiary">{task.due_date || '—'}</span></td>
                    <td>{task.priority && <span className={`badge badge-${task.priority}`}>{task.priority}</span>}</td>
                    <td><span className="text-xs font-mono">{(task.ocr_confidence * 100).toFixed(0)}%</span></td>
                    <td>
                      <div className="flex gap-1">
                        <button className="btn-icon" onClick={() => handleCopyTask(task)}>{copiedTaskId === task.id ? <Check size={14} style={{ color: 'var(--success)' }} /> : <Copy size={14} />}</button>
                        <button className="btn-icon" onClick={() => handleDelete(task.id)}><Trash2 size={14} style={{ color: 'var(--danger)' }} /></button>
                      </div>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
            {!filteredTasks.length && <div className="history-empty">No records found.</div>}
          </div>
        </div>
      </div>
    );
  }

  return (
    <div className="section">
      <div className="section-header">
        <h3><Database size={16} className="text-secondary" /> All Records ({filteredTasks.length})</h3>
        <div className="flex gap-2 items-center">
          <button className="btn btn-sm btn-ghost" onClick={handleCopyMD} disabled={!filteredTasks.length}>{copiedMD ? <Check size={12} /> : <Copy size={12} />} Copy MD</button>
          <button className="btn btn-sm btn-ghost" onClick={handleExportJSON} disabled={!filteredTasks.length}><Download size={12} /> JSON</button>
          <button className="btn btn-sm btn-ghost" onClick={handleExportCSV} disabled={!filteredTasks.length}><Download size={12} /> CSV</button>
        </div>
      </div>
      <div className="section-body">
        <div className="flex gap-3 mb-3" style={{ flexWrap: 'wrap' }}>
          <div style={{ position: 'relative', flex: 1, minWidth: 200 }}>
            <Search size={14} style={{ position: 'absolute', left: 10, top: '50%', transform: 'translateY(-50%)', color: 'var(--text-tertiary)' }} />
            <input className="input" style={{ paddingLeft: 32 }} placeholder="Search tasks or categories..." value={searchQuery} onChange={e => setSearchQuery(e.target.value)} />
          </div>
          <select value={filterPriority} onChange={e => setFilterPriority(e.target.value)} className="select">
            <option value="all">All Priorities</option>
            <option value="high">High</option>
            <option value="medium">Medium</option>
            <option value="low">Low</option>
          </select>
          <select value={filterStatus} onChange={e => setFilterStatus(e.target.value)} className="select">
            <option value="all">All Statuses</option>
            <option value="todo">To Do</option>
            <option value="done">Completed</option>
          </select>
        </div>
        <div className="table-wrap">
          <table>
            <thead>
              <tr>
                <th style={{ width: 32 }}></th>
                <th style={{ width: 80 }}>Type</th>
                <th>Description</th>
                <th style={{ width: 110 }}>Due Date</th>
                <th style={{ width: 80 }}>Priority</th>
                <th style={{ width: 100 }}>Category</th>
                <th style={{ width: 90 }}>Confidence</th>
                <th style={{ width: 100 }}>Actions</th>
              </tr>
            </thead>
            <tbody>
              {filteredTasks.map(task => {
                const isEditing = editingTaskId === task.id;
                const isLowConf = (task.ocr_confidence || 0) < 0.6;
                return (
                  <tr key={task.id} className={`${task.status === 'done' ? 'done' : ''} ${isLowConf ? 'low-confidence' : ''}`}>
                    <td>
                      <button className="btn-icon" onClick={() => handleToggleStatus(task)}>
                        {task.status === 'done' ? <CheckCircle size={16} style={{ color: 'var(--success)' }} /> : <Circle size={16} />}
                      </button>
                    </td>
                    <td><span className="text-xs text-tertiary font-semibold" style={{ textTransform: 'uppercase' }}>{(task.type || 'task').replace('_', ' ')}</span></td>
                    <td>
                      {isEditing ? (
                        <input className="input" value={editName} onChange={e => setEditName(e.target.value)} style={{ padding: '4px 8px', fontSize: 13 }} />
                      ) : (
                        <div className="flex gap-2 items-center">
                          <span className="task-text">{task.task}</span>
                          {isLowConf && <span className="badge badge-medium"><AlertTriangle size={10} /> Verify</span>}
                        </div>
                      )}
                    </td>
                    <td>
                      {isEditing ? (
                        <input type="date" className="input" value={editDueDate} onChange={e => setEditDueDate(e.target.value)} style={{ padding: '4px 8px', fontSize: 13 }} />
                      ) : (
                        <span className="flex items-center gap-1 text-xs text-tertiary"><Calendar size={12} />{task.due_date || '—'}</span>
                      )}
                    </td>
                    <td>
                      {isEditing ? (
                        <select value={editPriority} onChange={e => setEditPriority(e.target.value as any)} className="select" style={{ padding: '4px 8px', fontSize: 13 }}>
                          <option value="high">High</option>
                          <option value="medium">Medium</option>
                          <option value="low">Low</option>
                        </select>
                      ) : (
                        task.priority && <span className={`badge badge-${task.priority}`}>{task.priority}</span>
                      )}
                    </td>
                    <td>
                      {isEditing ? (
                        <input className="input" value={editCategory} onChange={e => setEditCategory(e.target.value)} placeholder="e.g. Work" style={{ padding: '4px 8px', fontSize: 13 }} />
                      ) : (
                        <span className="text-xs text-tertiary">{task.category || '—'}</span>
                      )}
                    </td>
                    <td>
                      <div className="flex items-center gap-1">
                        <span style={{ width: 6, height: 6, borderRadius: '50%', background: isLowConf ? 'var(--danger)' : (task.ocr_confidence || 0) < 0.85 ? 'var(--warning)' : 'var(--success)' }} />
                        <span className="text-xs font-mono">{(task.ocr_confidence * 100).toFixed(0)}%</span>
                      </div>
                    </td>
                    <td>
                      <div className="flex gap-1" style={{ justifyContent: 'flex-end' }}>
                        {isEditing ? (
                          <>
                            <button className="btn-icon" onClick={() => saveEditing(task.id)}><Check size={14} style={{ color: 'var(--success)' }} /></button>
                            <button className="btn-icon" onClick={() => setEditingTaskId(null)}><X size={14} style={{ color: 'var(--danger)' }} /></button>
                          </>
                        ) : (
                          <>
                            <button className="btn-icon" onClick={() => handleCopyTask(task)} title="Copy">{copiedTaskId === task.id ? <Check size={14} style={{ color: 'var(--success)' }} /> : <Copy size={14} />}</button>
                            <button className="btn-icon" onClick={() => startEditing(task)} title="Edit"><Edit2 size={14} /></button>
                            <button className="btn-icon" onClick={() => handleDelete(task.id)} title="Delete"><Trash2 size={14} style={{ color: 'var(--danger)' }} /></button>
                          </>
                        )}
                      </div>
                    </td>
                  </tr>
                );
              })}
            </tbody>
          </table>
          {!filteredTasks.length && <div className="history-empty">No records found matching the filters.</div>}
        </div>
      </div>
    </div>
  );
};