F1-3: 工作台首页与待办中心成品化
- 新增 WorkbenchPage (/workbench):KPI 卡片(待办数接真实 API)、待办快照(最近 5 条)、常用功能(localStorage 最近访问优先,回退菜单叶子) - 升级 TodoPage:待办/已办 Tab、关键字本地筛选、pageUrl 优先跳转、Pagination 组件复用 - 新增 api/done.ts:GET /api/done 已办接口,与待办接口同样的 503 降级逻辑 - 新增 utils/recentPages.ts:localStorage 常用功能工具,MAX_RECENT=8 LRU - 新增 utils/recentPages.test.ts:5 个单测覆盖写入/提升/截断/清空逻辑 - AppShell:顶栏铃铛接真实待办数(60s 轮询);默认 Tab 改为工作台 - App.tsx:新增 /workbench 路由,根路径重定向目标改为 /workbench Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
52
src/utils/recentPages.test.ts
Normal file
52
src/utils/recentPages.test.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { clearRecentPages, loadRecentPages, recordRecentPage } from './recentPages';
|
||||
|
||||
// jsdom 已提供 localStorage 实现,测试前清空
|
||||
beforeEach(() => {
|
||||
clearRecentPages();
|
||||
});
|
||||
|
||||
describe('recordRecentPage', () => {
|
||||
it('首次写入后可读取', () => {
|
||||
recordRecentPage({ templateId: 'tpl-1', name: '付款申请', path: '合同管理 / 付款申请' });
|
||||
const list = loadRecentPages();
|
||||
expect(list).toHaveLength(1);
|
||||
expect(list[0].templateId).toBe('tpl-1');
|
||||
expect(list[0].name).toBe('付款申请');
|
||||
});
|
||||
|
||||
it('重复访问同一页面提升到最前,不重复', () => {
|
||||
recordRecentPage({ templateId: 'tpl-1', name: '付款申请', path: 'A' });
|
||||
recordRecentPage({ templateId: 'tpl-2', name: '质量整改', path: 'B' });
|
||||
recordRecentPage({ templateId: 'tpl-1', name: '付款申请', path: 'A' });
|
||||
const list = loadRecentPages();
|
||||
expect(list).toHaveLength(2);
|
||||
expect(list[0].templateId).toBe('tpl-1'); // 最新访问排第一
|
||||
expect(list[1].templateId).toBe('tpl-2');
|
||||
});
|
||||
|
||||
it('超过 MAX_RECENT(8) 时截断最旧记录', () => {
|
||||
for (let i = 1; i <= 10; i++) {
|
||||
recordRecentPage({ templateId: `tpl-${i}`, name: `页面${i}`, path: `路径${i}` });
|
||||
}
|
||||
const list = loadRecentPages();
|
||||
expect(list).toHaveLength(8);
|
||||
// 最新访问的是 tpl-10,应在最前
|
||||
expect(list[0].templateId).toBe('tpl-10');
|
||||
// 最早的 tpl-1、tpl-2 应被淘汰
|
||||
expect(list.find((p) => p.templateId === 'tpl-1')).toBeUndefined();
|
||||
expect(list.find((p) => p.templateId === 'tpl-2')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('clearRecentPages 后列表为空', () => {
|
||||
recordRecentPage({ templateId: 'tpl-1', name: '付款申请', path: 'A' });
|
||||
clearRecentPages();
|
||||
expect(loadRecentPages()).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadRecentPages', () => {
|
||||
it('localStorage 为空时返回空数组', () => {
|
||||
expect(loadRecentPages()).toEqual([]);
|
||||
});
|
||||
});
|
||||
48
src/utils/recentPages.ts
Normal file
48
src/utils/recentPages.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
// 常用/最近访问功能工具 —— 基于 localStorage
|
||||
// 记录用户最近打开的菜单叶子(templateId),用于工作台「常用功能」快捷入口。
|
||||
// 最多保留 MAX_RECENT 条,按最近访问顺序排列(最新在前)。
|
||||
|
||||
const STORAGE_KEY = 'eip4-recent-pages';
|
||||
const MAX_RECENT = 8;
|
||||
|
||||
export interface RecentPage {
|
||||
templateId: string;
|
||||
name: string;
|
||||
/** 面包屑路径,如 "合同管理 / 付款申请" */
|
||||
path: string;
|
||||
/** 最近访问时间戳 */
|
||||
ts: number;
|
||||
}
|
||||
|
||||
/** 读取最近访问列表 */
|
||||
export function loadRecentPages(): RecentPage[] {
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
if (!raw) return [];
|
||||
const parsed = JSON.parse(raw) as RecentPage[];
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return parsed;
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/** 记录一次访问(已存在则提升到最前,超出 MAX_RECENT 截断) */
|
||||
export function recordRecentPage(page: Omit<RecentPage, 'ts'>): void {
|
||||
const list = loadRecentPages().filter((p) => p.templateId !== page.templateId);
|
||||
const next: RecentPage[] = [{ ...page, ts: Date.now() }, ...list].slice(0, MAX_RECENT);
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(next));
|
||||
} catch {
|
||||
// localStorage 不可用时静默忽略
|
||||
}
|
||||
}
|
||||
|
||||
/** 清空记录(测试/重置用) */
|
||||
export function clearRecentPages(): void {
|
||||
try {
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
} catch {
|
||||
// 忽略
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user