@tbox.cn/app-agent-ui-kit 1.0.0-alpha.16 → 1.0.0-alpha.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +3 -3
- package/src/ChatPage.tsx +204 -27
- package/src/card/confirm-fields.test.tsx +106 -0
- package/src/card/primitives.tsx +270 -59
- package/src/card/use-card-actions.ts +5 -1
- package/src/card/view-card.tsx +113 -22
- package/src/chat-config.ts +2 -0
- package/src/components/ActionBar/DebugButton.tsx +12 -4
- package/src/components/ActionBar/index.tsx +61 -27
- package/src/components/BottomSheet/index.test.tsx +11 -0
- package/src/components/BottomSheet/index.tsx +34 -3
- package/src/components/QuickCommands/index.tsx +60 -14
- package/src/components/ToolCallBlock/detail.test.ts +2 -0
- package/src/components/ToolCallBlock/detail.ts +9 -2
- package/src/contract.test.tsx +176 -0
- package/src/hooks/use-stick-to-bottom.test.tsx +101 -0
- package/src/hooks/use-stick-to-bottom.ts +35 -7
- package/src/index.ts +2 -0
- package/src/overlay/Overlay.tsx +20 -2
- package/src/preview-provider.tsx +8 -0
- package/src/shell.contract.json +1 -1
- package/src/shell.test.ts +105 -2
- package/src/theme/base.css +42 -2
package/README.md
CHANGED
|
@@ -48,7 +48,7 @@ import { BottomSheet } from '@tbox.cn/app-agent-ui-kit/bottom-sheet';
|
|
|
48
48
|
</BottomSheet>
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
支持 `className` 定制内容区,默认最大高度为视口的 65
|
|
51
|
+
支持 `className` 定制内容区,默认最大高度为视口的 65%;`size="large"` 固定为视口的 86%,用于活动详情等长内容,支持动态视口高度。长内容在弹层内部滚动。`preloadBottomSheet()` 保留为已废弃的空操作(弹层不再有第三方运行时需要预热)。
|
|
52
52
|
|
|
53
53
|
### 大图预览
|
|
54
54
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbox.cn/app-agent-ui-kit",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.18",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Agent 对话壳 UI 组件包(源码发布,单一模板统一化 W2-1):ChatPage/壳组件族/主题 tokens。",
|
|
6
6
|
"exports": {
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"remark-gfm": "4.0.1",
|
|
45
45
|
"yet-another-react-lightbox": "3.33.1",
|
|
46
46
|
"zustand": "4.5.7",
|
|
47
|
-
"@tbox.cn/app-
|
|
48
|
-
"@tbox.cn/app-
|
|
47
|
+
"@tbox.cn/app-contracts": "1.0.0-alpha.18",
|
|
48
|
+
"@tbox.cn/app-agent-sdk-client": "1.0.0-alpha.18"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"react": "18.3.1",
|
package/src/ChatPage.tsx
CHANGED
|
@@ -6,12 +6,13 @@
|
|
|
6
6
|
* 空态恒挂载——域自持其显隐);缺席 → chat 流(空态为空体,landing 槽已退役);
|
|
7
7
|
* - 域自持段(首屏数据/门禁/详情弹层/活动预热)经槽渲染函数自带——本页零域语义。
|
|
8
8
|
*/
|
|
9
|
-
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
9
|
+
import { Fragment, useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
10
10
|
import { consumeAutoSendQuery, runAutoSend } from '@tbox.cn/app-agent-sdk-client';
|
|
11
11
|
import { usePullDownHistory } from './hooks/use-pull-down-history';
|
|
12
12
|
import { registerScrollHost } from './overlay';
|
|
13
13
|
import type { TboxCardPayload } from '@tbox.cn/app-contracts';
|
|
14
14
|
import { CardRenderer } from './components/CardRenderer';
|
|
15
|
+
import { BottomSheet } from './components/BottomSheet';
|
|
15
16
|
import { ChatInput } from './components/ChatInput';
|
|
16
17
|
import HistoryEntry from './components/HistoryEntry';
|
|
17
18
|
import { HistoryList } from './components/HistoryList';
|
|
@@ -46,6 +47,33 @@ export interface ChatPageProps {
|
|
|
46
47
|
filterInlineCards?: (cards: readonly TboxCardPayload[]) => TboxCardPayload[];
|
|
47
48
|
}
|
|
48
49
|
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 欢迎区表面 → 类名(纯函数,供 ChatPage 与契约单测共用)。
|
|
53
|
+
*
|
|
54
|
+
* 为什么颜色必须在这里给:`@layer base` 的选择器压不过 `utilities` 层
|
|
55
|
+
* (层序优先于特异性),把 `color` 写在 base.css 里会被 `text-foreground` 无条件覆盖。
|
|
56
|
+
*/
|
|
57
|
+
export function welcomeSurfaceClasses(surface: 'default' | 'brand' | 'subtle' = 'default'): {
|
|
58
|
+
container: string;
|
|
59
|
+
title: string;
|
|
60
|
+
subtitle: string;
|
|
61
|
+
} {
|
|
62
|
+
return {
|
|
63
|
+
container:
|
|
64
|
+
surface === 'brand'
|
|
65
|
+
? 'chat-page__welcome--brand'
|
|
66
|
+
: surface === 'subtle'
|
|
67
|
+
? 'chat-page__welcome--subtle bg-card border border-border shadow-sm'
|
|
68
|
+
: '',
|
|
69
|
+
title: surface === 'brand' ? 'text-primary-foreground' : 'text-foreground',
|
|
70
|
+
subtitle: surface === 'brand' ? 'text-primary-foreground' : 'text-muted-foreground',
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 旧 store / 预览 store 没有锚点表时的稳定空表(避免每次渲染新建对象触发 memo 失效)。 */
|
|
75
|
+
const EMPTY_ANCHORS: Record<string, { afterMessageId: string | null; seq: number }> = {};
|
|
76
|
+
|
|
49
77
|
export function ChatPage({ store, cards, rawCards, slots, filterInlineCards }: ChatPageProps) {
|
|
50
78
|
return (
|
|
51
79
|
<KitChatProvider
|
|
@@ -70,6 +98,11 @@ function ChatPageInner() {
|
|
|
70
98
|
const runtimeStatus = useChatStore((s) => s.runtimeStatus);
|
|
71
99
|
const authBlocked = useChatStore((s) => s.authBlocked);
|
|
72
100
|
const orphanCards = useChatStore((s) => s.orphanCards);
|
|
101
|
+
// 防御读取:旧版 SDK store 与预览 store 可能没有这些字段(跨版本混跑承诺)。
|
|
102
|
+
const cardAnchors = useChatStore((s) => s.cardAnchors) ?? EMPTY_ANCHORS;
|
|
103
|
+
const activeSheetCardId = useChatStore((s) => s.activeSheetCardId) ?? null;
|
|
104
|
+
const openSheetCard = useChatStore((s) => s.openSheetCard);
|
|
105
|
+
const closeSheetCard = useChatStore((s) => s.closeSheetCard);
|
|
73
106
|
const initConversation = useChatStore((s) => s.initConversation);
|
|
74
107
|
const destroy = useChatStore((s) => s.destroy);
|
|
75
108
|
const sendMessage = useChatStore((s) => s.sendMessage);
|
|
@@ -126,7 +159,57 @@ function ChatPageInner() {
|
|
|
126
159
|
const historyLoadInFlightRef = useRef<Promise<void> | null>(null);
|
|
127
160
|
const isAtTopRef = useRef(true);
|
|
128
161
|
|
|
129
|
-
const
|
|
162
|
+
const filterInlineCards = useInlineCardFilter();
|
|
163
|
+
// 门禁卡先被域过滤掉(域浮层自持),剩下的按呈递落点分三桶:
|
|
164
|
+
// top → 消息区之前(业务旁路欢迎卡;无 presentation 的旧卡也落这里,行为不变)
|
|
165
|
+
// timeline → 对话流内、按客户端锚点插在对应消息之后(动作结果卡的默认落点)
|
|
166
|
+
// sheet → 半屏抽屉(详情/大列表),不进内联流,避免“卡里套卡 + 双标题”
|
|
167
|
+
const visibleOrphans = useMemo(
|
|
168
|
+
() => filterInlineCards(orphanCards),
|
|
169
|
+
[orphanCards, filterInlineCards],
|
|
170
|
+
);
|
|
171
|
+
const topCards = useMemo(
|
|
172
|
+
() => visibleOrphans.filter((card) => {
|
|
173
|
+
const placement = card.presentation?.placement;
|
|
174
|
+
return placement === undefined || placement === 'top';
|
|
175
|
+
}),
|
|
176
|
+
[visibleOrphans],
|
|
177
|
+
);
|
|
178
|
+
const timelineCards = useMemo(
|
|
179
|
+
() => visibleOrphans
|
|
180
|
+
.filter((card) => card.presentation?.placement === 'timeline')
|
|
181
|
+
.sort((a, b) => (cardAnchors[a.id]?.seq ?? 0) - (cardAnchors[b.id]?.seq ?? 0)),
|
|
182
|
+
[visibleOrphans, cardAnchors],
|
|
183
|
+
);
|
|
184
|
+
// 是否已有会话内容(对话态 vs 空会话);home 槽缺席时始终为对话形态。
|
|
185
|
+
// 先于 useStickToBottom 计算:空会话(home 槽首页或纯对话欢迎态)关闭贴底,
|
|
186
|
+
// 避免长首页/长欢迎网格高度超过视口时把首屏拽到底部。
|
|
187
|
+
const hasLiveContent = historyMessages.length > 0
|
|
188
|
+
|| messages.length > 0
|
|
189
|
+
|| topCards.length > 0
|
|
190
|
+
|| timelineCards.length > 0;
|
|
191
|
+
const showHome = !!homeSlot && !hasLiveContent;
|
|
192
|
+
// 空会话欢迎区:没有 home 槽时给首屏一个落点(标题来自 chat.json welcome,缺省「开始对话」)
|
|
193
|
+
const showWelcome = !homeSlot && !hasLiveContent;
|
|
194
|
+
|
|
195
|
+
const stick = useStickToBottom({ scrollRef: bodyRef, contentRef, enabled: hasLiveContent });
|
|
196
|
+
|
|
197
|
+
// 对话清空回空会话:外部 clearChat/会话升级等非按钮路径也可能从长对话退回首页/欢迎区;
|
|
198
|
+
// 不主动复位会停在旧对话的滚动位置,把首屏直接展示在中下部。
|
|
199
|
+
const prevHasLiveContentRef = useRef(hasLiveContent);
|
|
200
|
+
const resumeRef = useRef(stick.resume);
|
|
201
|
+
resumeRef.current = stick.resume;
|
|
202
|
+
useEffect(() => {
|
|
203
|
+
const wasLive = prevHasLiveContentRef.current;
|
|
204
|
+
prevHasLiveContentRef.current = hasLiveContent;
|
|
205
|
+
if (!wasLive || hasLiveContent) return;
|
|
206
|
+
const el = bodyRef.current;
|
|
207
|
+
if (el) el.scrollTop = 0;
|
|
208
|
+
isAtTopRef.current = true;
|
|
209
|
+
// 外部 clearChat / 会话升级等非按钮路径回空会话时同步恢复跟随态,
|
|
210
|
+
// 避免后续由 runAutoSend 或服务端首推消息进入新对话时不自动贴底。
|
|
211
|
+
resumeRef.current();
|
|
212
|
+
}, [hasLiveContent]);
|
|
130
213
|
|
|
131
214
|
const startNewConversation = useCallback(() => {
|
|
132
215
|
void newConversation();
|
|
@@ -181,29 +264,76 @@ function ChatPageInner() {
|
|
|
181
264
|
return task;
|
|
182
265
|
}, [loadMoreHistory, stick]);
|
|
183
266
|
|
|
184
|
-
const
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
[orphanCards, filterInlineCards],
|
|
267
|
+
const sheetCards = useMemo(
|
|
268
|
+
() => visibleOrphans.filter((card) => card.presentation?.placement === 'sheet'),
|
|
269
|
+
[visibleOrphans],
|
|
188
270
|
);
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
271
|
+
const sheetCard = useMemo(
|
|
272
|
+
() => sheetCards.find((card) => card.id === activeSheetCardId),
|
|
273
|
+
[sheetCards, activeSheetCardId],
|
|
274
|
+
);
|
|
275
|
+
// 抽屉收起后仍留在 store 的卡:给一个轻量重开入口,否则「关闭即消失」且无法再看。
|
|
276
|
+
// 位置跟随锚点(插在触发它的那条消息之后),不能一律堆在流首——聊了 5 轮后关抽屉,
|
|
277
|
+
// 入口跑到视口上方 1500px 就等于没有。
|
|
278
|
+
const collapsedSheetCards = useMemo(
|
|
279
|
+
() => sheetCards.filter((card) => card.id !== activeSheetCardId),
|
|
280
|
+
[sheetCards, activeSheetCardId],
|
|
281
|
+
);
|
|
282
|
+
// 时间线插槽:afterMessageId → 该消息之后要渲染的结果卡(按接收序)。
|
|
283
|
+
const timelineByMessage = useMemo(() => {
|
|
284
|
+
const map = new Map<string, TboxCardPayload[]>();
|
|
285
|
+
const tail: TboxCardPayload[] = [];
|
|
286
|
+
for (const card of [...timelineCards, ...collapsedSheetCards]) {
|
|
287
|
+
const after = cardAnchors[card.id]?.afterMessageId;
|
|
288
|
+
if (!after) {
|
|
289
|
+
tail.push(card);
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
const list = map.get(after) ?? [];
|
|
293
|
+
list.push(card);
|
|
294
|
+
map.set(after, list);
|
|
295
|
+
}
|
|
296
|
+
return { map, tail };
|
|
297
|
+
}, [timelineCards, collapsedSheetCards, cardAnchors]);
|
|
298
|
+
// 长卡(超过视口 75%)落位保护:贴底跟随会把卡片标题与首屏信息推出视口,
|
|
299
|
+
// 这里改为把最新结果卡的**顶部**对齐到滚动区上沿(留 12px 呼吸距)并暂停跟随,
|
|
300
|
+
// 由「回到底部」浮标引导用户继续看下方内容。
|
|
301
|
+
const lastTimelineId = timelineCards.length > 0 ? timelineCards[timelineCards.length - 1]!.id : '';
|
|
302
|
+
/** 已经做过顶对齐的卡 id:同一张卡只处理一次,否则 follow 状态每次翻转都会把用户拽回卡顶。 */
|
|
303
|
+
const alignedCardRef = useRef('');
|
|
304
|
+
/** stick 对象身份随 showJump 变化(useMemo 依赖),用 ref 拿最新 hold 避免进 effect 依赖。 */
|
|
305
|
+
const holdRef = useRef(stick.hold);
|
|
306
|
+
holdRef.current = stick.hold;
|
|
307
|
+
useEffect(() => {
|
|
308
|
+
if (!lastTimelineId || alignedCardRef.current === lastTimelineId) return;
|
|
309
|
+
const body = bodyRef.current;
|
|
310
|
+
const content = contentRef.current;
|
|
311
|
+
if (!body || !content) return;
|
|
312
|
+
// CSS.escape:卡实例 id 来自服务端,直接拼进属性选择器会被引号/括号打断(选择器抛错)。
|
|
313
|
+
const node = content.querySelector<HTMLElement>(`[data-timeline-card="${CSS.escape(lastTimelineId)}"]`);
|
|
314
|
+
if (!node) return;
|
|
315
|
+
const frame = requestAnimationFrame(() => {
|
|
316
|
+
if (node.getBoundingClientRect().height <= body.clientHeight * 0.75) {
|
|
317
|
+
alignedCardRef.current = lastTimelineId;
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
const offset = node.getBoundingClientRect().top - body.getBoundingClientRect().top;
|
|
321
|
+
body.scrollTop = Math.max(0, body.scrollTop + offset - 12);
|
|
322
|
+
alignedCardRef.current = lastTimelineId;
|
|
323
|
+
holdRef.current();
|
|
324
|
+
});
|
|
325
|
+
return () => cancelAnimationFrame(frame);
|
|
326
|
+
}, [lastTimelineId]);
|
|
195
327
|
|
|
196
328
|
const showHistoryEntry = historyHasMore && historyMessages.length === 0;
|
|
197
|
-
|
|
198
|
-
// 空会话欢迎区:没有 home 槽时给首屏一个落点(标题来自 chat.json welcome,缺省「开始对话」)
|
|
199
|
-
const showWelcome = !homeSlot
|
|
200
|
-
&& historyMessages.length === 0
|
|
201
|
-
&& messages.length === 0
|
|
202
|
-
&& inlineOrphanCards.length === 0;
|
|
203
329
|
// 欢迎区是身份的唯一位:身份写进 welcome.title / subtitle,壳不再有页头
|
|
204
330
|
const welcomeTitle = welcome?.title ?? '开始对话';
|
|
205
331
|
const welcomeSubtitle = welcome?.subtitle ?? '可以直接提问,也可以从下面的问题开始';
|
|
206
|
-
|
|
332
|
+
// 欢迎区表面:brand = 品牌深色渐变(迎宾感),subtle = 弱化描边;都只消费既有主题变量。
|
|
333
|
+
const welcomeSurfaceClass = welcomeSurfaceClasses(welcome?.surface ?? 'default');
|
|
334
|
+
// 仅无 home 槽的纯对话欢迎态在正文内用两列网格;
|
|
335
|
+
// 有 home 槽的首页(含 MallHome)与对话态共用输入框上方的快捷指令条,不沉到首页正文底部。
|
|
336
|
+
const showGridCommands = showWelcome;
|
|
207
337
|
|
|
208
338
|
const pull = usePullDownHistory({
|
|
209
339
|
isAtTopRef,
|
|
@@ -268,9 +398,9 @@ function ChatPageInner() {
|
|
|
268
398
|
|
|
269
399
|
{/* 空会话欢迎区:没有 home 槽时的首屏落点(文案来自 chat.json welcome) */}
|
|
270
400
|
{showWelcome ? (
|
|
271
|
-
<div className=
|
|
272
|
-
<h2 className=
|
|
273
|
-
{welcomeSubtitle ? <p className=
|
|
401
|
+
<div className={`chat-page__welcome flex flex-col items-center justify-center gap-2 px-page pb-4 pt-6 text-center ${welcomeSurfaceClass.container}`}>
|
|
402
|
+
<h2 className={`chat-page__welcome-title m-0 text-xl font-bold ${welcomeSurfaceClass.title}`}>{welcomeTitle}</h2>
|
|
403
|
+
{welcomeSubtitle ? <p className={`chat-page__welcome-subtitle m-0 text-sm leading-normal ${welcomeSurfaceClass.subtitle}`}>{welcomeSubtitle}</p> : null}
|
|
274
404
|
</div>
|
|
275
405
|
) : null}
|
|
276
406
|
|
|
@@ -286,16 +416,52 @@ function ChatPageInner() {
|
|
|
286
416
|
</div>
|
|
287
417
|
) : null}
|
|
288
418
|
|
|
289
|
-
{/* 业务旁路 emit
|
|
290
|
-
{
|
|
419
|
+
{/* 业务旁路 emit 的卡片(如欢迎卡):placement 缺省/top */}
|
|
420
|
+
{topCards.map((card) => (
|
|
291
421
|
<div key={card.id} className="chat-page__orphan-card mx-page">
|
|
292
422
|
<CardRenderer card={card} />
|
|
293
423
|
</div>
|
|
294
424
|
))}
|
|
295
425
|
|
|
296
|
-
{/* 消息(每条消息下的卡片由 MessageBubble
|
|
426
|
+
{/* 消息(每条消息下的卡片由 MessageBubble 内部渲染);
|
|
427
|
+
动作结果卡按客户端锚点插在对应消息之后——与服务端时钟无关,跨端偏差也不会乱序 */}
|
|
297
428
|
{messages.map((msg) => (
|
|
298
|
-
<
|
|
429
|
+
<Fragment key={msg.id}>
|
|
430
|
+
<MessageBubble message={msg} />
|
|
431
|
+
{(timelineByMessage.map.get(msg.id) ?? []).map((card) => (
|
|
432
|
+
<div key={card.id} className="chat-page__timeline-card mx-page" data-timeline-card={card.id}>
|
|
433
|
+
{card.presentation?.placement === 'sheet' ? (
|
|
434
|
+
// 抽屉已收起:留一个原地可点的重开入口(不把详情卡摊进流里)
|
|
435
|
+
<button
|
|
436
|
+
type="button"
|
|
437
|
+
className="tbox-sheet-reopen inline-flex min-h-11 items-center gap-1.5 rounded-full border border-border bg-card px-3.5 text-sm text-primary shadow-xs"
|
|
438
|
+
onClick={() => openSheetCard?.(card.id)}
|
|
439
|
+
>
|
|
440
|
+
{card.title ?? '查看详情'}
|
|
441
|
+
</button>
|
|
442
|
+
) : (
|
|
443
|
+
<CardRenderer card={card} />
|
|
444
|
+
)}
|
|
445
|
+
</div>
|
|
446
|
+
))}
|
|
447
|
+
</Fragment>
|
|
448
|
+
))}
|
|
449
|
+
|
|
450
|
+
{/* 锚点消息已不在列表(分页/清理)的兜底:按接收序落到流尾 */}
|
|
451
|
+
{timelineByMessage.tail.map((card) => (
|
|
452
|
+
<div key={card.id} className="chat-page__timeline-card mx-page" data-timeline-card={card.id}>
|
|
453
|
+
{card.presentation?.placement === 'sheet' ? (
|
|
454
|
+
<button
|
|
455
|
+
type="button"
|
|
456
|
+
className="tbox-sheet-reopen inline-flex min-h-11 items-center gap-1.5 rounded-full border border-border bg-card px-3.5 text-sm text-primary shadow-xs"
|
|
457
|
+
onClick={() => openSheetCard?.(card.id)}
|
|
458
|
+
>
|
|
459
|
+
{card.title ?? '查看详情'}
|
|
460
|
+
</button>
|
|
461
|
+
) : (
|
|
462
|
+
<CardRenderer card={card} />
|
|
463
|
+
)}
|
|
464
|
+
</div>
|
|
299
465
|
))}
|
|
300
466
|
</div>
|
|
301
467
|
</div>
|
|
@@ -303,7 +469,7 @@ function ChatPageInner() {
|
|
|
303
469
|
<ScrollToBottomButton visible={stick.showJump} onClick={scrollToBottom} />
|
|
304
470
|
</div>
|
|
305
471
|
|
|
306
|
-
{/*
|
|
472
|
+
{/* 有 home 槽的首页和对话态共用输入框上方的快捷胶囊,随输入区一起测量。 */}
|
|
307
473
|
<div ref={chatBottomRef}>
|
|
308
474
|
{!blocked && !showGridCommands && <QuickCommands variant={compactLayout ? 'compact' : 'row'} hidden={voiceActive} onCommand={send} />}
|
|
309
475
|
<ChatInput
|
|
@@ -316,6 +482,17 @@ function ChatPageInner() {
|
|
|
316
482
|
onVoiceActivityChange={setVoiceActive}
|
|
317
483
|
/>
|
|
318
484
|
</div>
|
|
485
|
+
{/* 常挂 + open 驱动:卸载式关闭会跳过 180ms 退场动画(抽屉「啪」地消失) */}
|
|
486
|
+
{sheetCards.length > 0 ? (
|
|
487
|
+
<BottomSheet
|
|
488
|
+
open={!!sheetCard}
|
|
489
|
+
variant="card"
|
|
490
|
+
title={sheetCard?.title ?? '详情'}
|
|
491
|
+
onClose={() => closeSheetCard?.()}
|
|
492
|
+
>
|
|
493
|
+
{sheetCard ? <CardRenderer card={sheetCard} /> : null}
|
|
494
|
+
</BottomSheet>
|
|
495
|
+
) : null}
|
|
319
496
|
{conversationOverlay && (
|
|
320
497
|
<div className="chat-page__conversation-overlay contents">
|
|
321
498
|
{conversationOverlay({ onSend: send, ...(mallName ? { mallName } : {}) })}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
/**
|
|
3
|
+
* 确认层的两个「动作声明侧」字段:`tone`(危险语气)与 `highlights`(权益摘要)。
|
|
4
|
+
*
|
|
5
|
+
* 它们必须能从**动作声明**一路透传到确认层——视图卡(CardView.actions[].confirm)、
|
|
6
|
+
* `ActionList`(三件套)、`CardActionButton`(单动作)三条路径都要过。
|
|
7
|
+
* 漏掉任何一条,业务就只能退回去手写一次确认弹层。
|
|
8
|
+
*/
|
|
9
|
+
import { act } from 'react';
|
|
10
|
+
import { createRoot } from 'react-dom/client';
|
|
11
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
12
|
+
import type { CardView } from '@tbox.cn/app-contracts';
|
|
13
|
+
import { createSurfaceHarness } from '../surface';
|
|
14
|
+
import type { ActionOutcome } from '@tbox.cn/app-agent-sdk-client';
|
|
15
|
+
import { ActionList, CardActionButton, type CardActionSpec } from './primitives';
|
|
16
|
+
import type { CardActions } from './use-card-actions';
|
|
17
|
+
import { ViewCard } from './view-card';
|
|
18
|
+
|
|
19
|
+
const roots: ReturnType<typeof createRoot>[] = [];
|
|
20
|
+
afterEach(async () => {
|
|
21
|
+
await act(async () => {
|
|
22
|
+
for (const root of roots) root.unmount();
|
|
23
|
+
});
|
|
24
|
+
roots.length = 0;
|
|
25
|
+
document.body.innerHTML = '';
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const mount = async (node: React.ReactElement) => {
|
|
29
|
+
const host = document.createElement('div');
|
|
30
|
+
document.body.append(host);
|
|
31
|
+
const root = createRoot(host);
|
|
32
|
+
roots.push(root);
|
|
33
|
+
await act(async () => {
|
|
34
|
+
root.render(node);
|
|
35
|
+
});
|
|
36
|
+
return host;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const SETTLED: ActionOutcome = { status: 'settled', outcome: 'applied' as never };
|
|
40
|
+
|
|
41
|
+
/** 动作外壳:send/invoke 都被记录(与运行时同形的最小替身)。 */
|
|
42
|
+
const api = (): CardActions => ({
|
|
43
|
+
send: vi.fn(async () => SETTLED),
|
|
44
|
+
invoke: vi.fn(async () => SETTLED),
|
|
45
|
+
disabled: false,
|
|
46
|
+
isHistory: false,
|
|
47
|
+
isGenerating: false,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const CONFIRM: NonNullable<CardActionSpec['confirm']> = {
|
|
51
|
+
title: '确认取消报名?',
|
|
52
|
+
tone: 'danger',
|
|
53
|
+
highlights: ['名额将释放给他人'],
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const clickButton = async (host: HTMLElement, label: string) => {
|
|
57
|
+
const button = [...host.querySelectorAll('button')].find((item) => item.textContent?.includes(label));
|
|
58
|
+
expect(button).toBeTruthy();
|
|
59
|
+
await act(async () => {
|
|
60
|
+
button!.click();
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
describe('确认层透传 tone / highlights', () => {
|
|
65
|
+
it('ActionList:声明里的 tone/highlights 进确认层', async () => {
|
|
66
|
+
const invoke = vi.fn(async () => SETTLED);
|
|
67
|
+
const host = await mount(
|
|
68
|
+
<ActionList api={{ ...api(), invoke }} actions={[{ label: '取消报名', invoke: { id: 'cancel' }, confirm: CONFIRM }]} />,
|
|
69
|
+
);
|
|
70
|
+
await clickButton(host, '取消报名');
|
|
71
|
+
expect(invoke).not.toHaveBeenCalled();
|
|
72
|
+
const dialog = document.querySelector('[role="dialog"]') as HTMLElement;
|
|
73
|
+
expect(dialog.textContent).toContain('名额将释放给他人');
|
|
74
|
+
// danger 语气的确认键带破坏色
|
|
75
|
+
expect(dialog.innerHTML).toContain('bg-destructive');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('CardActionButton:单动作同样透传', async () => {
|
|
79
|
+
const host = await mount(
|
|
80
|
+
<CardActionButton api={api()} action={{ label: '取消报名', invoke: { id: 'cancel' }, confirm: CONFIRM }} />,
|
|
81
|
+
);
|
|
82
|
+
await clickButton(host, '取消报名');
|
|
83
|
+
const dialog = document.querySelector('[role="dialog"]') as HTMLElement;
|
|
84
|
+
expect(dialog.textContent).toContain('名额将释放给他人');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('ViewCard:视图卡声明里的 tone/highlights 也透传', async () => {
|
|
88
|
+
// 视图卡用的是契约侧 confirm(description 是字符串),这里显式收窄,避免 ReactNode → string 的赋值
|
|
89
|
+
const view: CardView = {
|
|
90
|
+
header: { title: '活动' },
|
|
91
|
+
blocks: [],
|
|
92
|
+
actions: [
|
|
93
|
+
{
|
|
94
|
+
label: '取消报名',
|
|
95
|
+
invoke: { id: 'cancel' },
|
|
96
|
+
confirm: { title: '确认取消报名?', tone: 'danger', highlights: ['名额将释放给他人'] },
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
};
|
|
100
|
+
const harness = createSurfaceHarness();
|
|
101
|
+
const host = await mount(<>{harness.wrap(<ViewCard view={view} cardId="c1" actionTokens={{ cancel: 't' }} />)}</>);
|
|
102
|
+
await clickButton(host, '取消报名');
|
|
103
|
+
const dialog = document.querySelector('[role="dialog"]') as HTMLElement;
|
|
104
|
+
expect(dialog.textContent).toContain('名额将释放给他人');
|
|
105
|
+
});
|
|
106
|
+
});
|