import type React from 'react'; import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { CollapsibleSection } from '@/components/ui/CollapsibleSection'; import { Wand2, Loader2, PlusCircle, TrendingUp, TrendingDown } from 'lucide-react'; import { useUpdateTask, useUpdateSubtask, useScopeUpTask, useScopeDownTask } from '../../webview/hooks/useTaskQueries'; import type { TaskMasterTask } from '../../webview/types'; interface AIActionsSectionProps { currentTask: TaskMasterTask; isSubtask: boolean; parentTask?: TaskMasterTask | null; sendMessage: (message: any) => Promise; refreshComplexityAfterAI: () => void; onRegeneratingChange?: (isRegenerating: boolean) => void; onAppendingChange?: (isAppending: boolean) => void; } export const AIActionsSection: React.FC = ({ currentTask, isSubtask, parentTask, sendMessage, refreshComplexityAfterAI, onRegeneratingChange, onAppendingChange }) => { const [prompt, setPrompt] = useState(''); const [scopePrompt, setScopePrompt] = useState(''); const [scopeStrength, setScopeStrength] = useState< 'light' | 'regular' | 'heavy' >('regular'); const [lastAction, setLastAction] = useState< 'regenerate' | 'append' | 'scope-up' | 'scope-down' | null >(null); const updateTask = useUpdateTask(); const updateSubtask = useUpdateSubtask(); const scopeUpTask = useScopeUpTask(); const scopeDownTask = useScopeDownTask(); const handleRegenerate = async () => { if (!currentTask || !prompt.trim()) { return; } setLastAction('regenerate'); onRegeneratingChange?.(true); try { if (isSubtask && parentTask) { await updateSubtask.mutateAsync({ taskId: `${parentTask.id}.${currentTask.id}`, prompt: prompt, options: { research: false } }); } else { await updateTask.mutateAsync({ taskId: currentTask.id, updates: { description: prompt }, options: { append: false, research: false } }); } setPrompt(''); refreshComplexityAfterAI(); } catch (error) { console.error('❌ TaskDetailsView: Failed to regenerate task:', error); } finally { setLastAction(null); onRegeneratingChange?.(false); } }; const handleAppend = async () => { if (!currentTask || !prompt.trim()) { return; } setLastAction('append'); onAppendingChange?.(true); try { if (isSubtask && parentTask) { await updateSubtask.mutateAsync({ taskId: `${parentTask.id}.${currentTask.id}`, prompt: prompt, options: { research: false } }); } else { await updateTask.mutateAsync({ taskId: currentTask.id, updates: { description: prompt }, options: { append: true, research: false } }); } setPrompt(''); refreshComplexityAfterAI(); } catch (error) { console.error('❌ TaskDetailsView: Failed to append to task:', error); } finally { setLastAction(null); onAppendingChange?.(false); } }; const handleScopeUp = async () => { if (!currentTask) { return; } setLastAction('scope-up'); try { const taskId = isSubtask && parentTask ? `${parentTask.id}.${currentTask.id}` : currentTask.id; await scopeUpTask.mutateAsync({ taskId, strength: scopeStrength, prompt: scopePrompt.trim() || undefined, options: { research: false } }); setScopePrompt(''); refreshComplexityAfterAI(); } catch (error) { console.error('❌ AIActionsSection: Failed to scope up task:', error); } finally { setLastAction(null); } }; const handleScopeDown = async () => { if (!currentTask) { return; } setLastAction('scope-down'); try { const taskId = isSubtask && parentTask ? `${parentTask.id}.${currentTask.id}` : currentTask.id; await scopeDownTask.mutateAsync({ taskId, strength: scopeStrength, prompt: scopePrompt.trim() || undefined, options: { research: false } }); setScopePrompt(''); refreshComplexityAfterAI(); } catch (error) { console.error('❌ AIActionsSection: Failed to scope down task:', error); } finally { setLastAction(null); } }; // Track loading states based on the last action const isLoading = updateTask.isPending || updateSubtask.isPending || scopeUpTask.isPending || scopeDownTask.isPending; const isRegenerating = isLoading && lastAction === 'regenerate'; const isAppending = isLoading && lastAction === 'append'; const isScopingUp = isLoading && lastAction === 'scope-up'; const isScopingDown = isLoading && lastAction === 'scope-down'; return (
{/* Standard AI Actions Section */}