mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-02 04:16:38 +08:00
fix full library display and cloud search visibility
This commit is contained in:
+16
-1
@@ -8,6 +8,13 @@ export interface MediaPage {
|
||||
page_size: number
|
||||
}
|
||||
|
||||
export interface MediaSearchPage {
|
||||
items: Media[]
|
||||
total?: number
|
||||
page?: number
|
||||
page_size?: number
|
||||
}
|
||||
|
||||
export const libraryAPI = {
|
||||
list: (options?: { includeHidden?: boolean }) =>
|
||||
api
|
||||
@@ -38,7 +45,15 @@ export const libraryAPI = {
|
||||
|
||||
export const mediaAPI = {
|
||||
search: (q: string, limit = 50) =>
|
||||
api.get<{ items: Media[] }>('/media', { params: { q, limit } }).then((r) => r.data),
|
||||
api.get<MediaSearchPage>('/media', { params: { q, limit } }).then((r) => r.data),
|
||||
|
||||
searchPage: (q: string, page = 1, pageSize = 50) =>
|
||||
api
|
||||
.get<MediaSearchPage>('/media', {
|
||||
params: { q, page, page_size: pageSize },
|
||||
timeout: LONG_REQUEST_TIMEOUT,
|
||||
})
|
||||
.then((r) => r.data),
|
||||
|
||||
get: (id: string) => api.get<Media>(`/media/${id}`).then((r) => r.data),
|
||||
}
|
||||
|
||||
@@ -2,15 +2,18 @@ import { useCallback } from 'react'
|
||||
import toast from 'react-hot-toast'
|
||||
|
||||
import { useWebSocket } from '../hooks/useWebSocket'
|
||||
import { useAuthStore } from '../stores/auth'
|
||||
|
||||
// GlobalEvents subscribes to the WS hub and surfaces interesting events
|
||||
// as toasts. Lives at the top of the component tree so every page sees
|
||||
// the same stream without re-opening connections.
|
||||
export function GlobalEvents() {
|
||||
const role = useAuthStore((state) => state.user?.role)
|
||||
const onEvent = useCallback((topic: string, payload: unknown) => {
|
||||
if (!payload || typeof payload !== 'object') return
|
||||
const p = payload as Record<string, unknown>
|
||||
if (topic === 'scan') {
|
||||
if (role !== 'admin') return
|
||||
const id = `scan-${String(p.library_id ?? 'global')}`
|
||||
if (p.error) {
|
||||
toast.error(`扫描失败:${String(p.error)}`, { id })
|
||||
@@ -40,7 +43,7 @@ export function GlobalEvents() {
|
||||
const queued = (p.queued as number | undefined) ?? 0
|
||||
if (queued > 0) toast.success(`订阅「${p.name}」已加入 ${queued} 项下载`)
|
||||
}
|
||||
}, [])
|
||||
}, [role])
|
||||
|
||||
useWebSocket(onEvent)
|
||||
return null
|
||||
|
||||
@@ -13,8 +13,6 @@ import { useAuthStore } from '../stores/auth'
|
||||
import { getSeriesKey, groupSeries, isEpisodeLike, seriesTitle, type SeriesCard } from '../utils/groupSeries'
|
||||
import { useWebSocket } from '../hooks/useWebSocket'
|
||||
|
||||
const MAX_LIBRARY_ITEMS_IN_BROWSER = 3_000
|
||||
|
||||
export function LibraryPage() {
|
||||
const { id = '' } = useParams()
|
||||
const [searchParams, setSearchParams] = useSearchParams()
|
||||
@@ -24,6 +22,7 @@ export function LibraryPage() {
|
||||
const [items, setItems] = useState<Media[]>([])
|
||||
const [total, setTotal] = useState(0)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [loadingAll, setLoadingAll] = useState(false)
|
||||
const [scanning, setScanning] = useState(false)
|
||||
const [scanProgress, setScanProgress] = useState('')
|
||||
const [scraping, setScraping] = useState(false)
|
||||
@@ -77,12 +76,12 @@ export function LibraryPage() {
|
||||
if (!id || !library) return
|
||||
let cancelled = false
|
||||
setLoading(true)
|
||||
setLoadingAll(true)
|
||||
setItems([])
|
||||
const loadAll = async () => {
|
||||
const pageSize = 500
|
||||
const pageSize = 2000
|
||||
let page = 1
|
||||
let collected: Media[] = []
|
||||
let warnedLargeLibrary = false
|
||||
try {
|
||||
for (;;) {
|
||||
const d = await libraryAPI.listMedia(id, page, pageSize)
|
||||
@@ -90,18 +89,15 @@ export function LibraryPage() {
|
||||
collected = collected.concat(d.items)
|
||||
setItems(collected)
|
||||
setTotal(d.total)
|
||||
if (page === 1) setLoading(false)
|
||||
if (collected.length >= d.total || d.items.length < pageSize) break
|
||||
if (collected.length >= MAX_LIBRARY_ITEMS_IN_BROWSER) {
|
||||
if (!warnedLargeLibrary) {
|
||||
warnedLargeLibrary = true
|
||||
toast(`媒体库条目较多,已先加载前 ${MAX_LIBRARY_ITEMS_IN_BROWSER} 条,避免浏览器卡死。请使用搜索或更细的媒体库目录浏览。`)
|
||||
}
|
||||
break
|
||||
}
|
||||
page += 1
|
||||
}
|
||||
} finally {
|
||||
if (!cancelled) setLoading(false)
|
||||
if (!cancelled) {
|
||||
setLoading(false)
|
||||
setLoadingAll(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
loadAll().catch(() => {
|
||||
@@ -118,6 +114,7 @@ export function LibraryPage() {
|
||||
}, [])
|
||||
|
||||
const onRealtimeEvent = useCallback((topic: string, payload: unknown) => {
|
||||
if (role !== 'admin') return
|
||||
if (topic !== 'scan' || !payload || typeof payload !== 'object') return
|
||||
const p = payload as Record<string, unknown>
|
||||
if (p.library_id !== id) return
|
||||
@@ -146,7 +143,7 @@ export function LibraryPage() {
|
||||
setScanning(true)
|
||||
setScanProgress(`${stage}:目录 ${p.dirs ?? 0} · 已发现 ${p.discovered ?? 0} · 已入库 ${p.visited ?? 0}${speedText}`)
|
||||
}
|
||||
}, [id, reloadCurrentLibrary])
|
||||
}, [id, reloadCurrentLibrary, role])
|
||||
|
||||
useWebSocket(onRealtimeEvent)
|
||||
|
||||
@@ -249,6 +246,9 @@ export function LibraryPage() {
|
||||
<span className="text-sand-500"> ({isSeries ? seriesCards.length : total})</span>
|
||||
</h1>
|
||||
{library && <p className="text-sm text-ink-50">{library.type} · {library.path}</p>}
|
||||
{loadingAll && !loading && total > items.length && (
|
||||
<p className="mt-1 text-xs text-sand-500">正在继续加载全部条目:{items.length} / {total}</p>
|
||||
)}
|
||||
{scanProgress && <p className="mt-1 text-xs text-brand-500">{scanProgress}</p>}
|
||||
</div>
|
||||
{role === 'admin' && (
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ChangeEvent, FormEvent, useCallback, useEffect, useMemo, useState } from 'react'
|
||||
import { ChangeEvent, FormEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import toast from 'react-hot-toast'
|
||||
import { CheckCircle2, Info, Rss, Sparkles } from 'lucide-react'
|
||||
|
||||
@@ -10,6 +10,8 @@ import { MediaCard } from '../components/MediaCard'
|
||||
import type { Media } from '../types'
|
||||
import { groupSeries, seriesCardLink } from '../utils/groupSeries'
|
||||
|
||||
const LOCAL_SEARCH_PAGE_SIZE = 2000
|
||||
|
||||
export function SearchPage() {
|
||||
const [q, setQ] = useState('')
|
||||
const [items, setItems] = useState<Media[]>([])
|
||||
@@ -21,6 +23,8 @@ export function SearchPage() {
|
||||
const [hasSearched, setHasSearched] = useState(false)
|
||||
const [externalItems, setExternalItems] = useState<ExternalMediaResult[]>([])
|
||||
const [subscribing, setSubscribing] = useState('')
|
||||
const [searchTotal, setSearchTotal] = useState(0)
|
||||
const searchSeq = useRef(0)
|
||||
const localCards = useMemo(() => groupSeries(items), [items])
|
||||
|
||||
useEffect(() => {
|
||||
@@ -31,29 +35,45 @@ export function SearchPage() {
|
||||
}, [])
|
||||
|
||||
const doQuickSearch = useCallback((query: string) => {
|
||||
const seq = ++searchSeq.current
|
||||
if (!query.trim()) {
|
||||
setItems([])
|
||||
setSearchTotal(0)
|
||||
setHasSearched(false)
|
||||
setLoading(false)
|
||||
return
|
||||
}
|
||||
setHasSearched(true)
|
||||
setError('')
|
||||
mediaAPI
|
||||
.search(query, 60)
|
||||
.then((d) => {
|
||||
setItems(d.items ?? [])
|
||||
setExternalItems([])
|
||||
setIntent(null)
|
||||
})
|
||||
const loadAll = async () => {
|
||||
let page = 1
|
||||
let collected: Media[] = []
|
||||
for (;;) {
|
||||
const d = await mediaAPI.searchPage(query, page, LOCAL_SEARCH_PAGE_SIZE)
|
||||
if (seq !== searchSeq.current) return
|
||||
const pageItems = d.items ?? []
|
||||
collected = collected.concat(pageItems)
|
||||
const total = d.total ?? collected.length
|
||||
setItems(collected)
|
||||
setSearchTotal(total)
|
||||
if (collected.length >= total || pageItems.length < LOCAL_SEARCH_PAGE_SIZE) break
|
||||
page += 1
|
||||
}
|
||||
setExternalItems([])
|
||||
setIntent(null)
|
||||
}
|
||||
loadAll()
|
||||
.catch((err) => {
|
||||
if (seq !== searchSeq.current) return
|
||||
const msg =
|
||||
(err as { response?: { data?: { error?: string } } })?.response?.data?.error ??
|
||||
'搜索失败'
|
||||
setError(msg)
|
||||
toast.error(msg)
|
||||
})
|
||||
.finally(() => setLoading(false))
|
||||
.finally(() => {
|
||||
if (seq === searchSeq.current) setLoading(false)
|
||||
})
|
||||
}, [])
|
||||
|
||||
// Fast LIKE search-as-you-type when AI mode is OFF.
|
||||
@@ -67,12 +87,14 @@ export function SearchPage() {
|
||||
const onAISubmit = async (e: FormEvent) => {
|
||||
e.preventDefault()
|
||||
if (!q.trim()) return
|
||||
++searchSeq.current
|
||||
setLoading(true)
|
||||
setError('')
|
||||
setHasSearched(true)
|
||||
try {
|
||||
const data = await aiAPI.smartSearch(q)
|
||||
setItems(data.items ?? [])
|
||||
setSearchTotal((data.items ?? []).length)
|
||||
setExternalItems(data.external_items ?? [])
|
||||
setIntent(data.intent)
|
||||
} catch (err) {
|
||||
@@ -166,6 +188,7 @@ export function SearchPage() {
|
||||
<>
|
||||
<div className="text-sm font-semibold text-ink-100">
|
||||
本地媒体库 · {localCards.length} 个合集 / {items.length} 个条目
|
||||
{loading && searchTotal > items.length ? ` · 正在加载全部结果 ${items.length}/${searchTotal}` : ''}
|
||||
</div>
|
||||
<div className="grid grid-cols-2 gap-4 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-6">
|
||||
{localCards.map((card) => (
|
||||
|
||||
Reference in New Issue
Block a user