'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' import { createItem } from '@/lib/actions' import { Plus } from 'lucide-react' import { useToast } from './ToastContainer' import { validateItemTitle, sanitizeString } from '@/lib/validation' interface InlineItemCreateProps { listId: string } export default function InlineItemCreate({ listId }: InlineItemCreateProps) { const router = useRouter() const { showToast } = useToast() const [isCreating, setIsCreating] = useState(false) const [title, setTitle] = useState('') const [isSubmitting, setIsSubmitting] = useState(false) const [error, setError] = useState('') const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() // Validate title const validationResult = validateItemTitle(title) if (!validationResult.isValid) { setError(validationResult.error || 'Invalid title') return } if (isSubmitting) { return } setIsSubmitting(true) setError('') try { await createItem(listId, sanitizeString(title)) showToast('Task added successfully', 'success') setTitle('') setIsCreating(false) router.refresh() } catch (error) { console.error('Error creating item:', error) const errorMessage = error instanceof Error ? error.message : 'Failed to add task' setError(errorMessage) showToast(errorMessage, 'error') } finally { setIsSubmitting(false) } } const handleCancel = () => { setTitle('') setIsCreating(false) } const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Escape') { handleCancel() } } if (!isCreating) { return ( ) } return (
) }