import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { ChevronRight, RotateCcw, ExternalLink, Info } from 'lucide-react'; import { DecisionTree, DecisionNode, getRootNode, getNodeById } from '@/data/decision-trees'; import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; import { Alert, AlertDescription } from '@/components/ui/alert'; import Badge from '@/components/ui/Badge'; interface DecisionTreeViewerProps { tree: DecisionTree; onReset?: () => void; } const DecisionTreeViewer = ({ tree, onReset }: DecisionTreeViewerProps) => { const navigate = useNavigate(); const [currentNodeId, setCurrentNodeId] = useState(tree.rootNodeId); const [history, setHistory] = useState([tree.rootNodeId]); const currentNode = getNodeById(tree, currentNodeId); const rootNode = getRootNode(tree); if (!currentNode || !rootNode) { return ( Error: Nodo no encontrado en el árbol de decisión. ); } const handleAnswer = (answer: 'yes' | 'no') => { const nextNodeId = answer === 'yes' ? currentNode.yes : currentNode.no; if (!nextNodeId) { return; // No hay siguiente nodo } const nextNode = getNodeById(tree, nextNodeId); if (!nextNode) { return; // Nodo siguiente no encontrado } // Si es una acción o redirección, no avanzar más if (nextNode.type === 'action' || nextNode.type === 'redirect') { setCurrentNodeId(nextNodeId); setHistory([...history, nextNodeId]); return; } // Si es una pregunta, avanzar normalmente setCurrentNodeId(nextNodeId); setHistory([...history, nextNodeId]); }; const handleReset = () => { setCurrentNodeId(tree.rootNodeId); setHistory([tree.rootNodeId]); if (onReset) { onReset(); } }; const handleRedirect = (path?: string) => { if (path) { navigate(path); } }; const handleBack = () => { if (history.length > 1) { const newHistory = [...history]; newHistory.pop(); // Eliminar el actual const previousNodeId = newHistory[newHistory.length - 1]; setCurrentNodeId(previousNodeId); setHistory(newHistory); } }; const canGoBack = history.length > 1; const isQuestion = currentNode.type === 'question'; const isAction = currentNode.type === 'action' || currentNode.type === 'redirect'; return (
{/* Header */}

{tree.title}

{tree.description}

{/* Progress indicator */} {history.length > 1 && (
Paso {history.length} {canGoBack && ( )}
)} {/* Current Node Card */} {/* Question or Action */}
{isQuestion && (
?

{currentNode.text}

)} {isAction && (

{currentNode.text}

{currentNode.action && (

{currentNode.action}

)}
{currentNode.notes && ( {currentNode.notes} )} {currentNode.redirectTo && ( )}
)}
{/* Answer Buttons (only for questions) */} {isQuestion && (
)} {/* Action buttons for actions */} {isAction && (
{canGoBack && ( )}
)}
{/* Source reference */} {tree.source && (

Fuente: {tree.source}

)}
); }; export default DecisionTreeViewer;