xiaoshuo / ui-src /src /rooms /progress.ts
root
稿纸开关侧栏不跳回开头;阅读进度:接着看、目录每章进度、回到原处提示、下一章;协作 AI 审查全景包与牵连检查
c46ffbd
Raw History Blame Contribute Delete
3.15 kB
import { useEffect, useState } from 'react'
import { projectsApi, type ReadProgress } from '../api'
// 全书阅读进度:稿纸(正文)和通读两边看到哪儿,合成一份。
// 本机即时记,云端兜底;目录、作品概览、稿纸、通读都从这里取。
export type Spot = { cid: string; r: number; at: number; mode: 'edit' | 'read'; para?: number }
export type ChProg = { r: number; max: number; at: number; mode: 'edit' | 'read'; para?: number }
export type BookProgress = { chapters: Record<string, ChProg>; last: Spot | null; ready: boolean }
const EV = 'xs:progress'
const key = (pid: string) => `xs_prog_${pid}`
const readLocal = (pid: string): Record<string, ChProg> => { try { return JSON.parse(localStorage.getItem(key(pid)) || '{}') || {} } catch { return {} } }
const server = new Map<string, ReadProgress | null>()
const loading = new Map<string, Promise<void>>()
const timers = new Map<string, any>()
/** 记一笔:某章看到了 r(0~1) */
export function noteProgress(pid: string, s: Omit<Spot, 'at'>) {
const m = readLocal(pid)
const prev = m[s.cid]
m[s.cid] = { r: s.r, max: Math.max(s.r, prev?.max || 0), at: Date.now(), mode: s.mode, para: s.para }
try { localStorage.setItem(key(pid), JSON.stringify(m)) } catch {}
clearTimeout(timers.get(pid))
timers.set(pid, setTimeout(() => window.dispatchEvent(new CustomEvent(EV, { detail: pid })), 350))
}
function merge(pid: string): BookProgress {
const out: Record<string, ChProg> = {}
const sv = server.get(pid)
for (const [cid, c] of Object.entries(sv?.chapters || {})) {
const at = c.at ? new Date(c.at).getTime() : 0
const er = c.edit_ratio ?? -1, rr = c.read_ratio ?? -1
const r = Math.max(er, rr)
if (r < 0) continue
out[cid] = { r, max: r, at, mode: rr > er ? 'read' : 'edit', para: c.read_para }
}
for (const [cid, c] of Object.entries(readLocal(pid))) {
const s = out[cid]
if (!s || c.at >= s.at) out[cid] = { ...c, max: Math.max(c.max || 0, s?.max || 0) }
else out[cid] = { ...s, max: Math.max(s.max, c.max || 0) }
}
let last: Spot | null = null
for (const [cid, c] of Object.entries(out)) if (!last || c.at > last.at) last = { cid, r: c.r, at: c.at, mode: c.mode, para: c.para }
return { chapters: out, last, ready: server.has(pid) }
}
export function useBookProgress(pid: string): BookProgress {
const [state, setState] = useState<BookProgress>(() => merge(pid))
useEffect(() => {
let alive = true
const refresh = () => alive && setState(merge(pid))
refresh()
if (!server.has(pid) && !loading.has(pid)) {
loading.set(pid, projectsApi.progress(pid).then(d => { server.set(pid, d) }, () => { server.set(pid, null) }).finally(() => loading.delete(pid)))
}
loading.get(pid)?.then(refresh)
const on = (e: Event) => { if ((e as CustomEvent).detail === pid) refresh() }
window.addEventListener(EV, on)
return () => { alive = false; window.removeEventListener(EV, on) }
}, [pid])
return state
}
export const pct = (r: number) => `${Math.round(Math.min(1, Math.max(0, r)) * 100)}%`
export const isDone = (r: number) => r >= 0.95