task-manager-system / src /components /domain /TaskDetailModal.tsx
sarransh's picture
Upload batch 1/2
d1a980e verified
Raw History Blame Contribute Delete
34.7 kB
"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 >
);
}