'use client' import { PlusIcon, Trash2Icon } from 'lucide-react' import { useId, useState } from 'react' import { ContactCombobox, type ContactComboboxOption, } from '@/components/campaign/shared/ContactCombobox' import { Button } from '@/components/ui/button' import { Field, FieldError, FieldLabel } from '@/components/ui/field' import { Input } from '@/components/ui/input' export type ActivityTaskFieldValue = { key: string title: string responsible: ContactComboboxOption | null done: boolean } type ActivityTaskFieldsProps = { initialTasks: Array<{ id: string | null title: string responsible: ContactComboboxOption | null done: boolean }> searchContacts: (query: string) => Promise error?: string } let taskKeySequence = 0 const nextTaskKey = (): string => { taskKeySequence += 1 return `new-${taskKeySequence}` } const serializeTasks = (tasks: ActivityTaskFieldValue[]) => JSON.stringify( tasks .filter((task) => task.title.trim().length > 0) .map((task) => ({ title: task.title.trim(), ...(task.responsible ? { responsible: task.responsible.id } : {}), done: task.done, })), ) export const ActivityTaskFields = ({ initialTasks, searchContacts, error, }: ActivityTaskFieldsProps) => { const fieldId = useId() const [tasks, setTasks] = useState(() => initialTasks.map((task) => ({ key: task.id ?? nextTaskKey(), title: task.title, responsible: task.responsible, done: task.done, })), ) const updateTask = (key: string, patch: Partial) => { setTasks((current) => current.map((task) => (task.key === key ? { ...task, ...patch } : task))) } const addTask = () => { setTasks((current) => [ ...current, { key: nextTaskKey(), title: '', responsible: null, done: false }, ]) } const removeTask = (key: string) => { setTasks((current) => current.filter((task) => task.key !== key)) } return ( Tarefas
{tasks.map((task, index) => (
updateTask(task.key, { title: event.target.value })} /> updateTask(task.key, { responsible: contact })} />
))}
{error ? {error} : null}
) }