Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 把 warning toast 默认停留拉到 8 秒 (#2802)

Dashf6c86de13a22
diff --git a/apps/desktop/src/renderer/__tests__/toastDuration.test.ts b/apps/desktop/src/renderer/__tests__/toastDuration.test.ts
new file mode 100644
index 0000000000..25cbcde36b
--- /dev/null
+++ b/apps/desktop/src/renderer/__tests__/toastDuration.test.ts
@@ -0,0 +1,68 @@
+/**
+ * toastDuration.test.ts
+ * ---------------------------------------------------------------------------
+ * warning 与 error 共用 8000ms 默认停留:警告常带操作指引,1.2s 读不完。
+ * info / success 仍保持 1200ms。显式 duration 覆盖默认。
+ */
+
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { getToastSnapshot, toast } from '../lib/toast';
+
+/** 退出动画时长(与 lib/toast.ts 的 EXIT_ANIMATION_MS 对齐)+ 少量余量 */
+const EXIT_MS = 300 + 50;
+
+function item(id: string) {
+ return getToastSnapshot().find((t) => t.id === id);
+}
+
+function isVisible(id: string): boolean {
+ return getToastSnapshot().some((t) => t.id === id);
+}
+
+describe('toast default duration', () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ });
+
+ afterEach(() => {
+ toast.dismissAll();
+ vi.advanceTimersByTime(EXIT_MS);
+ vi.useRealTimers();
+ });
+
+ it('info / success 默认 1200ms', () => {
+ const infoId = toast.info('i');
+ const successId = toast.success('s');
+ expect(item(infoId)?.duration).toBe(1200);
+ expect(item(successId)?.duration).toBe(1200);
+ });
+
+ it('warning / error 默认 8000ms', () => {
+ const warningId = toast.warning('w');
+ const errorId = toast.error('e');
+ expect(item(warningId)?.duration).toBe(8000);
+ expect(item(errorId)?.duration).toBe(8000);
+ });
+
+ it('warning 在 1200ms 后仍可见,8000ms 后退出', () => {
+ const id = toast.warning('switch provider');
+ vi.advanceTimersByTime(1200);
+ expect(isVisible(id)).toBe(true);
+ expect(item(id)?.exiting).toBe(false);
+
+ vi.advanceTimersByTime(6800);
+ expect(item(id)?.exiting).toBe(true);
+
+ vi.advanceTimersByTime(EXIT_MS);
+ expect(isVisible(id)).toBe(false);
+ });
+
+ it('显式 duration 覆盖 warning 默认', () => {
+ const id = toast.warning('custom', { duration: 4000 });
+ expect(item(id)?.duration).toBe(4000);
+
+ vi.advanceTimersByTime(4000);
+ expect(item(id)?.exiting).toBe(true);
+ });
+});
diff --git a/apps/desktop/src/renderer/lib/toast.ts b/apps/desktop/src/renderer/lib/toast.ts
index 9bbfecf238..88b7d1e852 100644
--- a/apps/desktop/src/renderer/lib/toast.ts
+++ b/apps/desktop/src/renderer/lib/toast.ts
@@ -5,8 +5,8 @@
* - 无 Provider。任意代码(包括非 React 的 fetch 拦截器)都可调用 toast.xxx()
* - 模块级单例 Store,用 useSyncExternalStore 订阅
* - 最大并发 3 条,超出部分 FIFO 进入等待队列
- * - 默认 duration 按 variant 区分:success / warning 1200ms,error 8000ms
- * (错误信息需要用户读完,太短根本来不及看清);0 表示永久显示
+ * - 默认 duration 按 variant 区分:info / success 1200ms,warning / error 8000ms
+ * (警告和错误都需要用户读完,太短根本来不及看清);0 表示永久显示
* - hover 悬停时暂停自动关闭(pauseAutoDismiss / resumeAutoDismiss),移开后按剩余时长继续
* - 退出动画完成后(200ms)才真正从 state 移除
*
@@ -27,7 +27,7 @@ export interface ToastSource {
}
export interface ToastOptions {
- /** 自动关闭时长(ms)。不传时按 variant 取默认:info / success / warning 1200,error 8000;0 表示永久显示 */
+ /** 自动关闭时长(ms)。不传时按 variant 取默认:info / success 1200,warning / error 8000;0 表示永久显示 */
duration?: number;
/** 来源身份头(见 ToastSource;主机自己的提示不传) */
source?: ToastSource;
@@ -52,8 +52,8 @@ export interface ToastItem {
// ============================================================
const DEFAULT_DURATION = 1200;
-// error 提示需要用户读完(且常带诊断信息),默认停留时间显著拉长
-const DEFAULT_ERROR_DURATION = 8000;
+// warning / error 都需要用户读完(warning 常带操作指引,error 常带诊断),默认停留显著拉长
+const DEFAULT_ALERT_DURATION = 8000;
const MAX_ACTIVE = 3;
const EXIT_ANIMATION_MS = 300;
@@ -224,11 +224,7 @@ function finalizeRemove(id: string) {
// 对外命令式 API
// ============================================================
-function createItem(
- variant: ToastVariant,
- message: string,
- options?: ToastOptions,
-): string {
+function createItem(variant: ToastVariant, message: string, options?: ToastOptions): string {
const id = generateId();
const item: ToastItem = {
id,
@@ -236,7 +232,7 @@ function createItem(
message,
duration:
options?.duration ??
- (variant === 'error' ? DEFAULT_ERROR_DURATION : DEFAULT_DURATION),
+ (variant === 'error' || variant === 'warning' ? DEFAULT_ALERT_DURATION : DEFAULT_DURATION),
...(options?.source ? { source: options.source } : {}),
onClose: options?.onClose,
createdAt: Date.now(),