import { useState } from 'react' import { useTranslation } from 'react-i18next' import { useAppStore } from '@renderer/stores/useAppStore' import { useBookStore } from '@renderer/stores/useBookStore' import { useTabStore } from '@renderer/stores/useTabStore' import { ipcCall } from '@renderer/lib/ipc-client' export function HomePage(): React.JSX.Element { const { t } = useTranslation() const showToast = useAppStore((s) => s.showToast) const setView = useAppStore((s) => s.setView) const { books, loadBooks, openBook } = useBookStore() const { openBookTab, openSettingsTab } = useTabStore() const [showDialog, setShowDialog] = useState(false) const [name, setName] = useState('') const [category, setCategory] = useState('玄幻') const [target, setTarget] = useState('') const handleOpenBook = async (bookId: string, bookName: string): Promise => { await openBook(bookId) openBookTab(bookId, bookName) setView('editor') } const handleCreate = async (): Promise => { if (!name.trim()) { showToast(t('toast.enterBookName')) return } const meta = await ipcCall(() => window.electronAPI.book.create({ name: name.trim(), category, targetWordCount: target ? Number(target) : null }) ) setShowDialog(false) setName('') await loadBooks() showToast(t('toast.bookCreated')) await handleOpenBook(meta.id, meta.name) } return (

✒ {t('app.name')}

{t('app.tagline')}

📚 {t('home.myBooks')}{' '} {t('home.bookCount', { count: books.length })}

{books.map((book) => (
void handleOpenBook(book.id, book.name)} onKeyDown={(e) => e.key === 'Enter' && void handleOpenBook(book.id, book.name)} role="button" tabIndex={0} >
{book.name}
{book.category}
{new Date(book.lastOpenedAt).toLocaleDateString()}
))}
setShowDialog(true)} role="button" tabIndex={0} > + {t('home.newBook')}
{showDialog && (

{t('dialog.newBook.title')}

setName(e.target.value)} autoFocus />
setTarget(e.target.value)} />
)}
) }