import React, { useState } from 'react'; import { Button } from './ui/Button'; import { Card } from './ui/Card'; import { useVisualOrganizer } from '../hooks/useVisualOrganizer'; import { useDiagramStore } from '../store'; import { Sparkles, Wand2, Layout, Scan, CheckCircle2, RotateCcw } from 'lucide-react'; import type { LayoutSuggestion } from '../types/visualOrganization'; export const VisualOrganizerPanel: React.FC = () => { const { analyzeLayout, generateSuggestions, applySuggestion } = useVisualOrganizer(); const { nodes, edges, setNodes, setEdges } = useDiagramStore(); // UI States const [status, setStatus] = useState<'idle' | 'analyzing' | 'ready' | 'applied'>('idle'); const [bestSuggestion, setBestSuggestion] = useState(null); const [snapshot, setSnapshot] = useState<{ nodes: any[], edges: any[] } | null>(null); // AI Organize Handler const handleAIOrganize = async () => { setStatus('analyzing'); // 1. Analyze (Simulate brief delay for effect) analyzeLayout(); // 2. Generate Suggestions try { // Artificial delay for "Scanning" animation effect await new Promise(resolve => setTimeout(resolve, 1500)); const results = await generateSuggestions(); // Pick best suggestion (or default to first non-current) if (results.length > 0) { setBestSuggestion(results[0]); setStatus('ready'); } else { // Fallback if no suggestions setStatus('idle'); } } catch (error) { console.error(error); setStatus('idle'); } }; const handleApply = () => { if (!bestSuggestion) return; // Take snapshot before applying setSnapshot({ nodes: [...nodes], edges: [...edges] }); applySuggestion(bestSuggestion); setStatus('applied'); }; const handleUndo = () => { if (snapshot) { setNodes(snapshot.nodes); setEdges(snapshot.edges); setSnapshot(null); setStatus('ready'); } }; const handleReset = () => { setStatus('idle'); setSnapshot(null); setBestSuggestion(null); }; return (
{/* IDLE STATE: Main AI Button */} {status === 'idle' && (

AI Visual Organizer

Click to instantly analyze and reorganize your flow for maximum clarity.

)} {/* ANALYZING STATE: Scanning Animation */} {status === 'analyzing' && (

Analyzing Layout Logic...

)} {/* READY STATE: Suggestion Found */} {status === 'ready' && bestSuggestion && (
Optimization Found!

{bestSuggestion.title}

{bestSuggestion.description}

)} {/* APPLIED STATE: Success & Undo */} {status === 'applied' && (

Beautifully Organized!

Your graph has been transformed.

)}
); };