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 (
{view === 'notes' ? <>批阅汇总> : <>修改记录>}
{view === 'notes' ? '所有章节的批阅' : '正文修改记录'}
{view === 'notes'
? '点任意一条,会打开那一章并滚到被批注的句子。'
: '每次存为版本、恢复旧版本都会自动记一笔。你还没存为版本的改动也会同步到云端。我(写作 AI)和协作 AI 每次动笔前都会先看这里。'}
)
}
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
if (!res.data) return
return (
<>
{groups.length === 0 ? } title={filter === 'open' ? '没有待修改的批阅' : '还没有批阅'}>{filter === 'open' && all.length ? '全部处理完了。' : '在正文里点「批阅」,选中句子就能写批注。'} : groups.map(g => (
{g.notes.map(n => jumpToNote(pid, g.chapter_id, n.note_id)} />)}
))}
>
)
}
function HubNote({ n, onOpen }: { n: Note; onOpen: () => void }) {
return (
{ if (e.key === 'Enter') onOpen() }} title="打开这一章,定位到这句">
{NOTE_TYPE[n.type] || n.type}
{n.type !== 'praise' && {SEVERITY[n.severity]}}
{STATUS[n.status]}
{n.created_by === 'ai' ? 'AI · ' : ''}{fmtAgo(n.created_at)}
定位
{n.quote ? `「${n.quote.length > 80 ? n.quote.slice(0, 80) + '…' : n.quote}」` : '整章'}
{n.comment && {n.comment}
}
{n.suggestion && 建议{n.suggestion}
}
{n.reply && AI 回复{n.reply}
}
)
}
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
if (!res.data) return
const { items, unsaved_author_drafts: drafts } = res.data
return (
<>
{drafts.length > 0 && (
还没存为版本的改动
{drafts.map(d => (
))}
)}
{items.length === 0 && drafts.length === 0 ? } title="还没有修改记录">从现在起,你在稿纸上的每次改动都会记在这里。 : items.length > 0 && (
{drafts.length > 0 && 已存的修改
}
{items.map(c => )}
)}
>
)
}
function ChangeCard({ pid, c, tag }: { pid: string; c: Partial & Pick; tag?: string }) {
const [open, setOpen] = useState(false)
const shown = open ? c.hunks : c.hunks.slice(0, 2)
return (
{tag && {tag}}
{c.by ? `${c.by} · ` : ''}{fmtDate(c.at)}({fmtAgo(c.at)})
改了 {c.hunk_count} 处 · {c.word_delta > 0 ? '+' : ''}{c.word_delta} 字
{c.summary &&
{c.summary}
}
{shown.map((h, i) => (
第 {h.para} 段
{h.before.map((t, j) =>
{t}
)}
{h.after.map((t, j) =>
{t}
)}
))}
{c.hunks.length > 2 &&
}
{(c.acks || []).map(a =>
{a.by} 已看{a.note || '已读'}
)}
)
}