ui.tsx
18 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
import {
AlertTriangle, Check, CheckCircle2, ChevronRight, Copy, Info, Loader2, RotateCcw, X, XCircle,
} from "lucide-react";
import React, { useEffect, useRef, useState, type ReactNode } from "react";
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 px-4 py-3 border-b border-line", className)}>
<div className="min-w-0">
<h3 className="text-body font-semibold leading-tight truncate">{title}</h3>
{desc && <p className="text-xs text-muted mt-0.5">{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-xs text-muted leading-relaxed">{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="mt-1 text-label leading-relaxed text-muted">{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> = {
本体: "把业务里的「东西」(门店、商品)、「关系」(供货)、「算法」(指标、规则)和「能做的事」(补货、标记)描述清楚的一套模型,系统按它理解问题、取数和执行",
积木: "本体由四种积木搭成:对象、关系、指标与规则(函数)、动作",
四元组: "对象、关系、函数(指标与规则)、动作这四种积木",
骨架: "对象的主键、名称和关系键。只同步这些,明细数据仍然实时查业务库",
口径: "一个指标到底怎么算:统计哪些数据、分子分母是什么、按什么周期",
流程卡: "可以运行的标准流程:取数、判断、派任务、发起动作,按顺序自动推进",
MCP: "给外部 AI 助手(企业微信机器人、Claude 等)调用本体能力的标准接口",
变更提案: "改动本体后提交给别人审批,批准后才正式发布生效",
};
export function Term({ children, tip }: { children: string; tip?: string }) {
const text = tip || GLOSSARY[children];
if (!text) return <>{children}</>;
return (
<span className="cursor-help border-b border-dotted border-muted/60" title={text}>{children}</span>
);
}
/**
* 页面骨架:标题区(标题 + 一句话 + 主操作)/ 筛选条 / 内容区 / 右侧抽屉。
*
* 以前每页各写各的 —— 有的有渐变 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>}
{desc && <p className="mt-0.5 text-label text-muted">{desc}</p>}
</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}</>;
}