Code audit fixes: - CRITICAL: reorder Markdown pipeline (fixImages before sanitize) - CRITICAL: fix path prefix separator check - BLOCKING: remove duplicate useEffect in Editor - BLOCKING: skip onChange when content unchanged - BLOCKING: optimize Sidebar re-render with useMemo - HIGH: cleanup FileWatcher polling intervals - HIGH: improve validatePath segment check - HIGH: fix isExternalUpdate race with counter - HIGH: add will-navigate / setWindowOpenHandler - HIGH: explicit strip in sanitize schema - MEDIUM: random temp file suffix instead of Date.now() - MEDIUM/LOW: add IndexedDB error boundaries - LOW: support UTF-16 BOM detection
54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import { db, type RecentFile } from './schema'
|
|
import { logError } from '../lib/errorHandler'
|
|
|
|
export const recentFilesRepository = {
|
|
async add(filePath: string): Promise<void> {
|
|
try {
|
|
const existing: RecentFile | undefined = await db.recentFiles.where('filePath').equals(filePath).first()
|
|
if (existing) {
|
|
await db.recentFiles.update(existing.id!, { lastOpened: Date.now() })
|
|
} else {
|
|
await db.recentFiles.add({ filePath, lastOpened: Date.now() })
|
|
}
|
|
// L-06: 清理超过 50 条的旧记录
|
|
const all: RecentFile[] = await db.recentFiles.orderBy('lastOpened').reverse().toArray()
|
|
if (all.length > 50) {
|
|
const toDelete = all.slice(50)
|
|
await db.recentFiles.bulkDelete(toDelete.map((f: RecentFile) => f.id!))
|
|
}
|
|
} catch (error) {
|
|
logError('添加最近文件失败', error)
|
|
}
|
|
},
|
|
|
|
async getAll(limit: number = 20): Promise<string[]> {
|
|
try {
|
|
const files: RecentFile[] = await db.recentFiles
|
|
.orderBy('lastOpened')
|
|
.reverse()
|
|
.limit(limit)
|
|
.toArray()
|
|
return files.map((f: RecentFile) => f.filePath)
|
|
} catch (error) {
|
|
logError('读取最近文件失败', error)
|
|
return []
|
|
}
|
|
},
|
|
|
|
async remove(filePath: string): Promise<void> {
|
|
try {
|
|
await db.recentFiles.where('filePath').equals(filePath).delete()
|
|
} catch (error) {
|
|
logError('删除最近文件失败', error)
|
|
}
|
|
},
|
|
|
|
async clear(): Promise<void> {
|
|
try {
|
|
await db.recentFiles.clear()
|
|
} catch (error) {
|
|
logError('清空最近文件失败', error)
|
|
}
|
|
}
|
|
}
|