ui.tsx
22.2 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
import {
AlertTriangle, Check, CheckCircle2, ChevronRight, Copy, Info, Loader2, RotateCcw, X, XCircle,
} from "lucide-react";
import React, { useEffect, useRef, useState, type ReactNode } from "react";
import { createPortal } from "react-dom";
import { cn } from "@/lib/utils";
import { useAppStore } from "@/store/useAppStore";
export function Card({ className, children, ...rest }: React.HTMLAttributes<HTMLDivElement>) {
return <div className={cn("card", className)} {...rest}>{children}</div>;
}
export function CardHeader({ title, desc, right, className }: {
title: ReactNode; desc?: ReactNode; right?: ReactNode; className?: string;
}) {
return (
<div className={cn("flex items-start justify-between gap-3 border-b border-line px-5 py-3.5", className)}>
<div className="min-w-0">
<h3 className="truncate text-title font-semibold leading-snug">{title}</h3>
{desc && <p className="mt-0.5 text-caption text-muted">{desc}</p>}
</div>
{right && <div className="flex items-center gap-1.5 shrink-0">{right}</div>}
</div>
);
}
/**
* 状态色收口:以前 tone 是一串裸 tailwind class,同一个状态在画布是 9.5px 灰块、
* 在列表是 Badge、在编辑器是一整句话。现在页面只给 variant,配色只有这一处定义。
*/
export const BADGE_VARIANT = {
neutral: "bg-ink/5 text-muted",
draft: "bg-ink/5 text-muted",
published: "bg-ok/10 text-ok",
ok: "bg-ok/10 text-ok",
info: "bg-brand/10 text-brand",
warn: "bg-warn/10 text-warn",
danger: "bg-bad/10 text-bad",
deprecated: "bg-bad/10 text-bad",
archived: "bg-ink/10 text-muted",
} as const;
export type BadgeVariant = keyof typeof BADGE_VARIANT;
export function Badge({ variant, tone, children, className, title }: {
variant?: BadgeVariant; tone?: string; children: ReactNode; className?: string; title?: string;
}) {
return (
<span title={title}
className={cn("inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-caption font-medium",
tone || BADGE_VARIANT[variant || "neutral"], className)}>{children}</span>
);
}
export function Pill({ color, children, onClick, title }: {
color?: string; children: ReactNode; onClick?: () => void; title?: string;
}) {
return (
<span
title={title}
onClick={onClick}
className={cn("inline-flex items-center gap-1.5 rounded-md px-2 py-0.5 text-label font-medium",
onClick && "cursor-pointer hover:opacity-80")}
style={color
? { background: `${color}1f`, color, boxShadow: `inset 0 0 0 1px ${color}33` }
: undefined}
>
{color && <i className="h-1.5 w-1.5 rounded-full" style={{ background: color }} />}
{children}
</span>
);
}
export function Spinner({ className }: { className?: string }) {
return <Loader2 className={cn("h-4 w-4 animate-spin", className)} />;
}
export function Progress({ value, className }: { value: number; className?: string }) {
return (
<div className={cn("h-1.5 w-full overflow-hidden rounded-full bg-ink/8", className)}>
<div
className="h-full rounded-full transition-[width] duration-500 ease-out"
style={{ width: `${Math.min(100, Math.max(0, value))}%`,
background: "linear-gradient(90deg, rgb(var(--brand)), rgb(var(--brand2)))" }}
/>
</div>
);
}
export function Empty({ icon, title, desc, action }: {
icon?: ReactNode; title: string; desc?: ReactNode; action?: ReactNode;
}) {
return (
<div className="flex flex-col items-center justify-center gap-2 px-6 py-12 text-center">
{icon && <div className="mb-1 text-muted/50">{icon}</div>}
<p className="text-title font-medium">{title}</p>
{desc && <p className="max-w-sm text-label leading-relaxed text-muted">{desc}</p>}
{action && <div className="mt-3">{action}</div>}
</div>
);
}
export function Skeleton({ className }: { className?: string }) {
return <div className={cn("skeleton", className)} />;
}
export function ErrorNote({ children, onRetry }: { children: ReactNode; onRetry?: () => void }) {
return (
<div className="flex items-start gap-2 rounded-lg border border-bad/30 bg-bad/5 px-3 py-2 text-body">
<XCircle className="mt-0.5 h-4 w-4 shrink-0 text-bad" />
<div className="min-w-0 flex-1 break-words">{children}</div>
{onRetry && <button className="btn btn-sm shrink-0" onClick={onRetry}>重试</button>}
</div>
);
}
/**
* 弹窗。**内容区自带内边距,动作按钮走 `footer`**。
*
* 以前 body 是裸的 overflow-auto,每个表单自己补 padding —— 结果就是漏补的那几个
* 输入框直接贴着弹窗边框,保存按钮浮在滚动区里,长表单一滚就找不着。
* 边距和底栏在这里统一兜住,调用方不用再想这件事。
*/
export function Modal({ open, onClose, title, desc, children, footer, width = "max-w-lg", bodyClass }: {
open: boolean; onClose: () => void; title: ReactNode; desc?: ReactNode;
children: ReactNode; footer?: ReactNode; width?: string; bodyClass?: string;
}) {
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!open) return;
const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); };
document.addEventListener("keydown", onKey);
return () => document.removeEventListener("keydown", onKey);
}, [open, onClose]);
if (!open) return null;
return (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4"
onMouseDown={(e) => { if (e.target === e.currentTarget) onClose(); }}>
<div className="absolute inset-0 bg-black/40 backdrop-blur-[2px]" />
<div ref={ref}
className={cn("card relative z-10 flex max-h-[88vh] w-full animate-fade-up flex-col overflow-hidden",
width)}>
<CardHeader className="shrink-0" title={title} desc={desc} right={
<button className="btn btn-ghost btn-sm" onClick={onClose} aria-label="关闭">
<X className="h-4 w-4" />
</button>} />
<div className={cn("min-h-0 flex-1 overflow-auto px-4 py-4", bodyClass)}>{children}</div>
{footer && (
<div className="flex shrink-0 flex-wrap items-center justify-end gap-2 border-t border-line
bg-elevated px-4 py-3">
{footer}
</div>
)}
</div>
</div>
);
}
export function Confirm({ open, title, body, danger, confirmText = "确定", onConfirm, onClose }: {
open: boolean; title: string; body: ReactNode; danger?: boolean;
confirmText?: string; onConfirm: () => void; onClose: () => void;
}) {
return (
<Modal open={open} onClose={onClose} title={title} width="max-w-md"
footer={
<>
<button className="btn" onClick={onClose}>取消</button>
<button className={cn("btn", danger ? "btn-danger" : "btn-primary")}
onClick={() => { onConfirm(); onClose(); }}>{confirmText}</button>
</>
}>
<div className="text-body leading-relaxed">{body}</div>
</Modal>
);
}
/**
* 折叠区。问答页一屏的信息量太大 —— SQL、轨迹、解释全摊开是一片噪声,
* 全藏起来又变回黑盒。所以折叠条本身要带足够摘要:不展开也知道里面是什么、
* 值不值得展开。
*/
export function Collapse({ open, onToggle, icon, title, summary, right, tone, children }: {
open: boolean; onToggle: () => void; icon?: ReactNode; title: ReactNode;
summary?: ReactNode; right?: ReactNode; tone?: string; children: ReactNode;
}) {
return (
<div className={cn("overflow-hidden rounded-lg border border-line", tone)}>
<div className="flex items-center gap-2 px-2.5 py-1.5">
<button className="flex min-w-0 flex-1 items-center gap-2 text-left" onClick={onToggle}>
<ChevronRight className={cn("h-3.5 w-3.5 shrink-0 text-muted transition-transform",
open && "rotate-90")} />
{icon && <span className="shrink-0 text-muted">{icon}</span>}
<span className="shrink-0 text-label font-medium">{title}</span>
{summary && (
<span className="min-w-0 flex-1 truncate text-label text-muted">{summary}</span>
)}
</button>
{right && <div className="flex shrink-0 items-center gap-1">{right}</div>}
</div>
{open && <div className="border-t border-line p-2.5">{children}</div>}
</div>
);
}
/** 分段切换。比一排按钮更像「同一组里选一个」,也省掉选中态靠主色按钮硬撑。 */
export function Segmented<T extends string>({ value, options, onChange, className }: {
value: T; options: { key: T; label: ReactNode; badge?: ReactNode }[];
onChange: (v: T) => void; className?: string;
}) {
return (
<div className={cn("inline-flex items-center gap-0.5 rounded-lg border border-line bg-elevated p-0.5",
className)}>
{options.map((o) => (
<button key={o.key} onClick={() => onChange(o.key)}
className={cn("inline-flex items-center gap-1.5 whitespace-nowrap rounded-[6px] px-2.5 py-1",
"text-label font-medium transition-colors",
value === o.key ? "bg-surface text-ink shadow-sm" : "text-muted hover:text-ink")}>
{o.label}
{o.badge}
</button>
))}
</div>
);
}
/** 表单字段。label / 说明 / 控件的间距定死在一处,免得每个表单各写一套。 */
export function Field({ label, hint, required, children, className }: {
label: ReactNode; hint?: ReactNode; required?: boolean;
children: ReactNode; className?: string;
}) {
return (
<div className={cn("min-w-0", className)}>
<label className="label">
{label}
{required && <span className="ml-0.5 text-bad">*</span>}
</label>
{children}
{hint && <p className="hint">{hint}</p>}
</div>
);
}
/** 复制按钮。到处都要,统一带个「已复制」反馈。 */
export function CopyButton({ text, label = "复制", className }: {
text: string; label?: string; className?: string;
}) {
const [done, setDone] = useState(false);
return (
<button className={cn("btn btn-ghost btn-sm", className)} title={label}
onClick={async () => {
try {
await navigator.clipboard.writeText(text);
setDone(true);
window.setTimeout(() => setDone(false), 1400);
} catch { /* 没有剪贴板权限就静默失败,不值得为它弹个错 */ }
}}>
{done ? <Check className="h-3.5 w-3.5 text-ok" /> : <Copy className="h-3.5 w-3.5" />}
{label && <span>{done ? "已复制" : label}</span>}
</button>
);
}
const TOAST_ICON = {
info: <Info className="h-4 w-4 text-brand2" />,
ok: <CheckCircle2 className="h-4 w-4 text-ok" />,
warn: <AlertTriangle className="h-4 w-4 text-warn" />,
error: <XCircle className="h-4 w-4 text-bad" />,
};
export function Toaster() {
const { toasts, dismiss } = useAppStore();
return (
<div className="pointer-events-none fixed bottom-4 right-4 z-[60] flex w-[min(92vw,26rem)] flex-col gap-2">
{toasts.map((t) => (
<div key={t.id}
className="card pointer-events-auto flex animate-fade-up items-start gap-2.5 p-3">
<span className="mt-0.5 shrink-0">{TOAST_ICON[t.kind]}</span>
<div className="min-w-0 flex-1">
<p className="text-body font-medium leading-snug break-words">{t.text}</p>
{t.detail && <p className="mt-0.5 text-xs text-muted break-words">{t.detail}</p>}
</div>
<button className="btn btn-ghost btn-sm shrink-0 -mr-1 -mt-1"
onClick={() => dismiss(t.id)} aria-label="关闭提示">
<X className="h-3.5 w-3.5" />
</button>
</div>
))}
</div>
);
}
/** 小尺寸横向统计块,概览与工具条复用 */
export function Stat({ label, value, hint, tone }: {
label: string; value: ReactNode; hint?: string; tone?: string;
}) {
return (
<div className="min-w-0">
<p className="text-caption uppercase tracking-wide text-muted">{label}</p>
<p className={cn("mt-0.5 text-page font-semibold leading-tight tabular-nums", tone)}>{value}</p>
{hint && <p className="mt-0.5 text-caption text-muted truncate">{hint}</p>}
</div>
);
}
/**
* 页面级错误边界:一页渲染出错只影响这一页,侧栏和其他页面照常用;换页或点「重试」就恢复。
* 以前只有包在整个应用外面的一层,任何一页出错整个界面都没了,只能刷新。
*/
export class PageErrorBoundary extends React.Component<
{ resetKey: string; children: ReactNode; onHome?: () => void },
{ error: Error | null }
> {
state = { error: null as Error | null };
static getDerivedStateFromError(error: Error) { return { error }; }
componentDidCatch(error: Error, info: React.ErrorInfo) {
console.error("页面渲染出错", error, info.componentStack);
}
componentDidUpdate(prev: { resetKey: string }) {
if (prev.resetKey !== this.props.resetKey && this.state.error) this.setState({ error: null });
}
render() {
const { error } = this.state;
if (!error) return this.props.children;
const stack = (error.stack || "").split("\n").slice(1, 6).join("\n");
const detail = `${error.name}: ${error.message}\n${stack}`;
return (
<div className="flex h-full items-start justify-center p-6">
<div className="card mt-10 w-full max-w-lg p-5">
<p className="flex items-center gap-2 text-title font-semibold"><AlertTriangle className="h-4 w-4 text-warn" />这一页没能显示出来</p>
<p className="mt-1 text-body leading-relaxed text-muted">
其他页面不受影响,可以从左侧继续使用。点「重试」重新加载这一页;如果反复出现,把下面的信息发给实施人员。
</p>
<pre className="mt-3 max-h-40 overflow-auto rounded-lg bg-ink/5 p-2.5 font-mono text-caption">{detail}</pre>
<div className="mt-3 flex flex-wrap gap-2">
<button className="btn btn-primary" onClick={() => this.setState({ error: null })}><RotateCcw className="h-3.5 w-3.5" />重试</button>
{this.props.onHome && <button className="btn" onClick={() => { this.setState({ error: null }); this.props.onHome?.(); }}>回到工作台</button>}
<CopyButton text={detail} label="复制错误信息" />
</div>
</div>
</div>
);
}
}
/** 术语表:搭建页面里绕不开的专业词,悬停给一句大白话。 */
export const GLOSSARY: Record<string, string> = {
本体: "把业务里的「东西」(门店、配件)、「关系」(供货)、「算法」(指标、规则)和「能做的事」(补货、标记)描述清楚的一套模型,系统按它理解问题、取数和执行",
元素: "本体由四种元素搭成:对象、关系、函数(指标与规则)、动作",
四元组: "对象、关系、函数(指标与规则)、动作这四种元素",
对象: "业务里能一个个指认出来的东西,比如门店、配件、采购单。每个对象有若干属性,通常对应一张数据表",
关系: "两类对象之间怎么关联,比如「采购单属于供应商」。系统靠它从一个对象找到相关的另一个",
函数: "对对象做的计算或判断,分聚合(指标)、规则、决策表、周期等几类",
动作: "会改变业务数据的操作,比如「下发补货单」「标记缺件」。有参数、执行前的检查条件和执行效果,可以要求审批",
聚合: "算出一个数的指标,比如「缺件率」「库存周转天数」。接上取数表达式后,系统自动生成 SQL 去业务库里算",
规则: "一条判断或计算公式,比如「库存 + 在途 < 安全库存 就算缺货」。结果是 是 / 否,或者一个数",
决策表: "按几个条件查表得出结论,一行一条规则,比如「缺件超过 3 天且是事故车 → 优先级 高」",
周期: "统计用的时间窗口,比如自然月,或「上月 26 日到本月 25 日」这样的业务月",
模型推理: "调用已经登记的机器学习模型,按输入算出预测值,比如预测下个月的销量",
"AI 分析(参考)": "让 AI 读取数据后给出结论,只读不改,结果仅供参考",
基数: "两类对象各有几个对应:1:n 是一个 A 对应多个 B(一个供应商有多张采购单),n:1 反过来,1:1 一一对应,n:n 多对多",
绑定: "把元素接到真实数据上:对象对应哪张表、关系用哪两列关联、指标怎么取数",
取数表达式: "指标在数据库里怎么算的 SQL 片段,比如 SUM(amount)。AI 写出来后会在业务库里实际跑一次,跑通才采用",
前置条件: "动作执行前要满足的检查,比如「库存数量 > 0 才能出库」,不满足就不能执行",
效果: "动作执行后真正做的事:改某个字段、新建一条记录、发通知、派待办",
参数: "执行动作或流程卡时要人填的内容,比如补货数量、原因",
属性: "对象上的一个字段,比如配件的「库存数量」",
主键: "能唯一确定一条记录的属性,比如配件编号",
可分组: "能用来分组统计的属性,比如门店、品类、状态",
可写: "允许动作修改的属性",
可信度: "AI 对这条草案有多大把握。高的默认勾选;低的往往还没落到数据上(没绑表、没公式、动作没补全),建议看过再定",
深化: "AI 把文档里的文字规则进一步写成能直接计算的公式或决策表",
草案: "AI 起草、还没决定的元素。采纳后才会真正建出来并上线",
采纳: "确认这条草案没问题:建成元素并直接发布上线;体检没过的会留在草稿里并说明原因",
骨架: "对象的主键、名称和关系键。只同步这些,明细数据仍然实时查业务库",
口径: "一个指标到底怎么算:统计哪些数据、分子分母是什么、按什么周期",
流程卡: "可以运行的标准流程:取数、判断、派任务、发起动作,按顺序自动推进",
MCP: "给外部 AI 助手(企业微信机器人、Claude 等)调用本体能力的标准接口",
变更提案: "改动本体后提交给别人审批,批准后才正式发布生效",
};
/**
* 悬停说明气泡。挂在 body 上(fixed 定位),不会被滚动区、overflow:hidden 的卡片裁掉;
* 比原生 title 出得快、能换行。tip 为空时只渲染 children。
*/
export function Tip({ tip, children, className, style }: {
tip?: ReactNode; children: ReactNode; className?: string; style?: React.CSSProperties;
}) {
const ref = useRef<HTMLSpanElement>(null);
const [at, setAt] = useState<{ x: number; top: number; bottom: number } | null>(null);
const show = () => {
const r = ref.current?.getBoundingClientRect();
if (r) setAt({ x: r.left + r.width / 2, top: r.top, bottom: r.bottom });
};
const hide = () => setAt(null);
if (!tip) return <span className={className} style={style}>{children}</span>;
const below = at ? at.top < 90 : false;
const x = at ? Math.max(170, Math.min(window.innerWidth - 170, at.x)) : 0;
return (
<span ref={ref} className={className} style={style} onMouseEnter={show} onMouseLeave={hide} onFocus={show} onBlur={hide}>
{children}
{at && createPortal(
<div role="tooltip"
className="pointer-events-none fixed z-[200] w-max max-w-[320px] rounded-lg bg-ink px-2.5 py-1.5 text-caption font-normal leading-relaxed text-surface shadow-lg"
style={{ left: x, top: below ? at.bottom + 6 : at.top - 6, transform: `translate(-50%, ${below ? "0" : "-100%"})` }}>
{tip}
</div>, document.body)}
</span>
);
}
/** 术语:带虚下划线,悬停看大白话解释(取自 GLOSSARY,或直接给 tip)。 */
export function Term({ children, tip }: { children: string; tip?: string }) {
const text = tip || GLOSSARY[children];
if (!text) return <>{children}</>;
return <Tip tip={text} className="cursor-help border-b border-dotted border-muted/60">{children}</Tip>;
}
/**
* 页面骨架:标题区(标题 + 一句话 + 主操作)/ 筛选条 / 内容区 / 右侧抽屉。
*
* 以前每页各写各的 —— 有的有渐变 hero 卡有的没有,抽屉一个 52vw 一个 58vw,
* 同一个编辑器在两页里排版还不一样。统一骨架之后,页面之间的节奏才是一致的。
*/
export function PageShell({ title, desc, actions, toolbar, aside, asideWidth = 720, children }: {
title?: ReactNode; desc?: ReactNode; actions?: ReactNode; toolbar?: ReactNode;
aside?: ReactNode; asideWidth?: number; children: ReactNode;
}) {
return (
<div className="flex h-full min-h-0 gap-3">
<div className="flex min-w-0 flex-1 flex-col gap-3">
{(title || actions) && (
<div className="flex flex-wrap items-start gap-3">
<div className="min-w-0 flex-1">
{title && <h2 className="truncate text-section font-semibold">{title}</h2>}
</div>
{actions && <div className="flex shrink-0 flex-wrap items-center gap-2">{actions}</div>}
</div>
)}
{toolbar && <div className="flex flex-wrap items-center gap-2">{toolbar}</div>}
<div className="min-h-0 flex-1">{children}</div>
</div>
{aside && (
<div className="shrink-0 overflow-hidden rounded-xl border border-line bg-surface shadow-[var(--shadow)]
max-lg:fixed max-lg:inset-y-2 max-lg:right-2 max-lg:z-40 max-lg:w-[min(760px,96vw)]"
style={{ width: `min(${asideWidth}px, 46vw)` }}>
{aside}
</div>
)}
</div>
);
}
/**
* 列表三态:加载中 / 出错(可重试)/ 空。
* 以前 ErrorNote 组件写好了却一次都没用,搜索零结果时下方干脆什么都不渲染。
*/
export function ListState({ loading, error, empty, onRetry, emptyTitle = "没有数据",
emptyDesc, emptyAction, children }: {
loading?: boolean; error?: string; empty?: boolean; onRetry?: () => void;
emptyTitle?: string; emptyDesc?: ReactNode; emptyAction?: ReactNode; children?: ReactNode;
}) {
if (loading) return <div className="grid place-items-center p-10"><Spinner /></div>;
if (error) return <div className="p-4"><ErrorNote onRetry={onRetry}>{error}</ErrorNote></div>;
if (empty) return <Empty title={emptyTitle} desc={emptyDesc} action={emptyAction} />;
return <>{children}</>;
}