"use client"; import React, { useState, useEffect } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Badge } from "@/components/ui/badge"; import { Textarea } from "@/components/ui/textarea"; import { ArrowRight, Link, Key, Save, Globe, Plus } from "lucide-react"; import { DataFlowAPI, TableRelationship } from "@/lib/api/dataflow"; import toast from "react-hot-toast"; // 연결 정보 타입 interface ConnectionInfo { fromNode: { id: string; tableName: string; displayName: string; }; toNode: { id: string; tableName: string; displayName: string; }; fromColumn?: string; toColumn?: string; selectedColumnsData?: { [tableName: string]: { displayName: string; columns: string[]; }; }; } // 연결 설정 타입 interface ConnectionConfig { relationshipName: string; relationshipType: "one-to-one" | "one-to-many" | "many-to-one" | "many-to-many"; connectionType: "simple-key" | "data-save" | "external-call"; fromColumnName: string; toColumnName: string; settings?: Record; description?: string; } // 단순 키값 연결 설정 interface SimpleKeySettings { notes: string; } // 데이터 저장 설정 interface DataSaveSettings { sourceField: string; targetField: string; saveConditions: string; } // 외부 호출 설정 interface ExternalCallSettings { callType: "rest-api" | "email" | "webhook" | "ftp" | "queue"; apiUrl?: string; httpMethod?: "GET" | "POST" | "PUT" | "DELETE"; headers?: string; bodyTemplate?: string; } interface ConnectionSetupModalProps { isOpen: boolean; connection: ConnectionInfo | null; companyCode: string; onConfirm: (relationship: TableRelationship) => void; onCancel: () => void; } export const ConnectionSetupModal: React.FC = ({ isOpen, connection, companyCode, onConfirm, onCancel, }) => { const [config, setConfig] = useState({ relationshipName: "", relationshipType: "one-to-one", connectionType: "simple-key", fromColumnName: "", toColumnName: "", description: "", settings: {}, }); // 연결 종류별 설정 상태 const [simpleKeySettings, setSimpleKeySettings] = useState({ notes: "", }); const [dataSaveSettings, setDataSaveSettings] = useState({ sourceField: "", targetField: "", saveConditions: "", }); const [externalCallSettings, setExternalCallSettings] = useState({ callType: "rest-api", apiUrl: "", httpMethod: "POST", headers: "{}", bodyTemplate: "{}", }); // 모달이 열릴 때 기본값 설정 useEffect(() => { if (isOpen && connection) { const fromTableName = connection.fromNode.displayName; const toTableName = connection.toNode.displayName; setConfig({ relationshipName: `${fromTableName} → ${toTableName}`, relationshipType: "one-to-one", connectionType: "simple-key", fromColumnName: "", toColumnName: "", description: `${fromTableName}과 ${toTableName} 간의 데이터 관계`, settings: {}, }); // 단순 키값 연결 기본값 설정 setSimpleKeySettings({ notes: `${fromTableName}과 ${toTableName} 간의 키값 연결`, }); // 데이터 저장 기본값 설정 setDataSaveSettings({ sourceField: "", targetField: "", saveConditions: "데이터 저장 조건을 입력하세요", }); // 외부 호출 기본값 설정 setExternalCallSettings({ callType: "rest-api", apiUrl: "https://api.example.com/webhook", httpMethod: "POST", headers: "{}", bodyTemplate: "{}", }); } }, [isOpen, connection]); const handleConfirm = async () => { if (!config.relationshipName || !connection) { toast.error("필수 정보를 모두 입력해주세요."); return; } try { // 연결 종류별 설정을 준비 let settings = {}; switch (config.connectionType) { case "simple-key": settings = simpleKeySettings; break; case "data-save": settings = dataSaveSettings; break; case "external-call": settings = externalCallSettings; break; } // API 호출을 위한 관계 데이터 준비 const relationshipData: Omit = { relationshipName: config.relationshipName, fromTableName: connection.fromNode.tableName, fromColumnName: connection.fromColumn || "", toTableName: connection.toNode.tableName, toColumnName: connection.toColumn || "", relationshipType: config.relationshipType, connectionType: config.connectionType, companyCode: companyCode, settings: settings, isActive: "Y", }; toast.loading("관계를 생성하고 있습니다...", { id: "create-relationship" }); // API 호출 const createdRelationship = await DataFlowAPI.createRelationship(relationshipData); toast.success("관계가 성공적으로 생성되었습니다!", { id: "create-relationship" }); // 성공 콜백 호출 onConfirm(createdRelationship); handleCancel(); // 모달 닫기 } catch (error) { console.error("관계 생성 오류:", error); toast.error("관계 생성에 실패했습니다. 다시 시도해주세요.", { id: "create-relationship" }); } }; const handleCancel = () => { setConfig({ relationshipName: "", relationshipType: "one-to-one", connectionType: "simple-key", fromColumnName: "", toColumnName: "", description: "", }); onCancel(); }; if (!connection) return null; // 선택된 컬럼 데이터 가져오기 const selectedColumnsData = connection.selectedColumnsData || {}; const tableNames = Object.keys(selectedColumnsData); const fromTable = tableNames[0]; const toTable = tableNames[1]; const fromTableData = selectedColumnsData[fromTable]; const toTableData = selectedColumnsData[toTable]; // 연결 종류별 설정 패널 렌더링 const renderConnectionTypeSettings = () => { switch (config.connectionType) { case "simple-key": return (
단순 키값 연결 설정