mirror of
https://github.com/truewhile/MeBox.git
synced 2026-11-16 12:54:22 +08:00
优化
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { useNavigate, useSearchParams } from 'react-router-dom'
|
||||
import toast from 'react-hot-toast'
|
||||
import { ArrowLeft, BookOpen, ChevronDown, ChevronUp, Loader2, RefreshCw, Trash2 } from 'lucide-react'
|
||||
import { ArrowLeft, ArrowLeftRight, BookOpen, ChevronDown, ChevronUp, Loader2, RefreshCw, Trash2 } from 'lucide-react'
|
||||
|
||||
import { readerAPI, type ReaderBook, type ReaderBookInfo, type ReaderTocChapter } from '../../api/reader'
|
||||
import { readerAPI, type ReaderBook, type ReaderBookInfo, type ReaderSearchOrigin, type ReaderTocChapter } from '../../api/reader'
|
||||
import ReaderBookCover from '../../components/ReaderBookCover'
|
||||
import { SourcePickerDialog } from './SourcePickerDialog'
|
||||
|
||||
// 书籍详情页(仿 legado BookInfoActivity:封面 + 信息 + 简介 + 目录入口 + 加书架/开始阅读)。
|
||||
|
||||
@@ -24,6 +26,10 @@ export default function ReaderBookPage() {
|
||||
const [busy, setBusy] = useState(false)
|
||||
const [tocExpanded, setTocExpanded] = useState(false)
|
||||
const [introExpanded, setIntroExpanded] = useState(false)
|
||||
// 换源:候选源来自按书名重新搜索的结果
|
||||
const [pickerOpen, setPickerOpen] = useState(false)
|
||||
const [pickLoading, setPickLoading] = useState(false)
|
||||
const [candidates, setCandidates] = useState<ReaderSearchOrigin[]>([])
|
||||
|
||||
const loadInfo = () => {
|
||||
if (!bookURL) return
|
||||
@@ -120,6 +126,66 @@ export default function ReaderBookPage() {
|
||||
}
|
||||
}
|
||||
|
||||
// ── 换源 ──
|
||||
|
||||
// 换源候选:按书名重新搜索,取同名(作者一致优先)那本书上的所有源。
|
||||
const openSourcePicker = async () => {
|
||||
const key = (info?.name || qName).trim()
|
||||
if (!key) {
|
||||
toast.error('缺少书名,无法换源')
|
||||
return
|
||||
}
|
||||
setPickerOpen(true)
|
||||
setPickLoading(true)
|
||||
setCandidates([])
|
||||
try {
|
||||
const res = await readerAPI.search(key)
|
||||
const list = res.books ?? []
|
||||
const wantAuthor = (info?.author || qAuthor).trim()
|
||||
const hit =
|
||||
list.find((b) => b.name === key && wantAuthor !== '' && b.author === wantAuthor) ??
|
||||
list.find((b) => b.name === key) ??
|
||||
null
|
||||
setCandidates(hit?.origins ?? [])
|
||||
} catch (e) {
|
||||
toast.error((e as { response?: { data?: { error?: string } } })?.response?.data?.error ?? '搜索书源失败')
|
||||
} finally {
|
||||
setPickLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
const bookPath = (origin: ReaderSearchOrigin) =>
|
||||
`/reader/book?${new URLSearchParams({
|
||||
source_url: origin.origin,
|
||||
book_url: origin.book_url,
|
||||
name,
|
||||
author,
|
||||
cover_url: cover,
|
||||
origin_id: origin.source_id,
|
||||
origin_name: origin.origin_name,
|
||||
}).toString()}`
|
||||
|
||||
// 已在书架:调用换源接口(服务端保留进度、清空旧源目录缓存);
|
||||
// 未在书架:这本书还没绑定书源,直接按所选源打开详情页即可。
|
||||
const applyOrigin = async (origin: ReaderSearchOrigin) => {
|
||||
setPickerOpen(false)
|
||||
if (!shelfBook) {
|
||||
navigate(bookPath(origin))
|
||||
return
|
||||
}
|
||||
setBusy(true)
|
||||
try {
|
||||
await readerAPI.switchOrigin(shelfBook.id, origin)
|
||||
toast.success(`已切换到「${origin.origin_name || origin.origin}」`)
|
||||
// URL 上的旧源地址已失效,替换成新源地址,页面据此重新拉详情与目录
|
||||
navigate(bookPath(origin), { replace: true })
|
||||
} catch (e) {
|
||||
toast.error((e as { response?: { data?: { error?: string } } })?.response?.data?.error ?? '换源失败')
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
if (!bookURL) {
|
||||
return (
|
||||
<div className="mx-auto px-6 py-24 text-center text-sm text-[var(--app-muted)]">缺少书籍参数</div>
|
||||
@@ -151,13 +217,7 @@ export default function ReaderBookPage() {
|
||||
{/* 信息区(仿 legado:封面 + 书名/作者/最新章节/简介) */}
|
||||
<div className="mt-6 flex gap-5">
|
||||
<div className="h-40 w-28 shrink-0 overflow-hidden rounded-xl border border-[var(--app-border)] bg-[var(--app-panel-soft)]">
|
||||
{cover ? (
|
||||
<img src={cover} alt={name} referrerPolicy="no-referrer" className="h-full w-full object-cover" />
|
||||
) : (
|
||||
<div className="flex h-full w-full items-center justify-center">
|
||||
<BookOpen size={24} className="text-[var(--app-muted)]" />
|
||||
</div>
|
||||
)}
|
||||
<ReaderBookCover url={cover} alt={name} iconSize={24} />
|
||||
</div>
|
||||
<div className="min-w-0 flex-1">
|
||||
<h1 className="font-display text-xl text-ink-600">{name || '未知书名'}</h1>
|
||||
@@ -209,6 +269,15 @@ export default function ReaderBookPage() {
|
||||
{busy ? <Loader2 size={13} className="mr-1 inline animate-spin" /> : null}
|
||||
{shelfBook?.dur_chapter_title ? '继续阅读' : '开始阅读'}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={openSourcePicker}
|
||||
disabled={busy}
|
||||
className="btn-outline shrink-0 px-3 text-xs disabled:opacity-50"
|
||||
title="换源"
|
||||
>
|
||||
<ArrowLeftRight size={13} className="mr-1 inline" /> 换源
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 目录 */}
|
||||
@@ -245,6 +314,18 @@ export default function ReaderBookPage() {
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{pickerOpen && (
|
||||
<SourcePickerDialog
|
||||
title={name || '这本书'}
|
||||
origins={candidates}
|
||||
current={{ originURL: sourceURL, bookURL }}
|
||||
loading={pickLoading}
|
||||
emptyHint="按书名重搜后没有找到其它书源"
|
||||
onPick={applyOrigin}
|
||||
onClose={() => setPickerOpen(false)}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user