task-124(记录与版本中心): 实现历史结果预览抽屉

新增 history-preview.ts:从历史记录提取结果图/长图并限制数量,避免大内容阻塞。

TDD: task-124.test.ts 8 用例 RED→GREEN。
This commit is contained in:
2026-09-05 17:32:08 +08:00
parent df8255c41e
commit d8950e93da
2 changed files with 97 additions and 0 deletions
@@ -0,0 +1,29 @@
/** 历史结果预览抽屉(任务 124):从历史记录提取可预览图片并限制数量,避免大内容阻塞页面;纯逻辑。 */
import type { HistoryRecordItem } from './history-dto.ts'
export interface PreviewDescriptor {
images: string[]
}
function safeHttpUrl(value: unknown): string {
const url = typeof value === 'string' ? value.trim() : ''
return /^https?:\/\//i.test(url) ? url : ''
}
/** 构建预览描述(结果图 + 长图,按记录顺序去重,数量受 limit 限制)。 */
export function buildPreviewDescriptor(item: HistoryRecordItem | null | undefined, limit: number): PreviewDescriptor {
const images: string[] = []
const seen = new Set<string>()
const max = typeof limit === 'number' && Number.isFinite(limit) && limit > 0 ? Math.floor(limit) : 20
const push = (url: string) => {
const clean = safeHttpUrl(url)
if (!clean || seen.has(clean) || images.length >= max) return
seen.add(clean)
images.push(clean)
}
if (item && typeof item === 'object') {
if (Array.isArray(item.resultUrls)) item.resultUrls.forEach((url) => push(url))
push(item.longImageUrl)
}
return { images }
}