"use client"; import React, { useState, useEffect } from "react"; 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 { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Badge } from "@/components/ui/badge"; import { useToast } from "@/hooks/use-toast"; import { flowExternalDbApi } from "@/lib/api/flowExternalDb"; import { FlowExternalDbConnection, CreateFlowExternalDbConnectionRequest, UpdateFlowExternalDbConnectionRequest, DB_TYPE_OPTIONS, getDbTypeLabel, } from "@/types/flowExternalDb"; import { Plus, Pencil, Trash2, TestTube, Loader2 } from "lucide-react"; import { Switch } from "@/components/ui/switch"; export default function FlowExternalDbPage() { const { toast } = useToast(); const [connections, setConnections] = useState([]); const [loading, setLoading] = useState(true); const [showDialog, setShowDialog] = useState(false); const [editingConnection, setEditingConnection] = useState(null); const [testingId, setTestingId] = useState(null); // 폼 상태 const [formData, setFormData] = useState< CreateFlowExternalDbConnectionRequest | UpdateFlowExternalDbConnectionRequest >({ name: "", description: "", dbType: "postgresql", host: "", port: 5432, databaseName: "", username: "", password: "", sslEnabled: false, }); useEffect(() => { loadConnections(); }, []); const loadConnections = async () => { try { setLoading(true); const response = await flowExternalDbApi.getAll(); if (response.success) { setConnections(response.data); } } catch (error: any) { toast({ title: "오류", description: error.message || "외부 DB 연결 목록 조회 실패", variant: "destructive", }); } finally { setLoading(false); } }; const handleCreate = () => { setEditingConnection(null); setFormData({ name: "", description: "", dbType: "postgresql", host: "", port: 5432, databaseName: "", username: "", password: "", sslEnabled: false, }); setShowDialog(true); }; const handleEdit = (connection: FlowExternalDbConnection) => { setEditingConnection(connection); setFormData({ name: connection.name, description: connection.description, host: connection.host, port: connection.port, databaseName: connection.databaseName, username: connection.username, password: "", // 비밀번호는 비워둠 sslEnabled: connection.sslEnabled, isActive: connection.isActive, }); setShowDialog(true); }; const handleSave = async () => { try { if (editingConnection) { // 수정 await flowExternalDbApi.update(editingConnection.id, formData); toast({ title: "성공", description: "외부 DB 연결이 수정되었습니다" }); } else { // 생성 await flowExternalDbApi.create(formData as CreateFlowExternalDbConnectionRequest); toast({ title: "성공", description: "외부 DB 연결이 생성되었습니다" }); } setShowDialog(false); loadConnections(); } catch (error: any) { toast({ title: "오류", description: error.message, variant: "destructive", }); } }; const handleDelete = async (id: number, name: string) => { if (!confirm(`"${name}" 연결을 삭제하시겠습니까?`)) { return; } try { await flowExternalDbApi.delete(id); toast({ title: "성공", description: "외부 DB 연결이 삭제되었습니다" }); loadConnections(); } catch (error: any) { toast({ title: "오류", description: error.message, variant: "destructive", }); } }; const handleTestConnection = async (id: number, name: string) => { setTestingId(id); try { const result = await flowExternalDbApi.testConnection(id); toast({ title: result.success ? "연결 성공" : "연결 실패", description: result.message, variant: result.success ? "default" : "destructive", }); } catch (error: any) { toast({ title: "오류", description: error.message, variant: "destructive", }); } finally { setTestingId(null); } }; return (

외부 DB 연결 관리

플로우에서 사용할 외부 데이터베이스 연결을 관리합니다

{loading ? (
) : connections.length === 0 ? (

등록된 외부 DB 연결이 없습니다

) : (
이름 DB 타입 호스트 데이터베이스 상태 작업 {connections.map((conn) => (
{conn.name}
{conn.description &&
{conn.description}
}
{getDbTypeLabel(conn.dbType)} {conn.host}:{conn.port} {conn.databaseName} {conn.isActive ? "활성" : "비활성"}
))}
)} {/* 생성/수정 다이얼로그 */} {editingConnection ? "외부 DB 연결 수정" : "새 외부 DB 연결 추가"} 외부 데이터베이스 연결 정보를 입력하세요
setFormData({ ...formData, name: e.target.value })} placeholder="예: 운영_PostgreSQL" />
setFormData({ ...formData, description: e.target.value })} placeholder="연결에 대한 설명" />
setFormData({ ...formData, host: e.target.value })} placeholder="localhost" />
setFormData({ ...formData, port: parseInt(e.target.value) || 0 })} />
setFormData({ ...formData, databaseName: e.target.value })} placeholder="mydb" />
setFormData({ ...formData, username: e.target.value })} placeholder="dbuser" />
setFormData({ ...formData, password: e.target.value })} placeholder={editingConnection ? "변경하지 않으려면 비워두세요" : "비밀번호"} />
setFormData({ ...formData, sslEnabled: checked })} />
{editingConnection && (
setFormData({ ...formData, isActive: checked })} />
)}
); }