import { useState, useEffect } from "react"; import { api } from "@/lib/api"; import { DataSourceForm, type DataSourceConfig } from "@/components/DataSourceForm"; import { Button } from "@/components/ui/button"; import { Plus, Database, Pencil, Trash2, Loader2, FolderOpen } from "lucide-react"; import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { useAuthStore } from "@/store/authStore"; import { useProjectStore } from "@/store/projectStore"; import { useNavigate } from "react-router-dom"; export function DataSources() { const [datasources, setDatasources] = useState([]); const [isLoading, setIsLoading] = useState(false); const [isOpen, setIsOpen] = useState(false); const [editingDs, setEditingDs] = useState(null); const { user } = useAuthStore(); const { currentProject } = useProjectStore(); const navigate = useNavigate(); useEffect(() => { if (currentProject) { fetchDataSources(); } }, [currentProject]); const fetchDataSources = async () => { if (!currentProject) return; setIsLoading(true); try { const data = await api.get(`/api/v1/datasources?project_id=${currentProject.id}`); setDatasources(data); } catch (e) { console.error("Failed to fetch data sources", e); } finally { setIsLoading(false); } }; const handleCreate = () => { setEditingDs(null); setIsOpen(true); }; const handleEdit = (ds: DataSourceConfig) => { setEditingDs(ds); setIsOpen(true); }; const handleDelete = async (id: number) => { if (!window.confirm("确定要删除这个数据源吗?")) return; try { await api.delete(`/api/v1/datasources/${id}`); fetchDataSources(); } catch (e) { console.error("Failed to delete data source", e); } }; const handleSubmit = async (data: Omit) => { if (!currentProject) return; try { if (editingDs?.id) { await api.put(`/api/v1/datasources/${editingDs.id}`, { ...data, project_id: currentProject.id }); } else { await api.post("/api/v1/datasources", { ...data, project_id: currentProject.id }); } setIsOpen(false); fetchDataSources(); } catch (e) { console.error("Failed to save data source", e); alert("保存失败: " + (e as any).message); } }; const handleTest = async (type: string, config: Record) => { try { const res = await api.post<{ success: boolean; message: string }>("/api/v1/datasources/test", { type, config }); return res.success; } catch (e) { console.error("Test connection failed", e); throw e; } }; return (

数据源配置

管理可用于问答的数据源连接

{isLoading ? (
) : datasources.length === 0 ? (

暂无数据源

点击右上角按钮添加第一个数据源

) : (
{datasources.map((ds) => (

{ds.name}

{ds.type}

Host {ds.config.host || "Local / File"}
Database {ds.config.database || ds.config.file_path ? (ds.config.file_path?.split('/').pop()) : "-"}
))}
)}
{editingDs ? "编辑数据源" : "新建数据源"}
setIsOpen(false)} />
); }