'use client'; import { useEffect, useRef, useState, FormEvent, memo, useMemo, useCallback } from 'react'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { IconSettings, IconLoader2 } from '@tabler/icons-react'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogClose, } from '@kandev/ui/dialog'; import { Label } from '@kandev/ui/label'; import { Input } from '@kandev/ui/input'; import { Textarea } from '@kandev/ui/textarea'; import { Button } from '@kandev/ui/button'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@kandev/ui/select'; import { Combobox } from './combobox'; import { Badge } from '@kandev/ui/badge'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@kandev/ui/tooltip'; import type { LocalRepository, Task, Workspace, Repository, Environment, Executor, Branch } from '@/lib/types/http'; import type { AgentProfileOption } from '@/lib/state/slices'; import { DEFAULT_LOCAL_ENVIRONMENT_KIND, DEFAULT_LOCAL_EXECUTOR_TYPE, formatUserHomePath, selectPreferredBranch, truncateRepoPath, } from '@/lib/utils'; import { createTask, updateTask } from '@/lib/api'; import { useAppStore } from '@/components/state-provider'; import { useRepositories } from '@/hooks/domains/workspace/use-repositories'; import { useRepositoryBranches } from '@/hooks/domains/workspace/use-repository-branches'; import { useSettingsData } from '@/hooks/domains/settings/use-settings-data'; import { getWebSocketClient } from '@/lib/ws/connection'; import { SHORTCUTS } from '@/lib/keyboard/constants'; import { useKeyboardShortcutHandler } from '@/hooks/use-keyboard-shortcut'; import { KeyboardShortcutTooltip } from '@/components/keyboard-shortcut-tooltip'; import { useToast } from '@/components/toast-provider'; import { discoverRepositoriesAction, listLocalRepositoryBranchesAction } from '@/app/actions/workspaces'; import { getLocalStorage, setLocalStorage } from '@/lib/local-storage'; import { STORAGE_KEYS } from '@/lib/settings/constants'; import { linkToSession } from '@/lib/links'; interface TaskCreateDialogProps { open: boolean; onOpenChange: (open: boolean) => void; mode?: 'task' | 'session'; workspaceId: string | null; boardId: string | null; defaultColumnId: string | null; columns: Array<{ id: string; title: string; autoStartAgent?: boolean }>; editingTask?: { id: string; title: string; description?: string; workflowStepId: string; state?: Task['state']; repositoryId?: string } | null; onSuccess?: (task: Task, mode: 'create' | 'edit', meta?: { taskSessionId?: string | null }) => void; onCreateSession?: (data: { prompt: string; agentProfileId: string; executorId: string; environmentId: string; }) => void; initialValues?: { title: string; description?: string; repositoryId?: string; branch?: string; state?: Task['state']; }; submitLabel?: string; taskId?: string | null; /** Whether to navigate to the new session after task creation. Defaults to true. */ navigateOnSessionCreate?: boolean; } type RepositoryOption = { value: string; label: string; renderLabel: () => React.ReactNode; }; type RepositorySelectorProps = { options: RepositoryOption[]; value: string; onValueChange: (value: string) => void; disabled: boolean; placeholder: string; searchPlaceholder: string; emptyMessage: string; }; const RepositorySelector = memo(function RepositorySelector({ options, value, onValueChange, disabled, placeholder, searchPlaceholder, emptyMessage, }: RepositorySelectorProps) { return ( ); }); type BranchOption = { value: string; label: string; renderLabel: () => React.ReactNode; }; type BranchSelectorProps = { options: BranchOption[]; value: string; onValueChange: (value: string) => void; disabled: boolean; placeholder: string; searchPlaceholder: string; emptyMessage: string; }; const BranchSelector = memo(function BranchSelector({ options, value, onValueChange, disabled, placeholder, searchPlaceholder, emptyMessage, }: BranchSelectorProps) { return ( ); }); type AgentSelectorProps = { options: Array<{ value: string; label: string; renderLabel: () => React.ReactNode }>; value: string; onValueChange: (value: string) => void; disabled: boolean; placeholder: string; triggerClassName?: string; }; const AgentSelector = memo(function AgentSelector({ options, value, onValueChange, disabled, placeholder, triggerClassName, }: AgentSelectorProps) { return ( ); }); // Memoized text inputs to prevent re-rendering the entire dialog on every keystroke type TaskFormInputsProps = { isSessionMode: boolean; initialTitle: string; initialDescription: string; onTitleChange: (hasContent: boolean) => void; onDescriptionChange: (hasContent: boolean) => void; onKeyDown: (e: React.KeyboardEvent) => void; titleValueRef: React.RefObject<{ getValue: () => string } | null>; descriptionValueRef: React.RefObject<{ getValue: () => string } | null>; }; const TaskFormInputs = memo(function TaskFormInputs({ isSessionMode, initialTitle, initialDescription, onTitleChange, onDescriptionChange, onKeyDown, titleValueRef, descriptionValueRef, }: TaskFormInputsProps) { const [title, setTitle] = useState(initialTitle); const [description, setDescription] = useState(initialDescription); const textareaRef = useRef(null); // Expose getValue methods via refs passed from parent // This is an imperative handle pattern - refs are intentionally mutated useEffect(() => { const ref = titleValueRef as React.MutableRefObject<{ getValue: () => string } | null>; if (ref) { ref.current = { getValue: () => title }; } }, [title, titleValueRef]); useEffect(() => { const ref = descriptionValueRef as React.MutableRefObject<{ getValue: () => string } | null>; if (ref) { // eslint-disable-next-line react-hooks/immutability ref.current = { getValue: () => description }; } }, [description, descriptionValueRef]); // Auto-resize textarea useEffect(() => { const textarea = textareaRef.current; if (!textarea) return; textarea.style.height = 'auto'; textarea.style.height = `${textarea.scrollHeight}px`; }, [description]); const handleTitleChange = useCallback((e: React.ChangeEvent) => { const newValue = e.target.value; const hadContent = title.trim().length > 0; const hasContent = newValue.trim().length > 0; setTitle(newValue); if (hadContent !== hasContent) { onTitleChange(hasContent); } }, [title, onTitleChange]); const handleDescriptionChange = useCallback((e: React.ChangeEvent) => { const newValue = e.target.value; const hadContent = description.trim().length > 0; const hasContent = newValue.trim().length > 0; setDescription(newValue); if (hadContent !== hasContent) { onDescriptionChange(hasContent); } }, [description, onDescriptionChange]); return ( <> {!isSessionMode && (
)} {isSessionMode && (
)}
{isSessionMode && }