File size: 6,317 Bytes
3d4293e
0399aa9
3d4293e
 
 
 
 
 
 
 
8214b38
5f674d2
0399aa9
3d4293e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbe6540
3d4293e
0399aa9
3d4293e
5f674d2
8214b38
3d4293e
 
 
 
 
 
 
 
 
 
 
 
 
fbe6540
3d4293e
0399aa9
3d4293e
 
 
 
00385f7
5f674d2
8214b38
3d4293e
 
0399aa9
a7fab52
3d4293e
 
 
25b6182
3d4293e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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>
  )
}