'use client'; import { useState, useEffect, useCallback } from 'react'; import { Eye, Wrench, Save, FolderOpen } from 'lucide-react'; import { useControlMode } from './hooks/useControlMode'; import { getBusinessRuleList, getBusinessRuleInfo, insertBusinessRule, updateBusinessRule } from '@/lib/api/businessRule'; import { toast } from 'sonner'; interface ControlToolbarProps { dashboardId: string; } export function ControlToolbar({ dashboardId }: ControlToolbarProps) { const { mode, setMode, ruleNodes, ruleConnections, activeRuleId, setActiveRuleId, setRuleNodes, setRuleConnections } = useControlMode(); const [ruleList, setRuleList] = useState[]>([]); const [showRuleList, setShowRuleList] = useState(false); // ★ 편집 모드 진입 시 기존 규칙 목록 로드 useEffect(() => { if (mode !== 'edit') return; getBusinessRuleList(dashboardId) .then((res) => setRuleList(res?.list ?? res?.data ?? [])) .catch(() => setRuleList([])); }, [mode, dashboardId]); // ★ 기존 규칙 로드 → 편집 상태 복원 const handleLoadRule = useCallback(async (ruleId: string) => { try { const detail = await getBusinessRuleInfo(ruleId); if (!detail) { toast.error('규칙을 찾을 수 없습니다'); return; } setRuleNodes(detail.nodes ?? []); setRuleConnections(detail.connections ?? []); setActiveRuleId(ruleId); setShowRuleList(false); toast.success(`"${detail.name ?? ruleId}" 로드됨`); } catch { toast.error('규칙 로드 실패'); } }, [setRuleNodes, setRuleConnections, setActiveRuleId]); const handleSave = async () => { try { const data = { name: `규칙 ${new Date().toLocaleString('ko-KR')}`, nodes: ruleNodes, connections: ruleConnections, }; if (activeRuleId) { await updateBusinessRule(activeRuleId, data); toast.success('규칙 저장됨'); } else { const result = await insertBusinessRule(dashboardId, data); if (result?.rule_id) setActiveRuleId(result.rule_id); toast.success('규칙 생성됨'); } } catch { toast.error('저장 실패'); } }; return (
⚡ 제어 모드
{mode === 'edit' && (
{/* ★ 기존 규칙 로드 버튼 */} {/* ★ 규칙 목록 드롭다운 */} {showRuleList && ruleList.length > 0 && (
{ruleList.map((rule) => { const id = rule.rule_id ?? rule.RULE_ID; const name = rule.name ?? rule.NAME ?? id; const isActive = id === activeRuleId; return ( ); })}
)} {ruleNodes.length > 0 && ( )}
)}
); }