File size: 8,306 Bytes
8a6a2bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, { useMemo, useState } from 'react'
import { Highlighter, History, ChevronRight, MapPin, RefreshCw } from 'lucide-react'
import { ChangeItem, Note, projectsApi } from '../api'
import { cnNum, cx, fmtAgo, fmtDate, go, useAsync, useLocal } from '../lib'
import { Empty, ErrorNote, Loading, Segmented } from '../ui'
import type { BookCtx } from './Book'
import { NOTE_TYPE, SEVERITY, STATUS } from './Review'

/* 批阅汇总 + 修改记录:所有章节的批阅集中在一页,点一下跳到正文原句;
 * 正文的每次改动(谁改的、改了哪几段)也在这里,写作 AI 和协作 AI 读的是同一份记录。 */
export function jumpToNote(pid: string, cid: string, nid: string) {
  try { sessionStorage.setItem('xs_jump_note', JSON.stringify({ cid, nid, ts: Date.now() })) } catch {}
  go(`book/${pid}/${cid}`)
}

export default function NotesHub({ ctx, initial }: { ctx: BookCtx; initial?: string }) {
  const pid = ctx.bundle.project.project_id
  const [view, setView] = useLocal<'notes' | 'changes'>('xs_hub_view', initial === 'changes' ? 'changes' : 'notes')
  return (
    <div className="page narrow hub">
      <div className="eyebrow">{view === 'notes' ? <><Highlighter size={13} />批阅汇总</> : <><History size={13} />修改记录</>}</div>
      <h1 className="hub-title">{view === 'notes' ? '所有章节的批阅' : '正文修改记录'}</h1>
      <p className="muted hub-lead">{view === 'notes'
        ? '点任意一条,会打开那一章并滚到被批注的句子。'
        : '每次存为版本、恢复旧版本都会自动记一笔。你还没存为版本的改动也会同步到云端。我(写作 AI)和协作 AI 每次动笔前都会先看这里。'}</p>
      <Segmented value={view} onChange={setView} items={[{ id: 'notes', label: '批阅' }, { id: 'changes', label: '修改记录' }]} />
      <div className="hub-body">{view === 'notes' ? <AllNotes pid={pid} /> : <Changes pid={pid} />}</div>
    </div>
  )
}

function AllNotes({ pid }: { pid: string }) {
  const [filter, setFilter] = useLocal<'open' | 'all'>('xs_hub_filter', 'open')
  const res = useAsync(() => projectsApi.allNotes(pid), [pid])
  const groups = useMemo(() => {
    const cs = res.data?.chapters || []
    return cs.map(c => ({ ...c, notes: (c.notes || []).filter(n => filter === 'all' || n.status === 'open').sort((a, b) => (a.start || 0) - (b.start || 0)) }))
      .filter(c => c.notes.length).sort((a, b) => a.number - b.number)
  }, [res.data, filter])
  const all = (res.data?.chapters || []).flatMap(c => c.notes || [])
  const open = all.filter(n => n.status === 'open').length
  if (res.error) return <ErrorNote error={res.error} onRetry={res.reload} />
  if (!res.data) return <Loading label="正在收集批阅…" />
  return (
    <>
      <div className="hub-bar">
        <Segmented value={filter} onChange={setFilter} items={[{ id: 'open', label: `待修改 ${open}` }, { id: 'all', label: `全部 ${all.length}` }]} />
        <button className="link-btn" onClick={res.reload}><RefreshCw size={13} />刷新</button>
      </div>
      {groups.length === 0 ? <Empty icon={<Highlighter size={20} />} title={filter === 'open' ? '没有待修改的批阅' : '还没有批阅'}>{filter === 'open' && all.length ? '全部处理完了。' : '在正文里点「批阅」,选中句子就能写批注。'}</Empty> : groups.map(g => (
        <section key={g.chapter_id} className="hub-group">
          <button className="hub-ch" onClick={() => go(`book/${pid}/${g.chapter_id}`)}>第{cnNum(g.number)}章 {g.title}<span className="muted">{g.notes.length} 条</span><ChevronRight size={14} /></button>
          <ul className="note-list">
            {g.notes.map(n => <HubNote key={n.note_id} n={n} onOpen={() => jumpToNote(pid, g.chapter_id, n.note_id)} />)}
          </ul>
        </section>
      ))}
    </>
  )
}

