File size: 34,743 Bytes
d1a980e | 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 446 447 448 449 450 451 452 453 454 | "use client";
import { Fragment, useEffect, useState } from "react";
import { Dialog, Transition } from "@headlessui/react";
import { X, Clock, Calendar, Hash, User as UserIcon, Tag as TagIcon, Play, Send, Trash2, GitBranch, CheckSquare, CornerDownRight } from "lucide-react";
import { getTaskDetails, updateTaskField, addTag, removeTag, logTime } from "@/app/actions/task";
import { format } from "date-fns";
import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
type TaskDetailModalProps = {
taskId: string | null;
onClose: () => void;
users: any[];
sprints: any[];
onSwitchTask?: (taskId: string) => void;
};
export default function TaskDetailModal({ taskId, onClose, users, sprints, onSwitchTask }: TaskDetailModalProps) {
const { data: session } = useSession();
const router = useRouter();
const [task, setTask] = useState<any>(null);
const [loading, setLoading] = useState(true);
// Local state for inputs
const [newTag, setNewTag] = useState("");
const [timeAmount, setTimeAmount] = useState("");
const [timeDesc, setTimeDesc] = useState("");
const [newSubtaskTitle, setNewSubtaskTitle] = useState("");
useEffect(() => {
if (taskId) {
loadTask();
}
}, [taskId]);
async function loadTask() {
setLoading(true);
if (!taskId) return;
const res = await getTaskDetails(taskId);
if (res.success) {
setTask(res.data);
}
setLoading(false);
}
async function handleFieldUpdate(field: string, value: any) {
if (!taskId) return;
setTask((prev: any) => ({ ...prev, [field]: value }));
await updateTaskField(taskId, field, value, session?.user?.id); // Pass user ID for audit log
if (field === 'assigneeId' || field === 'sprintId') {
loadTask();
}
}
async function handleAddTag(e: React.KeyboardEvent) {
if (e.key === 'Enter' && newTag.trim() && taskId) {
e.preventDefault();
await addTag(taskId, newTag.trim(), session?.user?.id); // Pass user ID for audit log
setNewTag("");
loadTask();
}
}
async function handleLogTime(e: React.FormEvent) {
e.preventDefault();
if (!taskId || !session?.user?.id) return;
await logTime(taskId, session.user.id, parseFloat(timeAmount), timeDesc);
setTimeAmount("");
setTimeDesc("");
loadTask();
}
async function handleCreateSubtask(e: React.KeyboardEvent) {
if (e.key === 'Enter' && newSubtaskTitle.trim() && taskId) {
e.preventDefault();
const { createTask } = await import("@/app/actions/task");
const formData = new FormData();
formData.append("title", newSubtaskTitle.trim());
formData.append("parentId", taskId);
formData.append("type", "TASK");
formData.append("priority", "MEDIUM");
// Inherit sprint
if (task.sprintId) formData.append("sprintId", task.sprintId);
await createTask(formData);
setNewSubtaskTitle("");
loadTask();
}
}
if (!taskId) return null;
return (
<Transition show={!!taskId} as={Fragment}>
<Dialog as="div" className="relative z-50" onClose={onClose}>
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="ease-in duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="fixed inset-0 bg-black/70 backdrop-blur-sm transition-opacity" />
</Transition.Child>
<div className="fixed inset-0 z-10 overflow-y-auto">
<div className="flex min-h-full items-center justify-center p-4 text-center sm:p-0">
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
enterTo="opacity-100 translate-y-0 sm:scale-100"
leave="ease-in duration-200"
leaveFrom="opacity-100 translate-y-0 sm:scale-100"
leaveTo="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
>
<Dialog.Panel className="relative transform rounded-lg bg-[hsl(var(--card))] text-left shadow-xl transition-all sm:my-8 sm:w-full sm:max-w-4xl border border-[hsl(var(--border))] h-[80vh] flex flex-col">
{loading ? (
<div className="flex items-center justify-center h-full">
<div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-[hsl(var(--primary))]"></div>
</div>
) : (
<>
{/* Header */}
<div className="flex items-center justify-between px-6 py-4 border-b border-[hsl(var(--border))]">
<div className="flex items-center gap-3 w-full mr-8">
<span className={`text-xs uppercase font-bold px-2 py-1 rounded
${task.type === 'BUG' ? 'bg-red-900/50 text-red-200' : 'bg-blue-900/50 text-blue-200'}`}>
{task.type}
</span>
<input
className="text-xl font-bold bg-transparent border-none focus:outline-none focus:ring-0 w-full"
value={task.title}
onChange={(e) => setTask({ ...task, title: e.target.value })}
onBlur={(e) => handleFieldUpdate('title', e.target.value)}
/>
</div>
<button onClick={onClose} className="text-[hsl(var(--muted-foreground))] hover:text-[hsl(var(--foreground))]">
<X className="h-6 w-6" />
</button>
</div>
{/* Content Grid */}
<div className="flex-1 grid grid-cols-12 overflow-hidden">
{/* Left Column (Main) */}
<div className="col-span-8 p-6 overflow-y-auto border-r border-[hsl(var(--border))]">
{/* Description */}
<div className="mb-8">
<h3 className="text-sm font-semibold text-[hsl(var(--muted-foreground))] mb-2 flex items-center gap-2">
Description
</h3>
<textarea
className="w-full min-h-[150px] bg-[hsl(var(--muted)/0.3)] rounded-md p-4 text-sm border-none focus:ring-1 focus:ring-[hsl(var(--primary))]"
value={task.description || ""}
onChange={(e) => setTask({ ...task, description: e.target.value })}
onBlur={(e) => handleFieldUpdate('description', e.target.value)}
placeholder="Add a detailed description..."
/>
</div>
{/* Subtasks Section */}
<div className="mb-8 border-t border-[hsl(var(--border))] pt-6">
<h3 className="text-sm font-semibold text-[hsl(var(--muted-foreground))] mb-4 flex items-center gap-2">
<GitBranch className="h-4 w-4" /> Subtasks
</h3>
<div className="space-y-2 mb-3">
{task.subTasks?.map((sub: any) => (
<div
key={sub.id}
className="flex items-center gap-3 p-2 rounded-md hover:bg-[hsl(var(--muted)/0.3)] group border border-transparent hover:border-[hsl(var(--border))]"
>
<div className={`mt-0.5 h-4 w-4 rounded-sm border flex items-center justify-center
${sub.status === 'DONE' ? 'bg-green-500 border-green-500 text-white' : 'border-[hsl(var(--muted-foreground))]'}
`}>
{sub.status === 'DONE' && <CheckSquare className="h-3 w-3" />}
</div>
<div
className="flex-1 cursor-pointer"
onClick={() => {
if (onSwitchTask) onSwitchTask(sub.id);
else {
// Fallback: reload page with query param or just warn
// For now, since we haven't updated parents, this might be a no-op visually
// But we can try to force a reload? No.
// Ideally parent updates.
alert("Please implement onSwitchTask in parent");
}
}}
>
<p className={`text-sm ${sub.status === 'DONE' ? 'line-through text-[hsl(var(--muted-foreground))]' : 'text-[hsl(var(--foreground))]'}`}>
{sub.title}
</p>
<div className="flex gap-2 text-xs text-[hsl(var(--muted-foreground))] md:hidden">
<span>{sub.assignee?.name || 'Unassigned'}</span>
</div>
</div>
<div className="hidden md:flex items-center gap-2">
{sub.assignee ? (
<div className="h-5 w-5 rounded-full bg-[hsl(var(--primary)/0.1)] flex items-center justify-center text-[10px] text-[hsl(var(--primary))] font-bold" title={sub.assignee.name}>
{sub.assignee.name[0]}
</div>
) : (
<UserIcon className="h-4 w-4 text-[hsl(var(--muted-foreground))/0.5]" />
)}
<span className={`text-[10px] px-1.5 py-0.5 rounded border border-[hsl(var(--border))]
${sub.priority === 'CRITICAL' ? 'text-red-500 bg-red-500/10' : ''}
`}>
{sub.priority}
</span>
</div>
</div>
))}
</div>
<div className="relative pl-7">
<CornerDownRight className="absolute left-1 top-2.5 h-4 w-4 text-[hsl(var(--muted-foreground))]" />
<input
className="w-full bg-[hsl(var(--muted)/0.2)] rounded-md border border-transparent focus:border-[hsl(var(--primary))] placeholder:text-[hsl(var(--muted-foreground))] px-3 py-2 text-sm focus:outline-none"
placeholder="Add a subtask..."
value={newSubtaskTitle}
onChange={(e) => setNewSubtaskTitle(e.target.value)}
onKeyDown={handleCreateSubtask}
/>
</div>
</div>
{/* Activity / Comments Placeholder */}
<div>
<h3 className="text-sm font-semibold text-[hsl(var(--muted-foreground))] mb-4 px-1">Activity & Comments</h3>
<div className="space-y-4 mb-4 max-h-[300px] overflow-y-auto px-1">
{task.comments?.length === 0 && (
<p className="text-xs text-[hsl(var(--muted-foreground))] italic">No comments yet.</p>
)}
{task.comments?.map((comment: any) => (
<div key={comment.id} className="flex gap-3 text-sm">
<div className="h-6 w-6 rounded-full bg-[hsl(var(--primary)/0.1)] flex items-center justify-center text-[10px] uppercase font-bold text-[hsl(var(--primary))] shrink-0 mt-0.5">
{comment.user?.name?.[0]}
</div>
<div className="flex-1">
<div className="flex items-center gap-2 mb-0.5">
<span className="font-semibold text-xs">{comment.user?.name}</span>
<span className="text-[10px] text-[hsl(var(--muted-foreground))]">
{format(new Date(comment.createdAt), "MMM d, HH:mm")}
</span>
</div>
<p className="text-[hsl(var(--foreground))]">{comment.content}</p>
</div>
</div>
))}
</div>
<div className="flex gap-2 items-start mt-2">
<div className="h-8 w-8 rounded-full bg-[hsl(var(--primary)/0.2)] flex items-center justify-center text-xs shrink-0">
{session?.user?.name?.[0]}
</div>
<div className="flex-1 relative">
<input
className="w-full bg-[hsl(var(--background))] rounded-md border border-[hsl(var(--input))] pl-3 pr-10 py-2 text-sm focus:ring-1 focus:ring-[hsl(var(--primary))]"
placeholder="Write a comment..."
onKeyDown={async (e) => {
if (e.key === 'Enter' && e.currentTarget.value.trim()) {
e.preventDefault();
const val = e.currentTarget.value;
e.currentTarget.value = "";
const { addComment } = await import("@/app/actions/task");
if (session?.user?.id) {
await addComment(taskId, session.user.id, val);
loadTask();
}
}
}}
/>
<button className="absolute right-2 top-2 text-[hsl(var(--primary))] hover:text-[hsl(var(--primary)/0.8)]">
<Send className="h-4 w-4" />
</button>
</div>
</div>
</div>
</div>
{/* Right Column (Sidebar) */}
<div className="col-span-4 p-6 overflow-y-auto bg-[hsl(var(--muted)/0.1)] space-y-6">
{/* Status */}
<div>
<label className="text-xs font-semibold text-[hsl(var(--muted-foreground))] uppercase mb-1 block">Status</label>
<select
className="w-full bg-[hsl(var(--card))] border border-[hsl(var(--border))] rounded-md py-1.5 px-3 text-sm"
value={task.status}
onChange={(e) => handleFieldUpdate('status', e.target.value)}
>
<option value="TODO">To Do</option>
<option value="IN_PROGRESS">In Progress</option>
<option value="REVIEW">Review</option>
<option value="DONE">Done</option>
</select>
</div>
{/* Assignees */}
<div>
<label className="text-xs font-semibold text-[hsl(var(--muted-foreground))] uppercase mb-1 block">Assignee</label>
<div className="relative">
<UserIcon className="absolute left-3 top-2.5 h-3 w-3 text-[hsl(var(--muted-foreground))]" />
<select
className="w-full bg-[hsl(var(--card))] border border-[hsl(var(--border))] rounded-md py-1.5 pl-8 pr-3 text-sm"
value={task.assigneeId || ""}
onChange={(e) => handleFieldUpdate('assigneeId', e.target.value)}
>
<option value="">Unassigned</option>
{users.map(u => (
<option key={u.id} value={u.id}>{u.name}</option>
))}
</select>
</div>
</div>
{/* Priority */}
<div>
<label className="text-xs font-semibold text-[hsl(var(--muted-foreground))] uppercase mb-1 block">Priority</label>
<select
className="w-full bg-[hsl(var(--card))] border border-[hsl(var(--border))] rounded-md py-1.5 px-3 text-sm"
value={task.priority}
onChange={(e) => handleFieldUpdate('priority', e.target.value)}
>
<option value="LOW">Low</option>
<option value="MEDIUM">Medium</option>
<option value="HIGH">High</option>
<option value="CRITICAL">Critical</option>
</select>
</div>
{/* Tags */}
<div>
<label className="text-xs font-semibold text-[hsl(var(--muted-foreground))] uppercase mb-1 block">Tags</label>
<div className="flex flex-wrap gap-2 mb-2">
{task.tags.map((tag: any) => (
<span key={tag.id} className="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-200">
{tag.name}
<button onClick={() => removeTag(taskId!, tag.id)} className="ml-1 hover:text-white"><X className="h-3 w-3" /></button>
</span>
))}
</div>
<div className="relative">
<TagIcon className="absolute left-3 top-2.5 h-3 w-3 text-[hsl(var(--muted-foreground))]" />
<input
className="w-full bg-[hsl(var(--card))] border border-[hsl(var(--border))] rounded-md py-1.5 pl-8 pr-3 text-sm"
placeholder="Add tag..."
value={newTag}
onChange={(e) => setNewTag(e.target.value)}
onKeyDown={handleAddTag}
/>
</div>
</div>
{/* Time Tracking */}
<div className="pt-4 border-t border-[hsl(var(--border))]">
<label className="text-xs font-semibold text-[hsl(var(--muted-foreground))] uppercase mb-1 block">Time Tracking</label>
<div className="flex justify-between text-xs mb-2">
<span className="text-[hsl(var(--muted-foreground))]">Estimated:</span>
<input
type="number"
className="w-16 bg-transparent text-right border-b border-[hsl(var(--border))] focus:border-[hsl(var(--primary))] focus:outline-none"
value={task.estimatedTime || ""}
onChange={(e) => setTask({ ...task, estimatedTime: parseFloat(e.target.value) })}
onBlur={(e) => handleFieldUpdate('estimatedTime', e.target.value)}
placeholder="0h"
/>
</div>
<div className="flex justify-between text-xs mb-4">
<span className="text-[hsl(var(--muted-foreground))]">Logged:</span>
<span className="font-medium text-[hsl(var(--status-done))]">{task.loggedTime || 0}h</span>
</div>
<form onSubmit={handleLogTime} className="space-y-2 bg-[hsl(var(--background))] p-3 rounded-md border border-[hsl(var(--border))]">
<div className="flex items-center gap-2">
<Clock className="h-4 w-4 text-[hsl(var(--primary))]" />
<span className="text-xs font-medium">Log Time</span>
</div>
<div className="flex gap-2">
<input
type="number"
step="0.1"
className="w-1/3 bg-[hsl(var(--card))] border border-[hsl(var(--input))] rounded px-2 py-1 text-xs"
placeholder="Hrs"
value={timeAmount}
onChange={(e) => setTimeAmount(e.target.value)}
required
/>
<input
className="w-2/3 bg-[hsl(var(--card))] border border-[hsl(var(--input))] rounded px-2 py-1 text-xs"
placeholder="Details"
value={timeDesc}
onChange={(e) => setTimeDesc(e.target.value)}
/>
</div>
<button type="submit" className="w-full bg-[hsl(var(--muted))] hover:bg-[hsl(var(--muted-foreground))] text-xs py-1 rounded transition-colors flex items-center justify-center gap-1">
<Play className="h-3 w-3" /> Add Log
</button>
</form>
</div>
{/* Sprint */}
<div>
<label className="text-xs font-semibold text-[hsl(var(--muted-foreground))] uppercase mb-1 block">Sprint</label>
<select
className="w-full bg-[hsl(var(--card))] border border-[hsl(var(--border))] rounded-md py-1.5 px-3 text-sm"
value={task.sprintId || ""}
onChange={(e) => handleFieldUpdate('sprintId', e.target.value)}
>
<option value="">No Sprint</option>
{sprints.map(s => (
<option key={s.id} value={s.id}>{s.name}</option>
))}
</select>
</div>
{/* Delete Task */}
<div className="pt-4 mt-2 border-t border-[hsl(var(--border))]">
<button
onClick={async () => {
if (confirm("Are you sure you want to delete this task? This cannot be undone.")) {
const { deleteTask } = await import("@/app/actions/task");
await deleteTask(taskId, session?.user?.id);
onClose();
router.refresh();
}
}}
className="w-full bg-[hsl(var(--destructive)/0.1)] hover:bg-[hsl(var(--destructive)/0.2)] text-[hsl(var(--destructive))] text-xs py-2 rounded-md transition-colors flex items-center justify-center gap-2 font-medium"
>
<Trash2 className="h-3 w-3" />
Delete Task
</button>
</div>
</div>
</div>
</>
)}
</Dialog.Panel>
</Transition.Child>
</div>
</div>
</Dialog >
</Transition >
);
}
|