From bcaa56019f0dc440faafa260a6d598dea333cecc Mon Sep 17 00:00:00 2001 From: everpcpc Date: Sat, 28 Nov 2026 14:21:57 +0800 Subject: [PATCH] feat(webui): replace search tabs with per-category result pages --- webui/src/lib/i18n/locales/de/search.json | 3 +- webui/src/lib/i18n/locales/en/search.json | 3 +- webui/src/lib/i18n/locales/es/search.json | 3 +- webui/src/lib/i18n/locales/fr/search.json | 3 +- webui/src/lib/i18n/locales/it/search.json | 3 +- webui/src/lib/i18n/locales/ja/search.json | 3 +- webui/src/lib/i18n/locales/ko/search.json | 3 +- webui/src/lib/i18n/locales/ru/search.json | 3 +- .../src/lib/i18n/locales/zh-Hans/search.json | 3 +- .../src/lib/i18n/locales/zh-Hant/search.json | 3 +- webui/src/router.tsx | 3 +- webui/src/routes/search.tsx | 164 +++++++++--------- 12 files changed, 90 insertions(+), 107 deletions(-) diff --git a/webui/src/lib/i18n/locales/de/search.json b/webui/src/lib/i18n/locales/de/search.json index 8a41947b7..58afd95aa 100644 --- a/webui/src/lib/i18n/locales/de/search.json +++ b/webui/src/lib/i18n/locales/de/search.json @@ -7,8 +7,7 @@ "library": "Bibliothek", "ariaLabel": "Suchbereich: {{label}}" }, - "tabs": { - "all": "Alle", + "categories": { "series": "Serien", "books": "Bücher", "collections": "Sammlungen", diff --git a/webui/src/lib/i18n/locales/en/search.json b/webui/src/lib/i18n/locales/en/search.json index caf337933..61e9edc6d 100644 --- a/webui/src/lib/i18n/locales/en/search.json +++ b/webui/src/lib/i18n/locales/en/search.json @@ -7,8 +7,7 @@ "library": "Library", "ariaLabel": "Search scope: {{label}}" }, - "tabs": { - "all": "All", + "categories": { "series": "Series", "books": "Books", "collections": "Collections", diff --git a/webui/src/lib/i18n/locales/es/search.json b/webui/src/lib/i18n/locales/es/search.json index 42eab0884..db8671f6b 100644 --- a/webui/src/lib/i18n/locales/es/search.json +++ b/webui/src/lib/i18n/locales/es/search.json @@ -7,8 +7,7 @@ "library": "Biblioteca", "ariaLabel": "Ámbito de búsqueda: {{label}}" }, - "tabs": { - "all": "Todo", + "categories": { "series": "Series", "books": "Libros", "collections": "Colecciones", diff --git a/webui/src/lib/i18n/locales/fr/search.json b/webui/src/lib/i18n/locales/fr/search.json index 609a135ad..df0d15195 100644 --- a/webui/src/lib/i18n/locales/fr/search.json +++ b/webui/src/lib/i18n/locales/fr/search.json @@ -7,8 +7,7 @@ "library": "Bibliothèque", "ariaLabel": "Périmètre de recherche : {{label}}" }, - "tabs": { - "all": "Tout", + "categories": { "series": "Séries", "books": "Livres", "collections": "Collections", diff --git a/webui/src/lib/i18n/locales/it/search.json b/webui/src/lib/i18n/locales/it/search.json index 0782ff459..1e61c35c8 100644 --- a/webui/src/lib/i18n/locales/it/search.json +++ b/webui/src/lib/i18n/locales/it/search.json @@ -7,8 +7,7 @@ "library": "Libreria", "ariaLabel": "Ambito di ricerca: {{label}}" }, - "tabs": { - "all": "Tutto", + "categories": { "series": "Serie", "books": "Libri", "collections": "Raccolte", diff --git a/webui/src/lib/i18n/locales/ja/search.json b/webui/src/lib/i18n/locales/ja/search.json index 8d9458517..e00a5081e 100644 --- a/webui/src/lib/i18n/locales/ja/search.json +++ b/webui/src/lib/i18n/locales/ja/search.json @@ -7,8 +7,7 @@ "library": "ライブラリ", "ariaLabel": "検索範囲: {{label}}" }, - "tabs": { - "all": "すべて", + "categories": { "series": "シリーズ", "books": "本", "collections": "コレクション", diff --git a/webui/src/lib/i18n/locales/ko/search.json b/webui/src/lib/i18n/locales/ko/search.json index 865a5510e..455f7c7ff 100644 --- a/webui/src/lib/i18n/locales/ko/search.json +++ b/webui/src/lib/i18n/locales/ko/search.json @@ -7,8 +7,7 @@ "library": "라이브러리", "ariaLabel": "검색 범위: {{label}}" }, - "tabs": { - "all": "전체", + "categories": { "series": "시리즈", "books": "책", "collections": "컬렉션", diff --git a/webui/src/lib/i18n/locales/ru/search.json b/webui/src/lib/i18n/locales/ru/search.json index 777c5c000..1d9c068e1 100644 --- a/webui/src/lib/i18n/locales/ru/search.json +++ b/webui/src/lib/i18n/locales/ru/search.json @@ -7,8 +7,7 @@ "library": "Библиотека", "ariaLabel": "Область поиска: {{label}}" }, - "tabs": { - "all": "Все", + "categories": { "series": "Серии", "books": "Книги", "collections": "Коллекции", diff --git a/webui/src/lib/i18n/locales/zh-Hans/search.json b/webui/src/lib/i18n/locales/zh-Hans/search.json index fdb8b3e1b..50d665949 100644 --- a/webui/src/lib/i18n/locales/zh-Hans/search.json +++ b/webui/src/lib/i18n/locales/zh-Hans/search.json @@ -7,8 +7,7 @@ "library": "库", "ariaLabel": "搜索范围:{{label}}" }, - "tabs": { - "all": "全部", + "categories": { "series": "系列", "books": "书籍", "collections": "合集", diff --git a/webui/src/lib/i18n/locales/zh-Hant/search.json b/webui/src/lib/i18n/locales/zh-Hant/search.json index 5795da811..b37d46679 100644 --- a/webui/src/lib/i18n/locales/zh-Hant/search.json +++ b/webui/src/lib/i18n/locales/zh-Hant/search.json @@ -7,8 +7,7 @@ "library": "書庫", "ariaLabel": "搜尋範圍:{{label}}" }, - "tabs": { - "all": "全部", + "categories": { "series": "系列", "books": "書籍", "collections": "收藏", diff --git a/webui/src/router.tsx b/webui/src/router.tsx index 22e123206..5483374f3 100644 --- a/webui/src/router.tsx +++ b/webui/src/router.tsx @@ -22,7 +22,7 @@ import { CollectionDetailPage } from '@/routes/detail/collection' import { ReadListDetailPage } from '@/routes/detail/readlist' import { SmartListDetailPage } from '@/routes/detail/smartlist' import { OneshotDetailPage } from '@/routes/detail/oneshot' -import { SearchPage } from '@/routes/search' +import { SearchCategoryPage, SearchPage } from '@/routes/search' import { AccountProfilePage } from '@/routes/account/profile' import { AccountSecurityPage } from '@/routes/account/security' import { AccountApiKeysPage } from '@/routes/account/api-keys' @@ -82,6 +82,7 @@ export const router = createBrowserRouter([ { path: '/oneshot/:seriesId', element: }, { path: '/book/:bookId', element: }, { path: '/search', element: }, + { path: '/search/:category', element: }, { path: '/account', element: }, { path: '/account/profile', element: }, { path: '/account/security', element: }, diff --git a/webui/src/routes/search.tsx b/webui/src/routes/search.tsx index f5e11cc04..18a1a8ae5 100644 --- a/webui/src/routes/search.tsx +++ b/webui/src/routes/search.tsx @@ -1,5 +1,5 @@ -import { Fragment, useEffect, useMemo, useRef, type ReactNode } from 'react' -import { Link, useSearchParams } from 'react-router-dom' +import { Fragment, useCallback, useEffect, useMemo, type ReactNode } from 'react' +import { Link, Navigate, useParams, useSearchParams } from 'react-router-dom' import { useInfiniteQuery, useQuery } from '@tanstack/react-query' import { useTranslation } from 'react-i18next' import { CaretRight, CircleNotch, MagnifyingGlass, WarningCircle } from '@phosphor-icons/react' @@ -10,10 +10,10 @@ import { seriesApi } from '@/lib/api/series' import type { Page } from '@/lib/api/types' import { useDocumentTitle } from '@/lib/hooks/useDocumentTitle' import { useDensityCardWidth } from '@/lib/store/ui' +import { BackButton, HistoryBackButton } from '@/components/ui/BackButton' import { Button } from '@/components/ui/Button' import { EmptyState } from '@/components/ui/EmptyState' -import { HistoryBackButton } from '@/components/ui/BackButton' -import { SegmentedControl } from '@/components/ui/SegmentedControl' +import { PageHeader } from '@/components/ui/PageHeader' import { CardSkeleton, GridSkeleton, Skeleton } from '@/components/ui/Skeleton' import { BookCard } from '@/components/media/BookCard' import { HorizontalRow } from '@/components/media/HorizontalRow' @@ -24,14 +24,15 @@ import { SelectionModeButton } from '@/components/selection/SelectionModeButton' import { SeriesSelectionBar } from '@/components/browse/SeriesSelectionBar' import { BooksSelectionBar } from '@/components/browse/BooksSelectionBar' import { libraryScopeCondition } from '@/components/filters/builders' -import { scopeKey, scopeLibraryIds, useSearchScope, type SearchScope } from '@/components/search/scope' +import { Sentinel } from '@/components/filters/Sentinel' +import { scopeKey, scopeLibraryIds, scopeValue, useSearchScope, type SearchScope } from '@/components/search/scope' -const TABS = ['all', 'series', 'books', 'collections', 'readlists'] as const +const CATEGORIES = ['series', 'books', 'collections', 'readlists'] as const -type SearchTab = (typeof TABS)[number] +type SearchCategory = (typeof CATEGORIES)[number] -function parseTab(raw: string | null): SearchTab { - return TABS.some((tab) => tab === raw) ? (raw as SearchTab) : 'all' +function parseCategory(raw: string | null | undefined): SearchCategory | undefined { + return CATEGORIES.some((category) => category === raw) ? (raw as SearchCategory) : undefined } function scopedSearch(q: string, libraryIds: string[] | undefined) { @@ -39,67 +40,81 @@ function scopedSearch(q: string, libraryIds: string[] | undefined) { return { fullTextSearch: q, ...(condition && { condition }) } } +function categorySearchUrl(category: SearchCategory, q: string, scope: SearchScope) { + const params = new URLSearchParams({ q }) + const scopeParam = scopeValue(scope) + if (scopeParam !== 'all') params.set('scope', scopeParam) + return `/search/${category}?${params}` +} + export function SearchPage() { const { t } = useTranslation('search') - const [searchParams, setSearchParams] = useSearchParams() - const qRaw = searchParams.get('q') ?? '' - const q = qRaw.trim() - const tab = parseTab(searchParams.get('tab')) + const [searchParams] = useSearchParams() + const q = (searchParams.get('q') ?? '').trim() + const { scope } = useSearchScope() + + useDocumentTitle(q ? t('titleWithQuery', { q }) : t('title')) + + return ( +
+ + {!q ? ( + } + title={t('emptyTitle')} + body={t('emptyBody')} + /> + ) : ( + + )} +
+ ) +} + +export function SearchCategoryPage() { + const { t } = useTranslation('search') + const [searchParams] = useSearchParams() + const category = parseCategory(useParams().category) + const q = (searchParams.get('q') ?? '').trim() const { scope } = useSearchScope() const libraryIds = scopeLibraryIds(scope) const sk = scopeKey(scope) - useDocumentTitle(q ? t('titleWithQuery', { q }) : t('title')) + useDocumentTitle(category ? (q ? t('titleWithQuery', { q }) : t(`categories.${category}`)) : undefined) const seriesSel = useSelection() const booksSel = useSelection() + const activeSel = category === 'books' ? booksSel : seriesSel const clearSeriesSel = seriesSel.clear const clearBooksSel = booksSel.clear - // results are replaced wholesale when the query, tab or scope changes; stale selections would point at hidden items + // results are replaced wholesale when the query, category or scope changes; stale selections would point at hidden items useEffect(() => { clearSeriesSel() clearBooksSel() - }, [q, tab, sk, clearSeriesSel, clearBooksSel]) + }, [q, category, sk, clearSeriesSel, clearBooksSel]) - const onTabChange = (value: SearchTab) => { - setSearchParams( - (prev) => { - const next = new URLSearchParams(prev) - next.set('tab', value) - return next - }, - { replace: true }, - ) - } + const qs = searchParams.toString() + const backTo = qs ? `/search?${qs}` : '/search' + if (!category) return return (
- -
-
- - options={TABS.map((value) => ({ value, label: t(`tabs.${value}`) }))} - value={tab} - onChange={onTabChange} - /> -
- {(tab === 'series' || tab === 'books') && ( - - )} -
- + + + ) : undefined + } + /> {!q ? ( } title={t('emptyTitle')} body={t('emptyBody')} /> - ) : tab === 'all' ? ( - - ) : tab === 'series' ? ( + ) : category === 'series' ? ( } selectionBar={(items) => } /> - ) : tab === 'books' ? ( + ) : category === 'books' ? ( } selectionBar={(items) => b.id)} />} /> - ) : tab === 'collections' ? ( + ) : category === 'collections' ? ( seriesApi.list({ search: scopedSearch(q, libraryIds), size: 8 }), @@ -176,34 +190,26 @@ function AllResults({ q, scope }: { q: string; scope: SearchScope }) { transition={{ duration: 0.3, ease: 'easeOut' }} > } /> } /> } /> } /> @@ -213,16 +219,12 @@ function AllResults({ q, scope }: { q: string; scope: SearchScope }) { function PreviewRow({ title, - tab, - q, - scopeParam, + to, page, renderCard, }: { title: string - tab: SearchTab - q: string - scopeParam: string + to: string page?: Page renderCard: (item: T) => ReactNode }) { @@ -230,7 +232,7 @@ function PreviewRow({ const width = useDensityCardWidth() if (!page || page.totalElements === 0) return null return ( - + {page.content.map((item) => (
{renderCard(item)} @@ -238,7 +240,7 @@ function PreviewRow({ ))} {page.totalElements > page.content.length && ( @@ -273,18 +275,8 @@ function CategoryGrid({ getNextPageParam: (last) => (last.last ? undefined : last.number + 1), }) - const sentinelRef = useRef(null) - useEffect(() => { - const el = sentinelRef.current - if (!el) return - const io = new IntersectionObserver( - (entries) => { - if (entries.some((e) => e.isIntersecting) && hasNextPage && !isFetchingNextPage) void fetchNextPage() - }, - { rootMargin: '800px' }, - ) - io.observe(el) - return () => io.disconnect() + const loadMore = useCallback(() => { + if (hasNextPage && !isFetchingNextPage) void fetchNextPage() }, [hasNextPage, isFetchingNextPage, fetchNextPage]) const items = useMemo(() => data?.pages.flatMap((p) => p.content) ?? [], [data?.pages]) @@ -307,7 +299,7 @@ function CategoryGrid({
)} -
+ ) }