BindTools.tsx
8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/** 把元素落到数据上的两个工具:从数据表创建对象、一键补全还没绑的关系和属性。 */
import { Link2, Search } from "lucide-react";
import { useEffect, useMemo, useState } from "react";
import { Badge, Empty, Field, Modal, Spinner } from "@/components/ui";
import { api } from "@/lib/api";
import { go } from "@/lib/nav";
import { errText, onto, type AutobindResult } from "@/lib/onto";
import type { DataSource, SmModel } from "@/lib/types";
import { toast, loadFailed } from "@/store/useAppStore";
import { Select } from "@/components/kit";
/** 从一张数据表建对象:列成属性并绑定这张表。数据连接页和对象列表页用的是同一个。 */
export function FromTableModal({ dsId, table, onClose, onDone }: {
/** 已经知道是哪张表(从表列表里点进来)就直接带上 */
dsId?: number; table?: string; onClose: () => void; onDone?: () => void;
}) {
const [dss, setDss] = useState<DataSource[]>([]);
const [ds, setDs] = useState<number>(dsId || 0);
const [models, setModels] = useState<SmModel[] | null>(null);
const [map, setMap] = useState<Record<string, { concept_id: string; name: string }[]>>({});
const [pick, setPick] = useState(table || "");
const [name, setName] = useState("");
const [q, setQ] = useState("");
const [busy, setBusy] = useState(false);
useEffect(() => { void api.datasources().then((d) => { setDss(d); if (!ds && d[0]) setDs(d[0].id); }).catch((e) => { setDss([]); loadFailed(e); }); }, []); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
if (!ds) return;
setModels(null);
void api.sqlModels(ds).then(setModels).catch((e) => { setModels([]); loadFailed(e); });
void onto.mappings(ds).then(setMap).catch(() => setMap({}));
}, [ds]);
const tname = (m: SmModel) => m.physical_table || m.name;
const shown = useMemo(() => (models || []).filter((m) => !q || `${tname(m)} ${m.description}`.toLowerCase().includes(q.toLowerCase())), [models, q]);
const cur = (models || []).find((m) => tname(m) === pick);
useEffect(() => { setName(cur ? (cur.description || "").split(/[,。,((]/)[0].trim().slice(0, 30) || cur.name : ""); }, [cur?.id]); // eslint-disable-line react-hooks/exhaustive-deps
const create = async () => {
setBusy(true);
try {
const c = await onto.objectFromTable(ds, pick, name.trim());
toast("ok", `已建对象「${c.name}」`, "是草稿:补一下属性的中文名,再去发布");
onDone?.();
onClose();
go("b_object", { id: c.id });
} catch (e) { toast("error", "创建失败", errText(e)); } finally { setBusy(false); }
};
return (
<Modal open onClose={onClose} title="从数据表创建对象" width="max-w-2xl"
desc="表里每一行是一个对象,每一列是一个属性"
footer={<><button className="btn" onClick={onClose}>取消</button>
<button className="btn btn-primary" disabled={busy || !pick || !name.trim() || !!map[pick]?.length} onClick={() => void create()}>
{busy && <Spinner className="h-3.5 w-3.5" />}创建对象</button></>}>
<div className="space-y-3">
<div className="flex flex-wrap items-end gap-2">
<Field label="数据源" className="w-52">
<Select className="input" value={ds} onChange={(e) => { setDs(Number(e.target.value)); setPick(""); }}>
{!dss.length && <option value={0}>还没有接入数据源</option>}
{dss.map((d) => <option key={d.id} value={d.id}>{d.name}</option>)}
</Select>
</Field>
<div className="relative min-w-[12rem] flex-1">
<Search className="absolute left-2 top-2.5 h-3.5 w-3.5 text-muted" />
<input className="input pl-7" placeholder="搜表名 / 说明" value={q} onChange={(e) => setQ(e.target.value)} />
</div>
</div>
<div className="max-h-64 overflow-auto rounded-lg border border-line">
{!models ? <div className="p-3"><Spinner /></div> : !shown.length ? (
<Empty title={models.length ? "没有匹配的表" : "这个数据源还没有表结构"} desc={models.length ? "" : "先到「数据连接」同步一下表结构。"} />
) : shown.map((m) => {
const hit = map[tname(m)] || [];
return (
<button key={m.id} disabled={hit.length > 0} onClick={() => setPick(tname(m))}
className={`flex w-full items-center gap-2 border-b border-line px-3 py-2 text-left text-label last:border-0 ${pick === tname(m) ? "bg-brand/[0.06]" : "hover:bg-ink/[0.02]"} ${hit.length ? "opacity-60" : ""}`}>
<span className="font-mono font-medium">{tname(m)}</span>
<span className="min-w-0 flex-1 truncate text-muted" title={m.description || ""}>{m.description || ""}</span>
<span className="shrink-0 text-caption text-muted">{m.columns?.length || 0} 列</span>
{hit.map((h) => <Badge key={h.concept_id} tone="bg-ok/10 text-ok">已是「{h.name}」</Badge>)}
</button>
);
})}
</div>
{cur && (
<Field label="对象叫什么" required hint="用业务上的叫法">
<input className="input" value={name} onChange={(e) => setName(e.target.value)} />
</Field>
)}
</div>
</Modal>
);
}
/** 一键补全绑定:系统按表结构和列名给出建议,人勾选后落成草稿。 */
export function AutobindModal({ onClose, onDone }: { onClose: () => void; onDone?: () => void }) {
const [data, setData] = useState<AutobindResult | null>(null);
const [picked, setPicked] = useState<string[]>([]);
const [busy, setBusy] = useState(false);
useEffect(() => {
void onto.autobind().then((r) => { setData(r); setPicked(r.items.map((x) => x.id)); })
.catch((e) => { toast("error", "没算出来", errText(e)); setData({ items: [], skipped: [] }); });
}, []);
const apply = async () => {
setBusy(true);
try {
const r = await onto.applyAutobind(picked);
toast("ok", `补上了 ${r.count} 处绑定`, "都是草稿,去「发布与版本」发布后生效");
onDone?.();
onClose();
} catch (e) { toast("error", "保存失败", errText(e)); } finally { setBusy(false); }
};
return (
<Modal open onClose={onClose} title="一键补全绑定" width="max-w-2xl"
desc="只列出有把握的;拿不准的要手工选"
footer={<><button className="btn" onClick={onClose}>关闭</button>
<button className="btn btn-primary" disabled={busy || !picked.length} onClick={() => void apply()}>
{busy && <Spinner className="h-3.5 w-3.5" />}补上选中的 {picked.length || ""} 处</button></>}>
{!data ? <Spinner /> : (
<div className="space-y-3 text-label">
{!data.items.length && !data.skipped.length && <Empty title="没有要补的" desc="关系和属性都已经绑好了。" />}
{data.items.length > 0 && (
<div className="divide-y divide-line rounded-lg border border-line">
{data.items.map((it) => (
<label key={it.id} className="flex cursor-pointer items-start gap-2 px-3 py-2">
<input type="checkbox" className="mt-0.5" checked={picked.includes(it.id)}
onChange={(e) => setPicked((p) => (e.target.checked ? [...p, it.id] : p.filter((x) => x !== it.id)))} />
<span className="min-w-0 flex-1">
<span className="flex items-center gap-1.5 font-medium">
<Badge>{it.kind === "link_type" ? "关系" : "对象"}</Badge>{it.name}</span>
<span className="mt-0.5 block break-all font-mono text-caption">{it.text}</span>
<span className="block text-caption text-muted">依据:{it.why}</span>
</span>
</label>
))}
</div>
)}
{data.skipped.length > 0 && (
<div>
<p className="mb-1 font-medium text-muted">这些要手工绑</p>
<ul className="space-y-1">
{data.skipped.map((s) => (
<li key={s.concept_id} className="flex flex-wrap items-center gap-1.5">
<button className="chip" onClick={() => { onClose(); go("b_link", { id: s.concept_id }); }}><Link2 className="h-3 w-3" />{s.name}</button>
<span className="text-muted">{s.why}</span>
</li>
))}
</ul>
</div>
)}
</div>
)}
</Modal>
);
}