import { useMemo } from 'react' import { useTreeEditorStore } from '@/store/treeEditorStore' import type { NodeType } from '@/types' import { cn } from '@/lib/utils' // Special values for creating new nodes const CREATE_PREFIX = '__create_' const CREATE_DECISION = `${CREATE_PREFIX}decision__` const CREATE_ACTION = `${CREATE_PREFIX}action__` const CREATE_SOLUTION = `${CREATE_PREFIX}solution__` // Unicode symbols for node types (works in select options) const NODE_TYPE_SYMBOLS: Record = { decision: 'ⓘ', // Information/question symbol action: '⚡', // Lightning bolt for action solution: '✓' // Checkmark for solution } interface NodePickerProps { value: string onChange: (nodeId: string) => void /** The parent node ID - new nodes will be added as children of this node */ parentNodeId: string excludeNodeId?: string placeholder?: string className?: string label?: string error?: string /** Callback when a new node is created (receives the new node ID) */ onNodeCreated?: (nodeId: string) => void } export function NodePicker({ value, onChange, parentNodeId, excludeNodeId, placeholder = 'Select a node...', className, label, error, onNodeCreated }: NodePickerProps) { const { getAvailableTargetNodes, addNode } = useTreeEditorStore() const availableNodes = getAvailableTargetNodes(excludeNodeId) // Group nodes by type const groupedNodes = useMemo(() => { const decisions = availableNodes.filter(n => n.type === 'decision') const actions = availableNodes.filter(n => n.type === 'action') const solutions = availableNodes.filter(n => n.type === 'solution') return { decisions, actions, solutions } }, [availableNodes]) const handleChange = (selectedValue: string) => { // Check if it's a "create new" option if (selectedValue.startsWith(CREATE_PREFIX)) { let nodeType: NodeType if (selectedValue === CREATE_DECISION) { nodeType = 'decision' } else if (selectedValue === CREATE_ACTION) { nodeType = 'action' } else if (selectedValue === CREATE_SOLUTION) { nodeType = 'solution' } else { return } // Create the new node as a child of the parent const newNodeId = addNode(parentNodeId, nodeType) // Set this new node as the selected value onChange(newNodeId) // Notify parent if callback provided onNodeCreated?.(newNodeId) } else { // Normal selection onChange(selectedValue) } } // Find the label for the currently selected node const selectedNode = availableNodes.find(n => n.id === value) return (
{label && ( )} {/* Show what's selected */} {value && selectedNode && (

→ {selectedNode.label}

)} {error &&

{error}

}
) } export default NodePicker