import { useEffect, useState } from "react"; import { Sheet, SheetContent, SheetHeader, SheetTitle } from "@/components/ui/sheet"; import { Button } from "@/components/ui/button"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Badge } from "@/components/ui/badge"; import { CopyButton } from "@/components/elements/CopyButton"; import { TagPicker } from "@/components/elements/TagPicker"; import { DatePicker } from "@/components/elements/DatePicker"; import { toast } from "sonner"; import { Flag, Loader2, Trash2 } from "lucide-react"; import { cn } from "@/lib/utils"; import { useTodoData } from "@/context/TodoDataContext"; import { useTags, useTagMutations, useTodoTags } from "@/hooks/useTags"; import { PRIORITY_LABELS, dueLabel } from "@/lib/todos"; import type { TodoDto } from "@/lib/api"; interface Props { todo: TodoDto | null; onClose: () => void; } const NO_PRIORITY = "none"; export function TodoDetailSheet({ todo, onClose }: Props) { const { lists, updateTodo, deleteTodo, toggleTodo } = useTodoData(); const { data: allTags = [] } = useTags(); const { create: createTag } = useTagMutations(); const { data: todoTags = [], setTags } = useTodoTags(todo?.id); const [title, setTitle] = useState(""); const [notes, setNotes] = useState(""); const [dueDate, setDueDate] = useState(""); const [priority, setPriority] = useState(NO_PRIORITY); const [flagged, setFlagged] = useState(false); const [listId, setListId] = useState(""); const [saving, setSaving] = useState(false); useEffect(() => { if (!todo) return; setTitle(todo.title); setNotes(todo.notes ?? ""); setDueDate(todo.dueDate ?? ""); setPriority(todo.priority ?? NO_PRIORITY); setFlagged(todo.flagged); setListId(todo.listId ?? ""); }, [todo]); if (!todo) return null; const save = async (updates: Partial) => { setSaving(true); try { await updateTodo(todo.id, updates as never); } catch { toast.error("Couldn't save that change"); } finally { setSaving(false); } }; const commitTitle = () => { const trimmed = title.trim(); if (!trimmed) { setTitle(todo.title); return; } if (trimmed !== todo.title) void save({ title: trimmed }); }; const commitNotes = () => { const next = notes.trim() || null; if (next !== (todo.notes ?? null)) void save({ notes: next as never }); }; return ( !open && onClose()}> Todo {todo.source !== "manual" && ( via Shortcuts )} {saving && }