Files
opsxdb/src/DdlModal.tsx
T

119 lines
4.3 KiB
TypeScript

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<string | null>(null);
const [error, setError] = useState<string | null>(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 (
<OpsModal variant="info" wide title={`DDL · ${database ? `${database}.` : ""}${table}`} okLabel="关闭" onOk={onClose}>
{loading ? (
<div style={{ color: "var(--ops-muted)" }}>加载 DDL…</div>
) : error ? (
<div>
<div
className="rounded border px-2.5 py-1.5"
style={{
borderColor: "color-mix(in srgb, var(--ops-danger) 40%, var(--ops-border))",
background: "color-mix(in srgb, var(--ops-danger) 10%, transparent)",
color: "var(--ops-danger)",
fontSize: "var(--ops-text-sm)",
}}
>
{error}
</div>
<div className="mt-2 flex justify-end">
<button type="button" className="ops-btn-secondary h-6 px-2 text-[11px]" onClick={() => void load()}>
重试
</button>
</div>
</div>
) : (
<>
<div className="mb-1.5 flex items-center justify-end gap-1.5">
<button type="button" className="ops-btn-secondary h-6 px-2 text-[11px]" onClick={() => void copy()}>
复制
</button>
<button type="button" className="ops-btn-secondary h-6 px-2 text-[11px]" onClick={insertToEditor}>
插入编辑器
</button>
</div>
<pre
className="m-0 max-h-[50vh] overflow-auto whitespace-pre-wrap break-all rounded border px-2.5 py-2 font-mono leading-relaxed"
style={{
fontSize: "var(--ops-text-xs)",
borderColor: "var(--ops-border-soft)",
background: "var(--ops-editor-bg)",
color: "var(--ops-text)",
}}
>
{text}
</pre>
{truncated && (
<div className="mt-1.5" style={{ fontSize: "var(--ops-text-xs)", color: "var(--ops-warning)" }}>
内容过长,仅显示前 {DDL_CAP} 字符(共 {ddl?.length} 字符)。
</div>
)}
</>
)}
</OpsModal>
);
}