import { html } from 'htm/preact'; import { useState, useRef, useEffect } from 'preact/hooks'; const styles = { container: { position: 'relative', display: 'inline-block' }, button: { display: 'flex', alignItems: 'center', gap: '8px', padding: '8px 16px', background: 'var(--bg-elevated)', border: '1px solid var(--border-color)', borderRadius: 'var(--radius-md)', color: 'var(--text-primary)', fontSize: '14px', fontWeight: 500, cursor: 'pointer', transition: 'var(--transition)' }, dropdown: (isOpen) => ({ position: 'absolute', top: 'calc(100% + 4px)', right: 0, minWidth: '240px', maxHeight: '400px', overflowY: 'auto', background: 'var(--bg-elevated)', border: '1px solid var(--border-color)', borderRadius: 'var(--radius-md)', boxShadow: 'var(--shadow-lg)', zIndex: 100, display: isOpen ? 'block' : 'none' }), menuItem: (isSelected) => ({ display: 'flex', alignItems: 'center', gap: '12px', padding: '10px 16px', cursor: 'pointer', transition: 'var(--transition)', background: isSelected ? 'var(--primary-bg)' : 'transparent', color: 'var(--text-primary)', fontSize: '14px' }), checkbox: (isSelected) => ({ width: '18px', height: '18px', display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0, color: isSelected ? 'var(--primary)' : 'transparent' }), fieldInfo: { flex: 1, minWidth: 0, display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: '8px' }, fieldName: { display: 'flex', alignItems: 'center', gap: '6px', flex: 1, minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }, fieldType: { fontSize: '10px', color: 'var(--primary)', background: 'var(--primary-bg)', padding: '2px 5px', borderRadius: 'var(--radius-sm)', fontFamily: '"Courier New", monospace', flexShrink: 0 } }; export function ColumnSelector({ fields, selectedColumns, onToggle }) { const [isOpen, setIsOpen] = useState(false); const dropdownRef = useRef(null); const buttonRef = useRef(null); useEffect(() => { const handleClickOutside = (event) => { if ( dropdownRef.current && buttonRef.current && !dropdownRef.current.contains(event.target) && !buttonRef.current.contains(event.target) ) { setIsOpen(false); } }; if (isOpen) { document.addEventListener('mousedown', handleClickOutside); } return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, [isOpen]); const selectedCount = selectedColumns.length; const totalCount = fields?.length || 0; return html`
${fields?.map(field => { const isSelected = selectedColumns.includes(field.name); return html`
onToggle(field.name)} onMouseEnter=${(e) => { if (!isSelected) { e.currentTarget.style.background = 'var(--bg-hover)'; } }} onMouseLeave=${(e) => { if (!isSelected) { e.currentTarget.style.background = 'transparent'; } }} >
${isSelected ? '✓' : ''}
${field.name} ${field.indexed && html`🔍`}
${field.type}
`; })}
`; }