import { useState } from 'react' import { Link, useNavigate } from 'react-router-dom' import { Plus, ChevronDown, Sparkles, FolderTree, ListOrdered } from 'lucide-react' import { cn } from '@/lib/utils' import { editorAIApi } from '@/api/editorAI' import { apiClient } from '@/api/client' import { AIPromptDialog } from '@/components/editor-ai/AIPromptDialog' type AIFlowType = 'troubleshooting' | 'procedural' | 'maintenance' interface CreateFlowDropdownProps { aiEnabled: boolean className?: string /** Button label — defaults to "Create Flow" */ label?: string } export function CreateFlowDropdown({ aiEnabled, className, label = 'Create Flow', }: CreateFlowDropdownProps) { const [showMenu, setShowMenu] = useState(false) const [aiPromptOpen, setAiPromptOpen] = useState(false) const [aiPromptFlowType, setAiPromptFlowType] = useState('troubleshooting') const navigate = useNavigate() const handleAIGenerate = async (prompt: string) => { // Start an AI session const session = await editorAIApi.startSession( aiPromptFlowType === 'maintenance' ? 'procedural' : aiPromptFlowType ) const sessionId = session.session_id // Send the user's prompt await editorAIApi.sendMessage({ sessionId, content: prompt, actionType: 'generate_full', }) // Generate the full flow await editorAIApi.generateFull(sessionId) // Import to create the tree const { data: importResult } = await apiClient.post( `/ai/chat/sessions/${sessionId}/import`, {} ) const treeId = importResult.tree_id // Navigate to the editor if (aiPromptFlowType === 'troubleshooting') { navigate(`/trees/${treeId}/edit`, { state: { aiPanelOpen: true, sessionId }, }) } else { navigate(`/flows/${treeId}/edit`, { state: { aiPanelOpen: true, sessionId }, }) } } return (
{showMenu && ( <>
setShowMenu(false)} />
{/* Troubleshooting */} setShowMenu(false)} className="flex items-center gap-3 rounded-md px-3 py-2.5 text-sm text-foreground hover:bg-accent" >
Troubleshooting Tree
Branching decision flow
{aiEnabled && ( )}
{/* Procedural */} setShowMenu(false)} className="flex items-center gap-3 rounded-md px-3 py-2.5 text-sm text-foreground hover:bg-accent" >
Procedural Flow
Step-by-step procedure
{aiEnabled && ( )}
{aiEnabled && ( )}
)} setAiPromptOpen(false)} onGenerate={handleAIGenerate} flowType={aiPromptFlowType} />
) }