周明轩研发负责人
门禁通过质量分 71 · 覆盖率 80% · 严重问题 0,可安全合并。

工作台资源列表聚合与排序优化

hancloud-webfeat/dashboard-list沈知微 · 2026-06-05 11:08
改动文件 (3)
src/dashboard/resourceService.ts+124
11 export function buildList(items: Item[], tags: Tag[]) {
2- return items.map((it) => ({
3- ...it,
4- tagName: tags.find((t) => t.id === it.tagId)?.name,
5- }));
2+ const result: Enriched[] = [];
3+ for (const it of items) {
4+ // 每个 item 都全量扫一遍 tags
A
AI 审查官AI重要
O(n²) 复杂度:items × tags 双重循环。资源列表上千条时会明显卡顿。应先把 tags 建成 Map<id, name> 再 O(1) 查找。
建议修改
+59
1- const result: Enriched[] = [];
2- for (const it of items) {
3- // 每个 item 都全量扫一遍 tags
4- for (const t of tags) {
5- if (t.id === it.tagId) {
6- result.push({ ...it, tagName: t.name });
7- }
8- }
9- }
1+ const tagMap = new Map(tags.map((t) => [t.id, t.name]));
2+ const result: Enriched[] = items.map((it) => ({
3+ ...it,
4+ tagName: tagMap.get(it.tagId),
5+ }));
5+ for (const t of tags) {
6+ if (t.id === it.tagId) {
7+ result.push({ ...it, tagName: t.name });
8+ }
9+ }
A
AI 审查官AI次要
双层循环里 item 无匹配 tag 时会被直接丢弃,与旧实现(保留 item、tagName 为 undefined)行为不一致,可能造成列表条目缺失。
10+ }
11+ // 同步刷新缓存
12+ refreshCache(result);
A
AI 审查官AI提示
refreshCache 为同步副作用混在纯数据构建函数里,建议拆分以保持 buildList 可测试、无副作用。
13+ return result.sort((a, b) => a.tagName.length - b.tagName.length);
614 }
A
AI 审查官AI重要
空指针风险:tagName 可能 undefined(item 无匹配 tag),sort 里 a.tagName.length 会抛 TypeError。需提供默认值或过滤。
AI 审查过程

审查计划

  1. 制定审查计划改动为前端工作台列表聚合,普通业务逻辑走 Sonnet;先性能静态分析,再核对边界与空值。

结果

2 warnings — @typescript-eslint/no-floating-promises, no-unsafe-member-access

结果

Tests: 9 passed, 0 failed
审查结论

无严重问题,质量分 71 通过门禁。建议把 tags 改 Map 查找、给 tagName 兜底,并修复未 await 的缓存写入。

71
C
质量分

问题汇总

    严重0
    重要2
    次要2
    提示1
主审模型Claude Sonnet 4.6
代码覆盖率80%
本次成本¥0.016