File size: 9,633 Bytes
8e944c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useState } from "react";

interface TaskLink {
  id: string;
  url: string;
  label: string;
  createdByAnonId: string;
}

interface Task {
  id: string;
  title: string;
  description: string;
  acceptanceCriteria: string;
  effort: string;
  status: string;
  claimedByAnonId: string | null;
  links: TaskLink[];
}

const statusColors: Record<string, string> = {
  open: "bg-gray-100 text-gray-700",
  in_progress: "bg-blue-100 text-blue-700",
  done: "bg-green-100 text-green-700",
};

const statusLabels: Record<string, string> = {
  open: "Open",
  in_progress: "In Progress",
  done: "Done",
};

const effortColors: Record<string, string> = {
  S: "bg-emerald-50 text-emerald-700",
  M: "bg-yellow-50 text-yellow-700",
  L: "bg-orange-50 text-orange-700",
  XL: "bg-red-50 text-red-700",
};

export function TaskList({
  tasks,
  onUpdate,
}: {
  tasks: Task[];
  onUpdate: (tasks: Task[]) => void;
}) {
  const [expandedId, setExpandedId] = useState<string | null>(null);
  const [linkUrl, setLinkUrl] = useState("");
  const [linkLabel, setLinkLabel] = useState("");
  const [actionLoading, setActionLoading] = useState<string | null>(null);

  async function claimTask(taskId: string) {
    setActionLoading(taskId);
    const res = await fetch(`/api/tasks/${taskId}/claim`, { method: "POST" });
    if (res.ok) {
      const d = await res.json();
      onUpdate(tasks.map((t) => (t.id === taskId ? { ...t, ...d.data } : t)));
    }
    setActionLoading(null);
  }

  async function unclaimTask(taskId: string) {
    setActionLoading(taskId);
    const res = await fetch(`/api/tasks/${taskId}/claim`, {
      method: "DELETE",
    });
    if (res.ok) {
      const d = await res.json();
      onUpdate(tasks.map((t) => (t.id === taskId ? { ...t, ...d.data } : t)));
    }
    setActionLoading(null);
  }

  async function updateStatus(taskId: string, status: string) {
    setActionLoading(taskId);
    const res = await fetch(`/api/tasks/${taskId}/status`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ status }),
    });
    if (res.ok) {
      const d = await res.json();
      onUpdate(tasks.map((t) => (t.id === taskId ? { ...t, ...d.data } : t)));
    }
    setActionLoading(null);
  }

  async function addLink(taskId: string) {
    if (!linkUrl.trim()) return;
    const res = await fetch(`/api/tasks/${taskId}/links`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ url: linkUrl, label: linkLabel || undefined }),
    });
    if (res.ok) {
      const d = await res.json();
      onUpdate(
        tasks.map((t) =>
          t.id === taskId ? { ...t, links: [...t.links, d.data] } : t
        )
      );
      setLinkUrl("");
      setLinkLabel("");
    }
  }

  async function deleteLink(taskId: string, linkId: string) {
    const res = await fetch(`/api/task-links/${linkId}`, {
      method: "DELETE",
    });
    if (res.ok) {
      onUpdate(
        tasks.map((t) =>
          t.id === taskId
            ? { ...t, links: t.links.filter((l) => l.id !== linkId) }
            : t
        )
      );
    }
  }

  return (
    <div className="space-y-2">
      {tasks.map((task) => {
        const isExpanded = expandedId === task.id;
        const isLoading = actionLoading === task.id;

        return (
          <div
            key={task.id}
            className="bg-white border border-gray-200 rounded-lg overflow-hidden"
          >
            <button
              onClick={() =>
                setExpandedId(isExpanded ? null : task.id)
              }
              className="w-full px-4 py-3 flex items-center gap-3 text-left hover:bg-gray-50"
            >
              <span
                className={`px-2 py-0.5 rounded text-xs font-medium ${
                  statusColors[task.status]
                }`}
              >
                {statusLabels[task.status]}
              </span>
              <span
                className={`px-1.5 py-0.5 rounded text-xs font-medium ${
                  effortColors[task.effort]
                }`}
              >
                {task.effort}
              </span>
              <span className="flex-1 text-sm font-medium text-gray-900">
                {task.title}
              </span>
              <svg
                className={`w-4 h-4 text-gray-400 transition-transform ${
                  isExpanded ? "rotate-180" : ""
                }`}
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth={2}
                  d="M19 9l-7 7-7-7"
                />
              </svg>
            </button>

            {isExpanded && (
              <div className="px-4 pb-4 border-t border-gray-100 pt-3 space-y-3">
                {task.description && (
                  <p className="text-sm text-gray-600">{task.description}</p>
                )}

                {task.acceptanceCriteria && (
                  <div>
                    <p className="text-xs font-medium text-gray-500 mb-1">
                      Acceptance Criteria
                    </p>
                    <p className="text-sm text-gray-700">
                      {task.acceptanceCriteria}
                    </p>
                  </div>
                )}

                {/* Actions */}
                <div className="flex flex-wrap gap-2">
                  {task.status === "open" && (
                    <button
                      onClick={() => claimTask(task.id)}
                      disabled={isLoading}
                      className="px-3 py-1.5 bg-indigo-600 text-white rounded text-sm hover:bg-indigo-700 disabled:opacity-50"
                    >
                      Claim Task
                    </button>
                  )}
                  {task.claimedByAnonId && task.status !== "done" && (
                    <>
                      {task.status === "in_progress" && (
                        <button
                          onClick={() => updateStatus(task.id, "done")}
                          disabled={isLoading}
                          className="px-3 py-1.5 bg-green-600 text-white rounded text-sm hover:bg-green-700 disabled:opacity-50"
                        >
                          Mark Done
                        </button>
                      )}
                      <button
                        onClick={() => unclaimTask(task.id)}
                        disabled={isLoading}
                        className="px-3 py-1.5 bg-gray-200 text-gray-700 rounded text-sm hover:bg-gray-300 disabled:opacity-50"
                      >
                        Unclaim
                      </button>
                    </>
                  )}
                  {task.status === "done" && (
                    <button
                      onClick={() => updateStatus(task.id, "open")}
                      disabled={isLoading}
                      className="px-3 py-1.5 bg-gray-200 text-gray-700 rounded text-sm hover:bg-gray-300 disabled:opacity-50"
                    >
                      Reopen
                    </button>
                  )}
                </div>

                {/* Links */}
                {task.links.length > 0 && (
                  <div className="space-y-1">
                    <p className="text-xs font-medium text-gray-500">Links</p>
                    {task.links.map((link) => (
                      <div
                        key={link.id}
                        className="flex items-center gap-2 text-sm"
                      >
                        <a
                          href={link.url}
                          target="_blank"
                          rel="noopener noreferrer"
                          className="text-indigo-600 hover:underline truncate"
                        >
                          {link.label || link.url}
                        </a>
                        <button
                          onClick={() => deleteLink(task.id, link.id)}
                          className="text-gray-400 hover:text-red-500 text-xs"
                        >
                          Remove
                        </button>
                      </div>
                    ))}
                  </div>
                )}

                {/* Add Link */}
                {task.claimedByAnonId && (
                  <div className="flex gap-2">
                    <input
                      type="url"
                      value={linkUrl}
                      onChange={(e) => setLinkUrl(e.target.value)}
                      placeholder="https://github.com/..."
                      className="flex-1 px-2 py-1.5 border rounded text-sm"
                    />
                    <input
                      type="text"
                      value={linkLabel}
                      onChange={(e) => setLinkLabel(e.target.value)}
                      placeholder="Label"
                      className="w-24 px-2 py-1.5 border rounded text-sm"
                    />
                    <button
                      onClick={() => addLink(task.id)}
                      className="px-3 py-1.5 bg-gray-100 rounded text-sm hover:bg-gray-200"
                    >
                      Add
                    </button>
                  </div>
                )}
              </div>
            )}
          </div>
        );
      })}
    </div>
  );
}