xiaoshuo / ui-src /src /rooms /BookFile.tsx
root
整本书导出/封存/导入;删除时清干净所有痕迹;书本锁不过期、AI 不能自行解除
502de50
Raw History Blame Contribute Delete
8.12 kB
import React, { useRef, useState } from 'react'
import { Archive, CheckCircle2, Download, Upload } from 'lucide-react'
import { exportBook, importBook, readBookFile, projectsApi, Project, BookFile } from '../api'
import { fmtNum, go } from '../lib'
import { Modal, useToast } from '../ui'
const fmtSize = (n: number) => n > 1024 * 1024 ? `${(n / 1024 / 1024).toFixed(1)} MB` : `${Math.max(1, Math.round(n / 1024))} KB`
type Exported = { filename: string; bytes: number; ok: boolean; chapters: number; versions: number }
/** 只导出一份备份(书还留在书房里) */
export function ExportButton({ project, className = 'btn ghost' }: { project: Project; className?: string }) {
const toast = useToast()
const [busy, setBusy] = useState(false)
const run = async () => {
setBusy(true)
try {
const r = await exportBook(project.project_id, project.title)
toast(r.ok ? `已导出 ${r.filename}(${fmtSize(r.bytes)}),文件核对完整` : `已导出 ${r.filename},但核对没通过,建议再导一次`, r.ok ? 'ok' : 'err')
} catch (e: any) { toast(e.message, 'err') } finally { setBusy(false) }
}
return <button className={className} disabled={busy} onClick={run}><Download size={15} />{busy ? '正在打包…' : '导出备份'}</button>
}
/** 封存:先导出、核对文件,再把这本书从书房彻底删除 */
export function ArchiveModal({ open, onClose, project }: { open: boolean; onClose: () => void; project: Project }) {
const toast = useToast()
const [busy, setBusy] = useState<'' | 'export' | 'delete'>('')
const [done, setDone] = useState<Exported | null>(null)
const [noBackup, setNoBackup] = useState(false)
const [saved, setSaved] = useState(false)
const [typed, setTyped] = useState('')
const close = () => { if (busy) return; setDone(null); setNoBackup(false); setSaved(false); setTyped(''); onClose() }
const doExport = async () => {
setBusy('export')
try { const r = await exportBook(project.project_id, project.title); setDone(r); if (!r.ok) toast('文件核对没通过,请再导出一次', 'err') }
catch (e: any) { toast(e.message, 'err') } finally { setBusy('') }
}
const ready = ((done && done.ok && saved) || noBackup) && typed.trim() === project.title
const doDelete = async () => {
setBusy('delete')
try {
const r: any = await projectsApi.remove(project.project_id)
toast(r && r.remaining ? `已删除,但还有 ${r.remaining} 个文件没删掉,请再试一次` : `《${project.title}》已从书房彻底删除`, r && r.remaining ? 'err' : 'ok')
setBusy(''); close(); go('shelf')
} catch (e: any) { toast(e.message, 'err'); setBusy('') }
}
return (
<Modal open={open} onClose={close} title={<>封存《{project.title}》</>} width={560}
footer={<>
<button className="btn ghost" onClick={close} disabled={!!busy}>取消</button>
<button className="btn danger" disabled={!ready || !!busy} onClick={doDelete}>{busy === 'delete' ? '正在删除…' : '从书房彻底删除'}</button>
</>}>
<div className="bf">
<p className="bf-lead">封存 = 把整本书存成一个文件放在你手里,然后从书房里删干净。删掉以后,写作 AI 和协作 AI 在网站上再也读不到这本书,写别的书时就不会混进来。以后想接着写,在书房点「导入一本书」把文件放回来,一切原样恢复。</p>
<div className={`bf-step ${done?.ok ? 'done' : ''}`}>
<div className="bf-num">{done?.ok ? <CheckCircle2 size={18} /> : '1'}</div>
<div className="bf-body">
<b>导出整本书</b>
<span>作品圣经、大纲、全部章节和每一个历史版本、批注、修改记录、发布记录,都在这一个文件里。</span>
{done ? (
<div className={`bf-result ${done.ok ? 'ok' : 'bad'}`}>
{done.ok ? '已下载并核对完整:' : '已下载,但核对没通过(请再导一次):'}<b>{done.filename}</b>
<em>{fmtSize(done.bytes)} · {done.chapters} 章 · {fmtNum(done.versions)} 个版本</em>
</div>
) : null}
<div><button className="btn small" disabled={!!busy} onClick={doExport}><Download size={14} />{busy === 'export' ? '正在打包…' : done ? '再导出一次' : '导出到这台设备'}</button></div>
{done?.ok && (
<label className="bf-check"><input type="checkbox" checked={saved} onChange={e => setSaved(e.target.checked)} />我已经找到这个文件,并另存了一份到网盘 / 微信「文件传输助手」等安全的地方</label>
)}
</div>
</div>
<div className="bf-step">
<div className="bf-num">2</div>
<div className="bf-body">
<b>输入书名确认删除</b>
<span>会删掉这本书在云端的全部内容,以及书本锁、协作 AI 公共记录里跟它有关的条目。</span>
<input className="input" value={typed} onChange={e => setTyped(e.target.value)} placeholder={project.title} />
{!done && <label className="bf-check muted"><input type="checkbox" checked={noBackup} onChange={e => setNoBackup(e.target.checked)} />这本书我不要了,不导出,直接永久删除</label>}
</div>
</div>
</div>
</Modal>
)
}
/** 书房:把封存的书放回来 */
export function ImportBook({ onDone }: { onDone: () => void }) {
const toast = useToast()
const ref = useRef<HTMLInputElement>(null)
const [file, setFile] = useState<File | null>(null)
const [info, setInfo] = useState<{ doc: BookFile; ok: boolean; chapters: number; versions: number } | null>(null)
const [err, setErr] = useState('')
const [busy, setBusy] = useState(false)
const pick = async (f: File | undefined) => {
if (!f) return
setFile(f); setInfo(null); setErr('')
try { setInfo(await readBookFile(await f.arrayBuffer())) } catch (e: any) { setErr(e.message || '这个文件打不开') }
if (ref.current) ref.current.value = ''
}
const close = () => { if (busy) return; setFile(null); setInfo(null); setErr('') }
const run = async () => {
if (!file) return
setBusy(true)
try { const r = await importBook(file); toast(`《${r.project.title}》已经放回书房`, 'ok'); setBusy(false); close(); onDone(); go(`book/${r.project.project_id}/about`) }
catch (e: any) { toast(e.message, 'err'); setBusy(false) }
}
return (
<>
<button className="btn ghost" onClick={() => ref.current?.click()}><Upload size={16} />导入一本书</button>
<input ref={ref} type="file" accept=".xsbook,.gz,.json,application/gzip,application/octet-stream" hidden onChange={e => pick(e.target.files?.[0])} />
<Modal open={!!file} onClose={close} title="导入一本书" width={500}
footer={<>
<button className="btn ghost" onClick={close} disabled={busy}>取消</button>
<button className="btn primary" disabled={busy || (!info && !err) || !!(info && !info.ok) || !!err} onClick={run}>{busy ? '正在放回书房…' : '放回书房'}</button>
</>}>
<div className="bf">
{!info && !err && <p className="bf-lead">正在读取文件…</p>}
{err && <div className="bf-result bad">{err}</div>}
{info && (
<>
<div className={`bf-result ${info.ok ? 'ok' : 'bad'}`}>
{info.ok ? <>文件完整:</> : <>文件和导出时对不上,可能损坏了,不能导入:</>}<b>《{info.doc.project.title}》</b>
<em>{info.chapters} 章 · {fmtNum(info.versions)} 个版本 · 导出于 {info.doc.exported_at.slice(0, 10)}</em>
</div>
{info.ok && <p className="bf-lead">放回后,这本书的全部内容原样恢复。写作 AI 不会自动转去写它——要让 AI 处理这本书,在书房顶部的横条里把它换过去,并且最好开一个新对话。</p>}
</>
)}
</div>
</Modal>
</>
)
}