xiaoshuo / ui-src /src /App.tsx
root
正文/通读工具栏统一方形图标+悬停提示;手机:侧栏全屏带返回、正文时隐藏底栏、选字批注按钮、批注底部弹层
25b6182
Raw History Blame Contribute Delete
6.32 kB
import React, { useEffect, useState } from 'react'
import { BookOpen, Library, Radio, Compass, Moon, Sun, Lock, Feather, Layers, TrendingUp, Bot } from 'lucide-react'
import { authRequired, checkPassword, getPassword, setPassword, setUnauthorizedHandler } from './api'
import { cx, useRoute } from './lib'
import { ConfirmHost, ToastHost, Spinner } from './ui'
import Shelf from './rooms/Shelf'
import Book from './rooms/Book'
import LibraryRoom from './rooms/Library'
import Sources from './rooms/Sources'
import Guide from './rooms/Guide'
import Motif2Room from './rooms/Motif2'
import RankRoom from './rooms/Rank'
import AgentRoom from './rooms/Agent'
type Gate = 'checking' | 'locked' | 'open'
export default function App() {
const [gate, setGate] = useState<Gate>('checking')
useEffect(() => {
setUnauthorizedHandler(() => setGate('locked'))
;(async () => {
const need = await authRequired()
if (!need) return setGate('open')
const pw = getPassword()
if (pw && await checkPassword(pw).catch(() => false)) return setGate('open')
setGate('locked')
})()
}, [])
return (
<ToastHost>
<ConfirmHost>
{gate === 'checking' && <div className="boot"><Spinner size={20} label="正在打开书房…" /></div>}
{gate === 'locked' && <LockScreen onOpen={() => setGate('open')} />}
{gate === 'open' && <Shell onLock={() => { setPassword(''); setGate('locked') }} />}
</ConfirmHost>
</ToastHost>
)
}
function LockScreen({ onOpen }: { onOpen: () => void }) {
const [pw, setPw] = useState('')
const [err, setErr] = useState('')
const [busy, setBusy] = useState(false)
const submit = async (e: React.FormEvent) => {
e.preventDefault()
if (!pw.trim()) return
setBusy(true); setErr('')
const ok = await checkPassword(pw.trim()).catch(() => false)
setBusy(false)
if (ok) { setPassword(pw.trim()); onOpen() } else setErr('密码不对,请再试一次')
}
return (
<div className="lock">
<form className="lock-card" onSubmit={submit}>
<div className="lock-seal">文</div>
<h1>小说写作台</h1>
<p className="lock-sub">稿纸、设定、素材,都在这里。</p>
<input className="input lock-input" type="password" placeholder="访问密码" value={pw} onChange={e => setPw(e.target.value)} autoFocus />
{err && <div className="lock-err">{err}</div>}
<button className="btn primary block" disabled={busy}>{busy ? '正在核对…' : '进入书房'}</button>
<p className="lock-foot">密码只保存在这台设备的浏览器里</p>
</form>
</div>
)
}
const ROOMS = [
{ id: 'guide', match: ['guide'], label: '手册', icon: Compass },
{ id: 'shelf', match: ['', 'shelf', 'book'], label: '书房', icon: BookOpen },
{ id: 'agent', match: ['agent'], label: 'AI 协作', icon: Bot },
{ id: 'library', match: ['library'], label: '素材馆', icon: Library },
{ id: 'rank', match: ['rank'], label: '热榜', icon: TrendingUp },
{ id: 'motif2', match: ['motif2'], label: '母题库', icon: Layers },
{ id: 'sources', match: ['sources'], label: '采集', icon: Radio },
]
function Shell({ onLock }: { onLock: () => void }) {
const [parts, go] = useRoute()
const [night, setNight] = useState(() => document.documentElement.dataset.theme === 'night')
useEffect(() => {
document.documentElement.dataset.theme = night ? 'night' : 'paper'
try { localStorage.setItem('xs_theme', night ? 'night' : 'paper') } catch {}
const meta = document.querySelector('meta[name=theme-color]')
meta?.setAttribute('content', night ? '#1a1917' : '#f3efe6')
}, [night])
const head = parts[0] || ''
const room = ROOMS.find(r => r.match.includes(head)) || ROOMS.find(r => r.id === 'shelf')!
useEffect(() => {
const titles: Record<string, string> = { shelf: '书房', library: '素材馆', rank: '番茄热榜', motif2: '母题库', sources: '素材采集', guide: '写作手册', agent: 'AI 协作' }
if (room.id !== 'shelf' || head !== 'book') document.title = `${titles[room.id]} · 小说写作台`
}, [room.id, head])
let content: React.ReactNode
if (head === 'book' && parts[1]) content = <Book key={parts[1]} projectId={parts[1]} sub={parts[2] || ''} arg={parts[3] || ''} />
else if (head === 'rank') content = <RankRoom tab={parts[1] || 'analysis'} arg={parts[2] || ''} />
else if (head === 'motif2') content = <Motif2Room tab={parts[1] || 'role'} arg={parts[2] || ''} />
else if (head === 'library') content = <LibraryRoom tab={parts[1] || 'materials'} arg={parts[2] || ''} />
else if (head === 'sources') content = <Sources channel={parts[1] || ''} tab={parts[2] || ''} />
else if (head === 'agent') content = <AgentRoom tab={parts[1] || 'guide'} />
else if (head === 'guide') content = <Guide section={parts[1] || 'start'} focus={parts[2]} />
else content = <Shelf />
return (
<div className={cx('shell', head === 'book' && parts[2] === 'read' && 'reading', head === 'book' && /^chapter_/.test(parts[2] || '') && 'in-chapter')}>
<nav className="rail" aria-label="主导航">
<button className="rail-brand" onClick={() => go('shelf')} title="小说写作台"><Feather size={18} /></button>
<div className="rail-items">
{ROOMS.map(r => {
const Icon = r.icon
return (
<button key={r.id} className={cx('rail-item', room.id === r.id && 'on')} onClick={() => go(r.id)} title={r.label}>
<Icon size={19} strokeWidth={1.7} />
<span>{r.label}</span>
</button>
)
})}
</div>
<div className="rail-foot">
<button className="rail-item" onClick={() => setNight(n => !n)} title={night ? '切换到日间稿纸' : '切换到夜间模式'}>
{night ? <Sun size={18} strokeWidth={1.7} /> : <Moon size={18} strokeWidth={1.7} />}
<span>{night ? '日间' : '夜间'}</span>
</button>
<button className="rail-item" onClick={onLock} title="锁定(清除本机保存的密码)">
<Lock size={17} strokeWidth={1.7} />
<span>锁定</span>
</button>
</div>
</nav>
<main className="stage">{content}</main>
</div>
)
}