-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskDetail.tsx
More file actions
163 lines (143 loc) · 4.97 KB
/
Copy pathTaskDetail.tsx
File metadata and controls
163 lines (143 loc) · 4.97 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
'use client';
import Link from 'next/link';
import { useCallback, useRef, useState } from 'react';
import { ALLOWED_TRANSITIONS } from '@/constants/task';
import { useTaskSocket } from '@/hooks/useTaskSocket';
import { messages } from '@/i18n';
import { updateTaskStatus } from '@/lib/client-api';
import type { Task, TaskStatus } from '@/types/task';
import { EditTaskModal } from './EditTaskModal';
import { LocalTime } from './LocalTime';
import { PriorityBadge } from './PriorityBadge';
import { StatusBadge } from './StatusBadge';
import { Select } from './ui/Select';
const t = messages.detail;
/**
* Detail view. Status changes use OPTIMISTIC updates (reflected immediately,
* rolled back on error). Other fields are edited in a modal. The page also
* stays live: WebSocket updates for THIS task are applied (reconciled by
* `updatedAt`), but never while a local change is in flight.
*/
export function TaskDetail({ task: initialTask }: { task: Task }) {
const [task, setTask] = useState<Task>(initialTask);
const [error, setError] = useState<string | null>(null);
const [pending, setPending] = useState(false);
const [editing, setEditing] = useState(false);
const pendingRef = useRef(false);
const options = ALLOWED_TRANSITIONS[task.status];
const applyRemote = useCallback(
(incoming: Task) => {
if (incoming.id !== initialTask.id) return; // not our task
setTask((prev) => {
if (pendingRef.current) return prev; // local change in flight
if (
new Date(incoming.updatedAt).getTime() <
new Date(prev.updatedAt).getTime()
) {
return prev; // stale event
}
return incoming;
});
},
[initialTask.id],
);
useTaskSocket({ onUpdated: applyRemote, onDeleted: applyRemote });
async function handleChange(next: TaskStatus) {
setError(null);
setPending(true);
pendingRef.current = true;
let previous: Task | undefined;
setTask((prev) => {
previous = prev;
return { ...prev, status: next, updatedAt: new Date().toISOString() };
});
try {
const confirmed = await updateTaskStatus(task.id, next);
setTask(confirmed);
} catch (err) {
if (previous) setTask(previous);
setError(err instanceof Error ? err.message : t.error);
} finally {
setPending(false);
pendingRef.current = false;
}
}
return (
<div className="panel">
<div className="detail-head">
<Link href="/" className="back-link">
{t.back}
</Link>
<button
type="button"
className="btn btn--ghost btn--sm"
onClick={() => setEditing(true)}
>
{t.editButton}
</button>
</div>
<div className="card-top">
<h1 className="detail-title">{task.title}</h1>
<PriorityBadge priority={task.priority} />
</div>
<section className="detail-section">
<h2 className="detail-section-title">{t.descriptionHeading}</h2>
{task.description ? (
<p className="detail-description">{task.description}</p>
) : (
<p className="detail-empty">{t.noDescription}</p>
)}
</section>
<section className="detail-section">
<h2 className="detail-section-title">{t.detailsHeading}</h2>
<dl className="detail-fields">
<dt>{t.statusLabel}</dt>
<dd><StatusBadge status={task.status} /></dd>
<dt>{t.priorityLabel}</dt>
<dd><PriorityBadge priority={task.priority} /></dd>
<dt>{t.tagsLabel}</dt>
<dd>
{task.tags.length > 0 ? (
<div className="card-tags">
{task.tags.map((tag) => (
<span className="tag" key={tag}>
#{tag}
</span>
))}
</div>
) : (
<span className="detail-empty">{t.noTags}</span>
)}
</dd>
<dt>{t.createdLabel}</dt>
<dd><LocalTime iso={task.createdAt} /></dd>
<dt>{t.updatedLabel}</dt>
<dd><LocalTime iso={task.updatedAt} /></dd>
</dl>
</section>
{error ? <div className="form-error">{error}</div> : null}
<section className="detail-section detail-section--action">
<h2 className="detail-section-title">{t.changeStatus}</h2>
{options.length === 0 ? (
<span className="detail-empty">{t.terminal}</span>
) : (
<Select
value=""
options={options.map((s) => ({ value: s, label: messages.columns[s] }))}
onChange={(v) => handleChange(v as TaskStatus)}
placeholder={pending ? t.updating : t.selectPlaceholder}
ariaLabel={t.changeStatus}
disabled={pending}
width="auto"
/>
)}
</section>
<EditTaskModal
task={task}
open={editing}
onClose={() => setEditing(false)}
onSaved={(updated) => setTask(updated)}
/>
</div>
);
}