Files
XHChat/Desktop/src/browser-library.ts
T

80 lines
3.0 KiB
TypeScript

import { app } from 'electron'
import { randomUUID } from 'node:crypto'
import { readFileSync, renameSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
export type Entry = { id: string; url: string; title: string; time: number }
export type DownloadRecord = { id: string; name: string; path: string; state: 'completed' | 'cancelled' | 'interrupted'; time: number }
type Library = { bookmarks: Entry[]; history: Entry[]; downloads: DownloadRecord[] }
let cache: Library | undefined
const file = () => join(app.getPath('userData'), 'browser-library.json')
export function library(): Library {
if (cache) return cache
try {
const saved = JSON.parse(readFileSync(file(), 'utf8')) as Partial<Library>
cache = { bookmarks: parseEntries(saved.bookmarks), history: parseEntries(saved.history),
downloads: Array.isArray(saved.downloads) ? saved.downloads.slice(0, 150).filter((item) =>
typeof item?.id === 'string' && typeof item.name === 'string' && typeof item.path === 'string' &&
typeof item.time === 'number' && ['completed', 'cancelled', 'interrupted'].includes(item.state)) : [] }
} catch {
cache = { bookmarks: [], history: [], downloads: [] }
}
return cache
}
function parseEntries(value: unknown): Entry[] {
if (!Array.isArray(value)) return []
return value.slice(0, 250).filter((item): item is Entry =>
typeof item?.id === 'string' && typeof item.url === 'string' &&
typeof item.title === 'string' && typeof item.time === 'number')
}
function persist(next: Library) {
const target = file()
const temporary = target + '.tmp'
writeFileSync(temporary, JSON.stringify(next), { mode: 0o600 })
renameSync(temporary, target)
cache = next
}
function entry(url: string, title: string): Entry {
return { id: randomUUID(), url, title: title.trim().slice(0, 200), time: Date.now() }
}
export function visit(url: string, title: string) {
const previous = library()
const first = previous.history[0]
if (first?.url === url && first.title === title) return
const history = [entry(url, title), ...previous.history.filter((item) => item.url !== url)].slice(0, 250)
persist({ ...previous, history })
}
export function toggleBookmark(url: string, title: string) {
const previous = library()
const present = previous.bookmarks.some((item) => item.url === url)
if (!present && previous.bookmarks.length >= 250) throw new Error('书签数量已达上限')
const bookmarks = present ? previous.bookmarks.filter((item) => item.url !== url) :
[entry(url, title), ...previous.bookmarks]
persist({ ...previous, bookmarks })
}
export function removeBookmark(id: string) {
const previous = library()
persist({ ...previous, bookmarks: previous.bookmarks.filter((item) => item.id !== id) })
}
export function clearHistory() {
persist({ ...library(), history: [] })
}
export function recordDownload(record: DownloadRecord) {
const previous = library()
persist({ ...previous, downloads: [record, ...previous.downloads].slice(0, 150) })
}
export function clearDownloads() {
persist({ ...library(), downloads: [] })
}