import { useEffect, useRef, useState } from 'react' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { client } from '@/ui/api/client' import { m } from '@/ui/paraglide/messages.js' import { Button } from '@/ui/components/ui/button' import { Input } from '@/ui/components/ui/input' import { Textarea } from '@/ui/components/ui/textarea' import { Label } from '@/ui/components/ui/label' import { Switch } from '@/ui/components/ui/switch' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/ui/components/ui/select' import { Avatar, AvatarFallback, AvatarImage } from '@/ui/components/ui/avatar' import { ScrollArea } from '@/ui/components/ui/scroll-area' import { DateTimePicker } from '@/ui/components/datetime-picker' import { toast } from 'sonner' import { KanbanTaskPriority, type KanbanTaskDetail, type KanbanGoalInfo, type AgentInfo, type KanbanTaskAssetInfo, type UpdateKanbanTaskRequest, UNASSIGNED_GOAL_ID, } from '@shumai/dtos' import { getPriorityBadgeColor, getPriorityLabel } from '../kanban-types' import { TaskParentSelector } from '../task-parent-selector' import { TaskRelatedAssets } from '../task-related-assets' import { TaskTargetFolderDialog } from './task-target-folder-dialog' import { Bot, Folder, Target, Loader2 } from 'lucide-react' import { cn } from '@/ui/lib/utils' interface TaskInfoFormProps { teamId: string task: KanbanTaskDetail canEdit?: boolean } export function TaskInfoForm({ teamId, task, canEdit = true }: TaskInfoFormProps) { const queryClient = useQueryClient() // Local state for debounced text inputs const [title, setTitle] = useState(task.title) const [description, setDescription] = useState(task.description || '') const [isFolderPickerOpen, setIsFolderPickerOpen] = useState(false) const [targetFolderName, setTargetFolderName] = useState(null) const debounceTimerRef = useRef | null>(null) // Sync with prop changes if task id changed useEffect(() => { setTitle(task.title) setDescription(task.description || '') }, [task.id, task.title, task.description]) // Queries for selectors const { data: members = [] } = useQuery({ queryKey: ['teams', teamId, 'members', 'human'], queryFn: async () => { const res = await client.api.teams[':teamId'].members.$get({ param: { teamId }, query: { includeAgents: 'false' }, }) if (!res.ok) return [] const data = await res.json() return Array.isArray(data) ? data : [] }, enabled: !!teamId, }) const { data: agents = [] } = useQuery({ queryKey: ['teams', teamId, 'agents'], queryFn: async () => { const res = await client.api.teams[':teamId'].agents.$get({ param: { teamId }, }) if (!res.ok) return [] return ((await res.json()) as AgentInfo[]) || [] }, enabled: !!teamId, }) const { data: goalsData } = useQuery({ queryKey: ['teams', teamId, 'kanban', 'goals'], queryFn: async () => { const res = await client.api.teams[':teamId'].kanban.goals.$get({ param: { teamId }, query: {}, }) if (!res.ok) return { data: [] } return (await res.json()) as { data: KanbanGoalInfo[] } }, enabled: !!teamId, }) const goals = goalsData?.data || [] // Auto-save update mutation const { mutate: updateTask, isPending: isUpdating } = useMutation({ mutationFn: async (patch: UpdateKanbanTaskRequest) => { const res = await client.api.teams[':teamId'].kanban.tasks[':taskId'].$patch({ param: { teamId, taskId: task.id }, json: patch, }) if (!res.ok) { const err = await res.json().catch(() => ({ message: m.error() })) throw new Error((err as { message?: string }).message || m.error()) } return await res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'kanban', 'task', task.id] }) queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'kanban', 'tasks'] }) }, onError: (err) => { toast.error(err.message) }, }) const { mutate: linkAssets } = useMutation({ mutationFn: async (newAssets: KanbanTaskAssetInfo[]) => { const res = await client.api.teams[':teamId'].kanban.tasks[':taskId'].assets.$post({ param: { teamId, taskId: task.id }, json: { assetIds: newAssets.map((a) => a.id) }, }) if (!res.ok) throw new Error('Failed to link assets') return await res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'kanban', 'task', task.id] }) queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'kanban', 'tasks'] }) toast.success(m.asset_linked()) }, onError: (err) => { toast.error(err.message) }, }) const { mutate: unlinkAsset } = useMutation({ mutationFn: async (assetId: string) => { const res = await client.api.teams[':teamId'].kanban.tasks[':taskId'].assets[ ':assetId' ].$delete({ param: { teamId, taskId: task.id, assetId }, }) if (!res.ok) throw new Error('Failed to unlink asset') return await res.json() }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'kanban', 'task', task.id] }) queryClient.invalidateQueries({ queryKey: ['teams', teamId, 'kanban', 'tasks'] }) toast.success(m.asset_unlinked()) }, onError: (err) => { toast.error(err.message) }, }) // Debounced auto-save handler for text fields const handleTextChange = (field: 'title' | 'description', value: string) => { if (field === 'title') setTitle(value) if (field === 'description') setDescription(value) if (debounceTimerRef.current) { clearTimeout(debounceTimerRef.current) } debounceTimerRef.current = setTimeout(() => { if (field === 'title' && value.trim() && value.trim() !== task.title) { updateTask({ title: value.trim() }) } else if (field === 'description' && value !== (task.description || '')) { updateTask({ description: value.trim() || null }) } }, 600) } const isAgentic = task.isAgentTask return (
{/* Title Input */}
{isUpdating && ( {m.saving()} )}
handleTextChange('title', e.target.value)} disabled={!canEdit} className="text-base font-semibold h-10 bg-background" placeholder={m.task_title_placeholder()} />
{/* Description Textarea */}