import React, { useState, useEffect, useRef } from 'react'; import { useNavigate } from 'react-router-dom'; import { Bot, User, CheckCircle2, XCircle, Send, RefreshCcw, ShieldCheck } from 'lucide-react'; import useTicketStore from '../../store/ticketStore'; function AutoResolve() { const { aiTicket } = useTicketStore(); const navigate = useNavigate(); const [messages, setMessages] = useState([]); const [isThinking, setIsThinking] = useState(false); const [currentOptions, setCurrentOptions] = useState([]); const [isFinal, setIsFinal] = useState(false); const scrollRef = useRef(null); const fetchNextStep = async (history = []) => { setIsThinking(true); try { const response = await fetch('http://127.0.0.1:8000/ai/troubleshoot', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ text: aiTicket.summary, category: aiTicket.category, history: history }) }); const data = await response.json(); // Add bot message setMessages(prev => [...prev, { role: 'bot', text: data.step_text }]); setCurrentOptions(data.options || []); setIsFinal(data.is_final); } catch (error) { console.error("Troubleshooting Error:", error); setMessages(prev => [...prev, { role: 'bot', text: "I encountered an error connecting to the AI. Let's try basic troubleshooting first." }]); setCurrentOptions(["My internet is working", "I'm not sure"]); } finally { setIsThinking(false); } }; useEffect(() => { if (!aiTicket) { navigate('/create-ticket'); return; } // Initial fetch if (messages.length === 0) { fetchNextStep([]); } }, [aiTicket, navigate]); useEffect(() => { if (scrollRef.current) { scrollRef.current.scrollTop = scrollRef.current.scrollHeight; } }, [messages, isThinking]); const handleUserChoice = (choice) => { const newUserMsg = { role: 'user', text: choice }; const updatedHistory = [...messages, newUserMsg]; setMessages(prev => [...prev, newUserMsg]); setCurrentOptions([]); // Clear options while thinking fetchNextStep(updatedHistory); }; if (!aiTicket) return null; return (
{/* Header */}

Troubleshooting Assistant

Auto-Resolution for {aiTicket.summary}

AI Guided Mode
{/* Chat Area */}
{messages.map((msg, idx) => (
{msg.role === 'bot' ? : }
{msg.text}
))} {isThinking && (
)}
{/* Footer / Input */}
{!isFinal ? (
{currentOptions.map((option, idx) => ( ))}
) : (
)}
); } export default AutoResolve;