5 处逐字硬编码 profile?.account==='admin'||userId==='ADMIN'(Topbar/ModelerPage/NewFeatureShell/AiFeatureForm/DesignCenterPage)收口到 auth/token.ts 纯函数 isAdminProfile,口径不变。
顺带:5 个页面测试的 vi.mock('../auth/token') 从整模块替换改为 importOriginal 只覆盖 getProfile(不再 mock 纯函数,修复 mock 漏 isAdminProfile 致的 render 崩)。
验证:新增 token.test.ts 4 测试 + tsc -b 干净 + vitest 18 文件/77 测试全绿。见总览 §10 B4。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
226 lines
8.8 KiB
TypeScript
226 lines
8.8 KiB
TypeScript
// 建模器 —— NocoBase 式实体建模入口(五环第①环:元数据→表)。
|
||
// 仅管理员可用。展示已有实体清单 + 「新建实体」Dialog + 行内「加字段」Dialog。
|
||
// v2:可建新实体 + 给已有实体加字段(受控 ALTER 安全通道,只加列/恒可空)。字段表见同文件下方组件。
|
||
import { useEffect, useState } from 'react';
|
||
import { Dialog } from '../components/ui/Dialog';
|
||
import { getProfile, isAdminProfile } from '../auth/token';
|
||
import {
|
||
listEntities,
|
||
createEntity,
|
||
alterEntity,
|
||
type EntitySummary,
|
||
type EntityAttr,
|
||
} from '../api/meta';
|
||
import { FieldTable, buildAttribute, type FieldRow } from './ModelerFields';
|
||
|
||
export function validateTableName(t: string): boolean {
|
||
return /^[A-Z][A-Z0-9_]{2,40}$/.test(t);
|
||
}
|
||
|
||
export function ModelerPage() {
|
||
const profile = getProfile();
|
||
const isAdmin = isAdminProfile(profile);
|
||
|
||
const [entities, setEntities] = useState<EntitySummary[]>([]);
|
||
const [loading, setLoading] = useState(true);
|
||
const [error, setError] = useState('');
|
||
const [open, setOpen] = useState(false);
|
||
|
||
// 新建表单态
|
||
const [tablename, setTablename] = useState('');
|
||
const [name, setName] = useState('');
|
||
const [rows, setRows] = useState<FieldRow[]>([]);
|
||
const [submitting, setSubmitting] = useState(false);
|
||
const [submitErr, setSubmitErr] = useState('');
|
||
|
||
// 加字段(改表)态
|
||
const [alterTarget, setAlterTarget] = useState<string | null>(null);
|
||
const [alterRows, setAlterRows] = useState<FieldRow[]>([]);
|
||
const [alterSubmitting, setAlterSubmitting] = useState(false);
|
||
const [alterErr, setAlterErr] = useState('');
|
||
|
||
function reload() {
|
||
setLoading(true);
|
||
listEntities()
|
||
.then((es) => setEntities(es))
|
||
.catch((e: Error) => setError(e.message ?? '实体加载失败'))
|
||
.finally(() => setLoading(false));
|
||
}
|
||
|
||
useEffect(() => {
|
||
if (!isAdmin) return;
|
||
reload();
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||
}, [isAdmin]);
|
||
|
||
if (!isAdmin) {
|
||
return <div style={{ padding: 24, color: 'var(--text-3)' }}>建模器仅管理员可用。</div>;
|
||
}
|
||
|
||
const tableValid = validateTableName(tablename);
|
||
// 引用行必须选了目标表才合法(Task 17 填充 referenceTable)
|
||
const rowsValid = rows.every((r) => r.type !== '引用' || !!r.referenceTable);
|
||
const canSubmit = tableValid && rowsValid && !submitting;
|
||
// 加字段:至少一列,引用列必须选了目标表
|
||
const alterRowsValid = alterRows.length > 0 && alterRows.every((r) => r.type !== '引用' || !!r.referenceTable);
|
||
const canAlter = alterRowsValid && !alterSubmitting;
|
||
|
||
function openDialog() {
|
||
setTablename('');
|
||
setName('');
|
||
setRows([]);
|
||
setSubmitErr('');
|
||
setOpen(true);
|
||
}
|
||
|
||
async function submit() {
|
||
setSubmitting(true);
|
||
setSubmitErr('');
|
||
try {
|
||
const attributes: EntityAttr[] = rows.map(buildAttribute);
|
||
await createEntity({ tablename, name: name || undefined, attributes });
|
||
setOpen(false);
|
||
reload();
|
||
} catch (e) {
|
||
setSubmitErr((e as Error).message ?? '建表失败');
|
||
} finally {
|
||
setSubmitting(false);
|
||
}
|
||
}
|
||
|
||
function openAlter(table: string) {
|
||
setAlterTarget(table);
|
||
setAlterRows([]);
|
||
setAlterErr('');
|
||
}
|
||
|
||
async function submitAlter() {
|
||
if (!alterTarget) return;
|
||
setAlterSubmitting(true);
|
||
setAlterErr('');
|
||
try {
|
||
const attributes: EntityAttr[] = alterRows.map(buildAttribute);
|
||
await alterEntity(alterTarget, attributes);
|
||
setAlterTarget(null);
|
||
reload();
|
||
} catch (e) {
|
||
setAlterErr((e as Error).message ?? '加字段失败');
|
||
} finally {
|
||
setAlterSubmitting(false);
|
||
}
|
||
}
|
||
|
||
return (
|
||
<div style={{ height: '100%', overflow: 'auto', padding: 'var(--pad-lg)' }} data-testid="modeler">
|
||
<div style={{ display: 'flex', alignItems: 'center', gap: 16, marginBottom: 16, flexWrap: 'wrap' }}>
|
||
<div>
|
||
<h2 style={{ margin: 0, fontSize: 18, fontWeight: 600, color: 'var(--text)' }}>建模器</h2>
|
||
<div className="sub-text" style={{ marginTop: 4 }}>
|
||
新建实体(建表)。v1 只建新不改表 · 标准列由后端自动追加
|
||
</div>
|
||
</div>
|
||
<div style={{ flex: 1 }} />
|
||
<button className="btn btn-primary" data-testid="modeler-new-entity" onClick={openDialog}>
|
||
新建实体
|
||
</button>
|
||
</div>
|
||
|
||
{loading ? (
|
||
<div style={{ padding: 24, color: 'var(--text-3)' }}>加载中…</div>
|
||
) : error ? (
|
||
<div style={{ padding: 16, color: 'var(--err)' }}>实体加载失败:{error}</div>
|
||
) : entities.length === 0 ? (
|
||
<div className="empty">尚无实体,点「新建实体」开始</div>
|
||
) : (
|
||
<div className="card" style={{ padding: 0 }}>
|
||
<table style={{ width: '100%', borderCollapse: 'collapse', fontSize: 'var(--fs)' }}>
|
||
<thead>
|
||
<tr style={{ textAlign: 'left', color: 'var(--text-3)' }}>
|
||
<th style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)' }}>表名</th>
|
||
<th style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)' }}>显示名</th>
|
||
<th style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)' }}>实体ID</th>
|
||
<th style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)' }}>操作</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{entities.map((e) => (
|
||
<tr key={e.entityId} data-testid="modeler-entity-row">
|
||
<td style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)' }} data-testid="modeler-entity-name">{e.tablename}</td>
|
||
<td style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)' }}>{e.name}</td>
|
||
<td style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)', color: 'var(--text-3)' }}>{e.entityId}</td>
|
||
<td style={{ padding: '8px 14px', borderBottom: '1px solid var(--line)' }}>
|
||
<button className="btn btn-sm" data-testid="modeler-add-field" onClick={() => openAlter(e.tablename)}>加字段</button>
|
||
</td>
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
)}
|
||
|
||
{open ? (
|
||
<Dialog
|
||
title="新建实体"
|
||
width={720}
|
||
onClose={() => setOpen(false)}
|
||
foot={
|
||
<>
|
||
<button className="btn" onClick={() => setOpen(false)}>取消</button>
|
||
<button className="btn btn-primary" data-testid="modeler-submit" disabled={!canSubmit} onClick={submit}>
|
||
{submitting ? '建表中…' : '建表'}
|
||
</button>
|
||
</>
|
||
}
|
||
>
|
||
<div style={{ display: 'grid', gap: 12 }}>
|
||
<label style={{ display: 'grid', gap: 4 }}>
|
||
<span className="sub-text">表名(大写字母开头,仅大写字母/数字/下划线,3–41 位)</span>
|
||
<input
|
||
data-testid="modeler-tablename"
|
||
value={tablename}
|
||
onChange={(ev) => setTablename(ev.target.value.toUpperCase())}
|
||
placeholder="OA_DEMO"
|
||
/>
|
||
{tablename && !tableValid ? (
|
||
<span style={{ color: 'var(--err)', fontSize: 11 }}>表名格式不合法</span>
|
||
) : null}
|
||
</label>
|
||
<label style={{ display: 'grid', gap: 4 }}>
|
||
<span className="sub-text">显示名(可选)</span>
|
||
<input value={name} onChange={(ev) => setName(ev.target.value)} placeholder="演示维护" />
|
||
</label>
|
||
|
||
<FieldTable rows={rows} onChange={setRows} />
|
||
|
||
{submitErr ? <div style={{ color: 'var(--err)', fontSize: 12 }}>{submitErr}</div> : null}
|
||
</div>
|
||
</Dialog>
|
||
) : null}
|
||
|
||
{alterTarget ? (
|
||
<Dialog
|
||
title={`加字段 · ${alterTarget}`}
|
||
width={720}
|
||
onClose={() => setAlterTarget(null)}
|
||
foot={
|
||
<>
|
||
<button className="btn" onClick={() => setAlterTarget(null)}>取消</button>
|
||
<button className="btn btn-primary" data-testid="modeler-alter-submit" disabled={!canAlter} onClick={submitAlter}>
|
||
{alterSubmitting ? '加字段中…' : '加字段'}
|
||
</button>
|
||
</>
|
||
}
|
||
>
|
||
<div style={{ display: 'grid', gap: 12 }}>
|
||
<div className="sub-text">
|
||
给已有实体 <b>{alterTarget}</b> 加列。受控 ALTER:只加列、恒可空(对有数据的表安全),不改/删既有列。
|
||
</div>
|
||
<FieldTable rows={alterRows} onChange={setAlterRows} />
|
||
{alterErr ? <div style={{ color: 'var(--err)', fontSize: 12 }}>{alterErr}</div> : null}
|
||
</div>
|
||
</Dialog>
|
||
) : null}
|
||
</div>
|
||
);
|
||
}
|