import React, { useEffect, useMemo, useState } from 'react'; import { INDICATOR_REGISTRY, type IndicatorDef } from '../utils/indicatorRegistry'; import { MAIN_INDICATOR_LABELS } from '../utils/indicatorMainTab'; interface IndicatorCustomTabEditorProps { mode: 'create' | 'edit'; initialName: string; initialTypes: string[]; onSave: (name: string, indicatorTypes: string[]) => void; onClose: () => void; } function indicatorLabel(def: IndicatorDef): { ko: string; en: string } { const main = MAIN_INDICATOR_LABELS[def.type]; return main ?? { ko: def.description, en: def.name }; } const IndicatorCustomTabEditor: React.FC = ({ mode, initialName, initialTypes, onSave, onClose, }) => { const [name, setName] = useState(initialName); const [selected, setSelected] = useState>(() => new Set(initialTypes)); const [search, setSearch] = useState(''); useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') { e.stopImmediatePropagation(); onClose(); } }; window.addEventListener('keydown', onKey, true); return () => window.removeEventListener('keydown', onKey, true); }, [onClose]); const filtered = useMemo(() => { const q = search.toLowerCase().trim(); if (!q) return INDICATOR_REGISTRY; return INDICATOR_REGISTRY.filter(d => { const lbl = indicatorLabel(d); return ( lbl.ko.toLowerCase().includes(q) || lbl.en.toLowerCase().includes(q) || d.shortName.toLowerCase().includes(q) || d.type.toLowerCase().includes(q) ); }); }, [search]); const toggle = (type: string) => { setSelected(prev => { const next = new Set(prev); if (next.has(type)) next.delete(type); else next.add(type); return next; }); }; const handleSave = () => { const trimmed = name.trim(); if (!trimmed) { window.alert('탭 이름을 입력해 주세요.'); return; } if (selected.size === 0) { window.alert('탭에 표시할 지표를 하나 이상 선택해 주세요.'); return; } const ordered = INDICATOR_REGISTRY .filter(d => selected.has(d.type)) .map(d => d.type); onSave(trimmed, ordered); }; return (
{ if (e.target === e.currentTarget) onClose(); }} >
e.stopPropagation()}>
{mode === 'create' ? '탭 추가' : '탭 수정'}
지표 선택 ({selected.size}개)
setSearch(e.target.value)} placeholder="지표 검색..." />
{filtered.length === 0 ? (
검색 결과 없음
) : ( filtered.map(def => { const lbl = indicatorLabel(def); const checked = selected.has(def.type); return ( ); }) )}
); }; export default IndicatorCustomTabEditor;