import { useCallback, useEffect, useState } from "react"; import { getDdl } from "./api"; import { ensureSession } from "./ObjectTree"; import { useWorkspaceStore, type SavedConnection } from "./store"; import { OpsModal } from "./OpsModal"; /** 超长 DDL 截断阈值(R2-F3):超过只展示前 5000 字符并提示 */ const DDL_CAP = 5000; type Props = { conn: SavedConnection; database: string; table: string; onClose: () => void; }; /** * R2-F3:DDL 查看 —— OpsModal(info 变体)展示服务端返回的建表语句。 * 等宽 pre + 加载态 + 错误重试;「复制」「插入编辑器」把 DDL 带到剪贴板/当前 Tab。 */ export function DdlModal({ conn, database, table, onClose }: Props) { const notify = useWorkspaceStore((s) => s.notify); const [ddl, setDdl] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(true); const load = useCallback(async () => { setLoading(true); setError(null); try { const sid = await ensureSession(conn); const res = await getDdl(conn, table, { database, sessionId: sid }); if (res.error) throw new Error(res.error); setDdl(res.ddl ?? "(空)"); } catch (e) { setError(e instanceof Error ? e.message : String(e)); setDdl(null); } finally { setLoading(false); } }, [conn, database, table]); useEffect(() => { void load(); }, [load]); const truncated = ddl != null && ddl.length > DDL_CAP; const text = truncated ? (ddl ?? "").slice(0, DDL_CAP) : (ddl ?? ""); const copy = async () => { await navigator.clipboard.writeText(text); notify({ kind: "success", title: "已复制", message: `${database}.${table} DDL`, mode: "toast", durationMs: 1400 }); }; /** 插入编辑器:DDL 追加到当前激活 Tab 的 SQL 末尾 */ const insertToEditor = () => { const st = useWorkspaceStore.getState(); const tab = st.tabs.find((t) => t.id === st.activeTabId); const cur = tab?.sql ?? ""; st.updateActiveSql(cur.trim() ? `${cur.replace(/\s+$/, "")}\n\n${text}` : text); notify({ kind: "success", title: "已插入编辑器", message: `${database}.${table} DDL`, mode: "toast", durationMs: 1600 }); onClose(); }; return ( {loading ? (
加载 DDL…
) : error ? (
{error}
) : ( <>
            {text}
          
{truncated && (
内容过长,仅显示前 {DDL_CAP} 字符(共 {ddl?.length} 字符)。
)} )}
); }