import { useState, useRef, useEffect } from "react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import { getUserTodos, addUserTodo, toggleUserTodo, deleteUserTodo } from "../../lib/commands"; export function UserTodoPanel({ projectId }: { projectId: string }) { const qc = useQueryClient(); const [input, setInput] = useState(""); const inputRef = useRef(null); const { data: todos = [] } = useQuery({ queryKey: ["user-todos", projectId], queryFn: () => getUserTodos(projectId), }); const addMut = useMutation({ mutationFn: (content: string) => addUserTodo(projectId, content), onSuccess: () => { qc.invalidateQueries({ queryKey: ["user-todos", projectId] }); setInput(""); }, }); const toggleMut = useMutation({ mutationFn: (id: number) => toggleUserTodo(id), onSuccess: () => qc.invalidateQueries({ queryKey: ["user-todos", projectId] }), }); const deleteMut = useMutation({ mutationFn: (id: number) => deleteUserTodo(id), onSuccess: () => qc.invalidateQueries({ queryKey: ["user-todos", projectId] }), }); useEffect(() => { inputRef.current?.focus(); }, []); const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Enter" && input.trim()) { addMut.mutate(input.trim()); } }; const pending = todos.filter((t) => !t.done); const done = todos.filter((t) => t.done); return (
{/* 输入框 */} setInput(e.target.value)} onKeyDown={handleKeyDown} placeholder="记点什么… 回车确认" className="w-full text-xs px-2 py-1.5 rounded border border-gray-200 bg-gray-50 placeholder-gray-300 focus:outline-none focus:border-gray-400 mb-2" /> {/* 待办列表 */} {pending.length === 0 && done.length === 0 && (

暂无备忘

)}
    {pending.map((t) => (
  • ))} {/* 已完成的条目折叠在末尾 */} {done.map((t) => (
  • {t.content}
  • ))}
); }