2026-07-16 06:19:58 -04:00
|
|
|
'use client';
|
|
|
|
|
|
2026-07-29 06:12:35 -04:00
|
|
|
import { useEffect, useState, useCallback } from 'react';
|
2026-07-16 06:19:58 -04:00
|
|
|
import {
|
|
|
|
|
Table,
|
|
|
|
|
TableBody,
|
|
|
|
|
TableCell,
|
|
|
|
|
TableHead,
|
|
|
|
|
TableHeader,
|
2026-07-29 06:12:35 -04:00
|
|
|
TableRow,
|
2026-07-16 06:19:58 -04:00
|
|
|
} from '@/components/ui/table';
|
|
|
|
|
import { Badge } from '@/components/ui/badge';
|
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
|
import { Checkbox } from '@/components/ui/checkbox';
|
2026-07-18 19:05:52 -04:00
|
|
|
import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area';
|
2026-07-26 01:22:41 +00:00
|
|
|
import { Calendar, MoreHorizontal, Pencil, Trash2 } from 'lucide-react';
|
2026-07-18 19:05:52 -04:00
|
|
|
import { toast } from 'sonner';
|
2026-07-16 06:19:58 -04:00
|
|
|
import { TaskDetailPanel } from './task-detail-panel';
|
2026-07-25 02:22:01 +00:00
|
|
|
import {
|
|
|
|
|
DropdownMenu,
|
|
|
|
|
DropdownMenuContent,
|
|
|
|
|
DropdownMenuItem,
|
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
|
} from '@/components/ui/dropdown-menu';
|
|
|
|
|
import {
|
|
|
|
|
AlertDialog,
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
AlertDialogTitle,
|
|
|
|
|
} from '@/components/ui/alert-dialog';
|
2026-07-29 06:12:35 -04:00
|
|
|
import { useRealtimeContext } from '@/components/realtime-provider';
|
2026-07-16 06:19:58 -04:00
|
|
|
|
|
|
|
|
interface Task {
|
|
|
|
|
id: string;
|
|
|
|
|
title: string;
|
2026-07-29 06:12:35 -04:00
|
|
|
description?: string | null;
|
|
|
|
|
status: 'todo' | 'in_progress' | 'done' | 'cancelled';
|
2026-07-16 06:19:58 -04:00
|
|
|
priority: 'low' | 'medium' | 'high' | 'urgent';
|
2026-07-29 06:12:35 -04:00
|
|
|
domainId: string;
|
|
|
|
|
dueDate?: string | null;
|
|
|
|
|
projectId?: string | null;
|
|
|
|
|
order: number;
|
|
|
|
|
tags: { id: string; name: string; color: string | null }[];
|
2026-07-16 06:19:58 -04:00
|
|
|
}
|
|
|
|
|
|
2026-07-29 06:12:35 -04:00
|
|
|
const priorityColors: Record<string, string> = {
|
|
|
|
|
urgent: 'destructive',
|
|
|
|
|
high: 'default',
|
|
|
|
|
medium: 'secondary',
|
|
|
|
|
low: 'secondary',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function TasksListView({
|
|
|
|
|
domainId,
|
|
|
|
|
onRefresh,
|
|
|
|
|
}: {
|
|
|
|
|
domainId: string;
|
|
|
|
|
onRefresh?: () => void;
|
|
|
|
|
}) {
|
2026-07-16 06:19:58 -04:00
|
|
|
const [tasks, setTasks] = useState<Task[]>([]);
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
const [selectedTask, setSelectedTask] = useState<Task | null>(null);
|
2026-07-25 02:22:01 +00:00
|
|
|
const [deleteId, setDeleteId] = useState<string | null>(null);
|
|
|
|
|
const [deleting, setDeleting] = useState(false);
|
2026-07-26 01:25:41 +00:00
|
|
|
const [domainMap, setDomainMap] = useState<Map<string, string>>(new Map());
|
2026-07-29 06:12:35 -04:00
|
|
|
const { subscribe } = useRealtimeContext();
|
2026-07-16 06:19:58 -04:00
|
|
|
|
2026-07-29 06:12:35 -04:00
|
|
|
const fetchTasks = useCallback(async () => {
|
|
|
|
|
if (!domainId) return;
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(`/api/domains/${domainId}/tasks?sort=order&limit=200`);
|
|
|
|
|
if (!response.ok) throw new Error('Unable to load tasks');
|
|
|
|
|
const data = await response.json();
|
|
|
|
|
setTasks(data.items || []);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to fetch tasks:', error);
|
|
|
|
|
toast.error('Unable to load tasks');
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
}, [domainId]);
|
2026-07-16 06:19:58 -04:00
|
|
|
|
2026-07-29 06:12:35 -04:00
|
|
|
const fetchDomains = useCallback(async () => {
|
2026-07-26 01:25:41 +00:00
|
|
|
try {
|
|
|
|
|
const res = await fetch('/api/domains?sort=sort_order');
|
|
|
|
|
if (res.ok) {
|
|
|
|
|
const data = await res.json();
|
|
|
|
|
const map = new Map<string, string>();
|
|
|
|
|
for (const d of data.items || []) map.set(d.id, d.name);
|
|
|
|
|
setDomainMap(map);
|
|
|
|
|
}
|
|
|
|
|
} catch {}
|
2026-07-29 06:12:35 -04:00
|
|
|
}, []);
|
2026-07-26 01:25:41 +00:00
|
|
|
|
2026-07-29 06:12:35 -04:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!domainId) return;
|
|
|
|
|
setLoading(true);
|
|
|
|
|
fetchTasks();
|
|
|
|
|
fetchDomains();
|
|
|
|
|
}, [domainId, fetchTasks, fetchDomains]);
|
2026-07-26 01:25:41 +00:00
|
|
|
|
2026-07-29 06:12:35 -04:00
|
|
|
// Subscribe to realtime updates
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!domainId) return;
|
|
|
|
|
const unsubscribe = subscribe(['task'], (event: any) => {
|
|
|
|
|
if (event.type === 'task') {
|
|
|
|
|
fetchTasks();
|
|
|
|
|
onRefresh?.();
|
2026-07-16 06:19:58 -04:00
|
|
|
}
|
2026-07-29 06:12:35 -04:00
|
|
|
});
|
|
|
|
|
return unsubscribe;
|
|
|
|
|
}, [domainId, subscribe, fetchTasks, onRefresh]);
|
2026-07-16 06:19:58 -04:00
|
|
|
|
|
|
|
|
async function toggleTaskComplete(task: Task) {
|
|
|
|
|
const newStatus = task.status === 'done' ? 'todo' : 'done';
|
|
|
|
|
try {
|
2026-07-29 06:12:35 -04:00
|
|
|
const response = await fetch(`/api/domains/${domainId}/tasks/${task.id}`, {
|
2026-07-16 06:19:58 -04:00
|
|
|
method: 'PATCH',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
2026-07-29 06:12:35 -04:00
|
|
|
body: JSON.stringify({ status: newStatus }),
|
2026-07-16 06:19:58 -04:00
|
|
|
});
|
2026-07-18 19:05:52 -04:00
|
|
|
if (!response.ok) throw new Error('Unable to update task');
|
|
|
|
|
await fetchTasks();
|
|
|
|
|
toast.success(
|
2026-07-29 06:12:35 -04:00
|
|
|
`Marked "${task.title}" as ${newStatus === 'done' ? 'complete' : 'incomplete'}`
|
2026-07-18 19:05:52 -04:00
|
|
|
);
|
2026-07-16 06:19:58 -04:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to toggle task:', error);
|
2026-07-29 06:12:35 -04:00
|
|
|
toast.error(`Unable to update "${task.title}"`);
|
2026-07-16 06:19:58 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
return <p className="text-muted-foreground">Loading tasks...</p>;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 15:02:01 +00:00
|
|
|
if (tasks.length === 0) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="py-12 text-center">
|
|
|
|
|
<p className="text-muted-foreground">No tasks yet. Create your first task to get started!</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-16 06:19:58 -04:00
|
|
|
return (
|
|
|
|
|
<>
|
2026-07-18 19:05:52 -04:00
|
|
|
<ScrollArea className="w-full">
|
|
|
|
|
<div className="min-w-[700px]">
|
|
|
|
|
<Table>
|
|
|
|
|
<TableHeader>
|
|
|
|
|
<TableRow>
|
|
|
|
|
<TableHead className="w-[50px]"></TableHead>
|
|
|
|
|
<TableHead>Task</TableHead>
|
2026-07-29 06:12:35 -04:00
|
|
|
<TableHead>Status</TableHead>
|
2026-07-18 19:05:52 -04:00
|
|
|
<TableHead>Priority</TableHead>
|
|
|
|
|
<TableHead>Domain</TableHead>
|
|
|
|
|
<TableHead>Due Date</TableHead>
|
|
|
|
|
<TableHead className="w-[50px]"></TableHead>
|
|
|
|
|
</TableRow>
|
|
|
|
|
</TableHeader>
|
|
|
|
|
<TableBody>
|
|
|
|
|
{tasks.map((task) => (
|
|
|
|
|
<TableRow key={task.id}>
|
|
|
|
|
<TableCell>
|
|
|
|
|
<Checkbox
|
|
|
|
|
checked={task.status === 'done'}
|
|
|
|
|
onCheckedChange={() => toggleTaskComplete(task)}
|
|
|
|
|
aria-label={`Mark "${task.title}" as ${task.status === 'done' ? 'incomplete' : 'complete'}`}
|
|
|
|
|
/>
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell>
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => setSelectedTask(task)}
|
|
|
|
|
className={`text-left font-medium hover:underline ${
|
|
|
|
|
task.status === 'done'
|
|
|
|
|
? 'line-through text-muted-foreground'
|
|
|
|
|
: ''
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
{task.title}
|
|
|
|
|
</button>
|
|
|
|
|
</TableCell>
|
2026-07-29 06:12:35 -04:00
|
|
|
<TableCell>
|
|
|
|
|
<Badge variant="outline" className="text-xs capitalize">
|
|
|
|
|
{task.status.replace('_', ' ')}
|
|
|
|
|
</Badge>
|
|
|
|
|
</TableCell>
|
2026-07-18 19:05:52 -04:00
|
|
|
<TableCell>
|
|
|
|
|
<Badge
|
2026-07-29 06:12:35 -04:00
|
|
|
variant={(priorityColors[task.priority] as any) || 'secondary'}
|
2026-07-18 19:05:52 -04:00
|
|
|
>
|
|
|
|
|
{task.priority}
|
|
|
|
|
</Badge>
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell>
|
2026-07-29 06:12:35 -04:00
|
|
|
<Badge variant="outline">{domainMap.get(task.domainId) || task.domainId.slice(0, 8)}</Badge>
|
2026-07-18 19:05:52 -04:00
|
|
|
</TableCell>
|
|
|
|
|
<TableCell>
|
2026-07-29 06:12:35 -04:00
|
|
|
{task.dueDate && (
|
2026-07-18 19:05:52 -04:00
|
|
|
<span className="flex items-center gap-1 text-sm text-muted-foreground">
|
|
|
|
|
<Calendar className="h-3 w-3" />
|
2026-07-29 06:12:35 -04:00
|
|
|
{new Date(task.dueDate).toLocaleDateString()}
|
2026-07-18 19:05:52 -04:00
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</TableCell>
|
|
|
|
|
<TableCell>
|
2026-07-26 01:22:41 +00:00
|
|
|
<DropdownMenu>
|
|
|
|
|
<DropdownMenuTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
className="h-11 w-11"
|
|
|
|
|
aria-label={`More options for ${task.title}`}
|
|
|
|
|
>
|
|
|
|
|
<MoreHorizontal className="h-4 w-4" aria-hidden="true" />
|
|
|
|
|
</Button>
|
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
|
<DropdownMenuContent align="end">
|
|
|
|
|
<DropdownMenuItem onClick={() => setSelectedTask(task)}>
|
|
|
|
|
<Pencil className="mr-2 h-4 w-4" />
|
|
|
|
|
Edit
|
|
|
|
|
</DropdownMenuItem>
|
|
|
|
|
<DropdownMenuItem className="text-destructive" onClick={() => setDeleteId(task.id)}>
|
|
|
|
|
<Trash2 className="mr-2 h-4 w-4" />
|
|
|
|
|
Delete
|
|
|
|
|
</DropdownMenuItem>
|
|
|
|
|
</DropdownMenuContent>
|
|
|
|
|
</DropdownMenu>
|
2026-07-18 19:05:52 -04:00
|
|
|
</TableCell>
|
|
|
|
|
</TableRow>
|
|
|
|
|
))}
|
|
|
|
|
</TableBody>
|
|
|
|
|
</Table>
|
|
|
|
|
</div>
|
|
|
|
|
<ScrollBar orientation="horizontal" />
|
|
|
|
|
</ScrollArea>
|
2026-07-16 06:19:58 -04:00
|
|
|
|
2026-07-25 02:22:01 +00:00
|
|
|
<AlertDialog open={!!deleteId} onOpenChange={(o) => !o && setDeleteId(null)}>
|
|
|
|
|
<AlertDialogContent>
|
|
|
|
|
<AlertDialogHeader>
|
|
|
|
|
<AlertDialogTitle>Delete task?</AlertDialogTitle>
|
|
|
|
|
<AlertDialogDescription>This cannot be undone.</AlertDialogDescription>
|
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
<AlertDialogFooter>
|
|
|
|
|
<AlertDialogCancel disabled={deleting}>Cancel</AlertDialogCancel>
|
2026-07-29 06:12:35 -04:00
|
|
|
<AlertDialogAction
|
|
|
|
|
onClick={async () => {
|
|
|
|
|
if (!deleteId || !domainId) return;
|
|
|
|
|
setDeleting(true);
|
|
|
|
|
try {
|
|
|
|
|
const res = await fetch(`/api/domains/${domainId}/tasks/${deleteId}`, { method: 'DELETE' });
|
|
|
|
|
if (!res.ok) throw new Error();
|
|
|
|
|
toast.success('Task deleted');
|
|
|
|
|
setDeleteId(null);
|
|
|
|
|
fetchTasks();
|
|
|
|
|
} catch {
|
|
|
|
|
toast.error('Unable to delete task');
|
|
|
|
|
} finally {
|
|
|
|
|
setDeleting(false);
|
|
|
|
|
setDeleteId(null);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
disabled={deleting}
|
|
|
|
|
>
|
2026-07-25 02:22:01 +00:00
|
|
|
{deleting ? 'Deleting...' : 'Delete'}
|
|
|
|
|
</AlertDialogAction>
|
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
</AlertDialog>
|
|
|
|
|
|
2026-07-16 06:19:58 -04:00
|
|
|
{selectedTask && (
|
|
|
|
|
<TaskDetailPanel
|
2026-07-29 06:12:35 -04:00
|
|
|
taskId={selectedTask.id}
|
|
|
|
|
domainId={domainId}
|
2026-07-16 06:19:58 -04:00
|
|
|
open={!!selectedTask}
|
|
|
|
|
onOpenChange={(open) => !open && setSelectedTask(null)}
|
|
|
|
|
onUpdate={fetchTasks}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|