import { useState } from 'react'; import { Calendar, User, CircleDot, FolderOpen, Zap, Pencil, Trash2, Send, } from 'lucide-react'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from '@/components/ui/dialog'; import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { Separator } from '@/components/ui/separator'; import { Input } from '@/components/ui/input'; import { ScrollArea } from '@/components/ui/scroll-area'; import { trpc } from '@/lib/trpc'; import { PriorityBadge } from './PriorityBadge'; import { parseAssignees, type TaskItem } from './TaskRow'; function formatDate(timestamp: number): string { const d = new Date(timestamp); const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; const date = `${months[d.getMonth()]} ${String(d.getDate()).padStart(2, '0')}, ${d.getFullYear()}`; if (d.getHours() === 0 && d.getMinutes() === 0) return date; const h = String(d.getHours()).padStart(2, '0'); const m = String(d.getMinutes()).padStart(2, '0'); return `${date} ${h}:${m}`; } function relativeTime(timestamp: number): string { const diff = Date.now() - timestamp; const minutes = Math.floor(diff / 60000); if (minutes < 1) return 'just now'; if (minutes < 60) return `${minutes} min ago`; const hours = Math.floor(minutes / 60); if (hours < 24) return `${hours} hr ago`; const days = Math.floor(hours / 24); return `${days}d ago`; } const STATUS_CONFIG: Record = { todo: { label: 'To Do', className: 'bg-gray-100 text-gray-700 dark:bg-gray-800 dark:text-gray-300' }, in_progress: { label: 'In Progress', className: 'bg-amber-100 text-amber-700 dark:bg-amber-900/40 dark:text-amber-300' }, done: { label: 'Done', className: 'bg-green-100 text-green-700 dark:bg-green-900/40 dark:text-green-300' }, }; function AuthorAvatar({ name }: { name: string }) { const initials = name .split(/\s+/) .slice(0, 2) .map((w) => w[0]?.toUpperCase() ?? '') .join(''); return (
{initials}
); } interface TaskDetailDialogProps { task: TaskItem | null; open: boolean; onOpenChange: (open: boolean) => void; onEdit: (task: TaskItem) => void; onDelete: (id: string) => void; } export function TaskDetailDialog({ task, open, onOpenChange, onEdit, onDelete }: TaskDetailDialogProps) { const [commentText, setCommentText] = useState(''); const [activeTab, setActiveTab] = useState('description'); const { data: comments } = trpc.taskComments.list.useQuery( { taskId: task?.id ?? '' }, { enabled: !!task }, ); const utils = trpc.useUtils(); const addComment = trpc.taskComments.create.useMutation({ onSuccess: () => { void utils.taskComments.list.invalidate({ taskId: task?.id ?? '' }); setCommentText(''); }, }); const deleteComment = trpc.taskComments.delete.useMutation({ onSuccess: () => { void utils.taskComments.list.invalidate({ taskId: task?.id ?? '' }); }, }); if (!task) return null; const assignees = parseAssignees(task.assignee); const statusConf = STATUS_CONFIG[task.status ?? 'todo'] ?? { label: 'To Do', className: 'bg-gray-100 text-gray-700 dark:bg-gray-800 dark:text-gray-300' }; const breadcrumb = [task.clientName, task.subClientName, task.projectName].filter(Boolean); const handleAddComment = () => { const text = commentText.trim(); if (!text) return; addComment.mutate({ taskId: task.id, author: 'Me', content: text }); }; return ( {/* Header */} {task.title} {/* Field rows */}
{/* Assignee */}
Assignee
{assignees.length > 0 ? ( assignees.map((name) => ( {name} )) ) : ( Unassigned )}
{/* Status */}
Status
{statusConf.label}
{/* Due date */}
Due date
{task.dueDate ? formatDate(task.dueDate) : No due date}
{/* Priority */}
Priority
{/* Project */} {breadcrumb.length > 0 && ( <>
Project
{breadcrumb.join(' > ')}
)}
{/* Tabs: Description / Comment */} Description Comment {task.description ? (

{task.description}

) : (

No description provided.

)}
{/* Comment list */}
{(!comments || comments.length === 0) ? (

No comments yet.

) : ( comments.map((c) => (
{c.author} {relativeTime(c.createdAt)}
{c.content}
)) )}
{/* Add comment input */}
{ e.preventDefault(); handleAddComment(); }} > setCommentText(e.target.value)} className="flex-1" />
{/* Footer */}
); }