function HubNote({ n, onOpen }: { n: Note; onOpen: () => void }) {
  return (
    <li className={cx('note hub-note', `sev-${n.severity}`, n.type === 'praise' && 'praise', n.status !== 'open' && 'done')} onClick={onOpen} role="button" tabIndex={0}
      onKeyDown={e => { if (e.key === 'Enter') onOpen() }} title="打开这一章,定位到这句">
      <div className="note-head">
        <span className="tag">{NOTE_TYPE[n.type] || n.type}</span>
        {n.type !== 'praise' && <span className={cx('sev', n.severity)}>{SEVERITY[n.severity]}</span>}
        <span className={cx('note-st', n.status)}>{STATUS[n.status]}</span>
        <span className="muted">{n.created_by === 'ai' ? 'AI · ' : ''}{fmtAgo(n.created_at)}</span>
        <span className="hub-go"><MapPin size={13} />定位</span>
      </div>
      <div className={cx('note-quote', !n.quote && 'general')}>{n.quote ? `「${n.quote.length > 80 ? n.quote.slice(0, 80) + '…' : n.quote}」` : '整章'}</div>
      {n.comment && <div className="note-body">{n.comment}</div>}
      {n.suggestion && <div className="note-sug"><span>建议</span>{n.suggestion}</div>}
      {n.reply && <div className="note-reply"><span>AI 回复</span>{n.reply}</div>}
    </li>
  )
}

function Changes({ pid }: { pid: string }) {
  const [by, setBy] = useLocal<'author' | 'all'>('xs_hub_by', 'author')
  const res = useAsync(() => projectsApi.changes(pid, { by: by === 'author' ? 'author' : undefined, limit: 200 }), [pid, by])
  if (res.error) return <ErrorNote error={res.error} onRetry={res.reload} />
  if (!res.data) return <Loading label="正在读取修改记录…" />
  const { items, unsaved_author_drafts: drafts } = res.data
  return (
    <>
      <div className="hub-bar">
        <Segmented value={by} onChange={setBy} items={[{ id: 'author', label: '我改的' }, { id: 'all', label: '全部(含 AI)' }]} />
        <button className="link-btn" onClick={res.reload}><RefreshCw size={13} />刷新</button>
      </div>
      {drafts.length > 0 && (
        <section className="hub-group">
          <div className="hub-sub">还没存为版本的改动</div>
          {drafts.map(d => (
            <ChangeCard key={d.chapter_id} pid={pid} c={{ chapter_id: d.chapter_id, number: d.number, title: d.title, at: d.at, hunks: d.hunks, hunk_count: d.hunk_count, word_delta: d.word_delta }} tag="未存版本" />
          ))}
        </section>
      )}
      {items.length === 0 && drafts.length === 0 ? <Empty icon={<History size={20} />} title="还没有修改记录">从现在起,你在稿纸上的每次改动都会记在这里。</Empty> : items.length > 0 && (
        <section className="hub-group">
          {drafts.length > 0 && <div className="hub-sub">已存的修改</div>}
          {items.map(c => <ChangeCard key={c.change_id} pid={pid} c={c} tag={c.kind === 'rollback' ? '恢复旧版本' : undefined} />)}
        </section>
      )}
    </>
  )
}

function ChangeCard({ pid, c, tag }: { pid: string; c: Partial<ChangeItem> & Pick<ChangeItem, 'chapter_id' | 'number' | 'title' | 'at' | 'hunks' | 'hunk_count' | 'word_delta'>; tag?: string }) {
  const [open, setOpen] = useState(false)
  const shown = open ? c.hunks : c.hunks.slice(0, 2)
  return (
    <div className="change-card">
      <div className="change-head">
        <button className="hub-ch inline" onClick={() => go(`book/${pid}/${c.chapter_id}`)}>第{cnNum(c.number)}章 {c.title}</button>
        {tag && <span className="tag">{tag}</span>}
        <span className="muted">{c.by ? `${c.by} · ` : ''}{fmtDate(c.at)}({fmtAgo(c.at)})</span>
        <span className="muted">改了 {c.hunk_count} 处 · {c.word_delta > 0 ? '+' : ''}{c.word_delta} 字</span>
      </div>
      {c.summary && <div className="note-body">{c.summary}</div>}
      {shown.map((h, i) => (
        <div key={i} className="change-hunk">
          <div className="change-para">第 {h.para} 段</div>
          {h.before.map((t, j) => <div key={'b' + j} className="diff-row del">{t}</div>)}
          {h.after.map((t, j) => <div key={'a' + j} className="diff-row add">{t}</div>)}
        </div>
      ))}
      {c.hunks.length > 2 && <button className="link-btn" onClick={() => setOpen(!open)}>{open ? '收起' : `展开其余 ${c.hunks.length - 2} 处`}</button>}
      {(c.acks || []).map(a => <div key={a.by} className="note-reply"><span>{a.by} 已看</span>{a.note || '已读'}</div>)}
    </div>
  )
}