"use client"; import React, { useState, useEffect } from "react"; import { ChartDataSource, ExternalConnection, ApiResponse } from "../types"; import { Card } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Button } from "@/components/ui/button"; import { ExternalLink, Database, Server } from "lucide-react"; interface DatabaseConfigProps { dataSource: ChartDataSource; onChange: (updates: Partial) => void; } /** * 데이터베이스 설정 컴포넌트 * - 현재 DB / 외부 DB 선택 * - 외부 커넥션 목록 불러오기 */ export function DatabaseConfig({ dataSource, onChange }: DatabaseConfigProps) { const [connections, setConnections] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); // 외부 커넥션 목록 불러오기 useEffect(() => { if (dataSource.connectionType === "external") { loadExternalConnections(); } }, [dataSource.connectionType]); const loadExternalConnections = async () => { setLoading(true); setError(null); try { const response = await fetch("http://localhost:8080/api/external-connections", { headers: { Authorization: `Bearer ${localStorage.getItem("token") || "test-token"}`, }, }); if (!response.ok) { throw new Error("외부 커넥션 목록을 불러오는데 실패했습니다"); } const result: ApiResponse = await response.json(); if (!result.success) { throw new Error(result.message || "외부 커넥션 목록을 불러오는데 실패했습니다"); } setConnections(result.data || []); } catch (err) { const errorMessage = err instanceof Error ? err.message : "알 수 없는 오류가 발생했습니다"; setError(errorMessage); } finally { setLoading(false); } }; // 현재 선택된 커넥션 찾기 const selectedConnection = connections.find((conn) => conn.id === dataSource.externalConnectionId); return (

2단계: 데이터베이스 설정

데이터를 조회할 데이터베이스를 선택하세요

{/* 현재 DB vs 외부 DB 선택 */}
{/* 외부 DB 선택 시 커넥션 목록 */} {dataSource.connectionType === "external" && (
{loading && (
커넥션 목록 불러오는 중...
)} {error && (
❌ {error}
)} {!loading && !error && connections.length === 0 && (
등록된 외부 커넥션이 없습니다
)} {!loading && !error && connections.length > 0 && ( <> {selectedConnection && (
커넥션명: {selectedConnection.name}
타입: {selectedConnection.type.toUpperCase()}
)} )} )} {/* 다음 단계 안내 */} {(dataSource.connectionType === "current" || (dataSource.connectionType === "external" && dataSource.externalConnectionId)) && (
✅ 데이터베이스가 선택되었습니다. 아래에서 SQL 쿼리를 작성하세요.
)}
); }