"use client" import { useState, useEffect, useRef } from "react" import { motion, AnimatePresence } from "framer-motion" import { ListOfTasks, Task } from "@/lib/schema/tasks" import { Input } from "@/components/ui/input" import { Button } from "@/components/ui/button" import { useAccount } from "@/lib/providers/jazz-provider" import { LaIcon } from "@/components/custom/la-icon" import { Checkbox } from "@/components/ui/checkbox" import { format } from "date-fns" interface TaskFormProps {} export const TaskForm: React.FC = ({}) => { const [title, setTitle] = useState("") const [inputVisible, setInputVisible] = useState(false) const { me } = useAccount({ root: {} }) const inputRef = useRef(null) const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (title.trim()) { if (me?.root?.tasks === undefined) { if (!me) return me.root.tasks = ListOfTasks.create([], { owner: me }) } const newTask = Task.create( { title, description: "", status: "todo", createdAt: new Date(), updatedAt: new Date() }, { owner: me._owner } ) me.root.tasks?.push(newTask) resetForm() } } const resetForm = () => { setTitle("") setInputVisible(false) } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === "Escape") { resetForm() } else if (e.key === "Backspace" && title.trim() === "") { resetForm() } } useEffect(() => { if (inputVisible && inputRef.current) { inputRef.current.focus() } }, [inputVisible]) const formattedDate = format(new Date(), "EEE, MMMM do, yyyy") return (
{!inputVisible ? ( ) : (
{}} /> setTitle(e.target.value)} onKeyDown={handleKeyDown} // placeholder="Task title" />
{formattedDate}
)}
) }