File size: 3,147 Bytes
c46ffbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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