feat: add search result filter

This commit is contained in:
shinya
2025-08-14 13:13:22 +08:00
parent 8b2e8368b7
commit 95ae4645fd
4 changed files with 420 additions and 67 deletions

View File

@@ -70,6 +70,7 @@ async function refreshConfig() {
throw new Error('配置文件格式错误,请检查 JSON 语法');
}
config.ConfigFile = decodedContent;
config.ConfigSubscribtion.LastCheck = new Date().toISOString();
config = refineConfig(config);
const storage = getStorage();
if (storage && typeof (storage as any).setAdminConfig === 'function') {

View File

@@ -18,6 +18,7 @@ import { yellowWords } from '@/lib/yellow';
import PageLayout from '@/components/PageLayout';
import SearchSuggestions from '@/components/SearchSuggestions';
import VideoCard from '@/components/VideoCard';
import SearchResultFilter, { SearchFilterCategory } from '@/components/SearchResultFilter';
function SearchPageClient() {
// 搜索历史
@@ -32,6 +33,19 @@ function SearchPageClient() {
const [showResults, setShowResults] = useState(false);
const [searchResults, setSearchResults] = useState<SearchResult[]>([]);
const [showSuggestions, setShowSuggestions] = useState(false);
// 过滤器:非聚合与聚合
const [filterAll, setFilterAll] = useState<{ source: string; title: string; year: string; yearOrder: 'asc' | 'desc' }>({
source: 'all',
title: 'all',
year: 'all',
yearOrder: 'desc',
});
const [filterAgg, setFilterAgg] = useState<{ source: string; title: string; year: string; yearOrder: 'asc' | 'desc' }>({
source: 'all',
title: 'all',
year: 'all',
yearOrder: 'desc',
});
// 获取默认聚合设置:只读取用户本地设置,默认为 true
const getDefaultAggregate = () => {
@@ -53,9 +67,8 @@ function SearchPageClient() {
const map = new Map<string, SearchResult[]>();
searchResults.forEach((item) => {
// 使用 title + year + type 作为键year 必然存在,但依然兜底 'unknown'
const key = `${item.title.replaceAll(' ', '')}-${
item.year || 'unknown'
}-${item.episodes.length === 1 ? 'movie' : 'tv'}`;
const key = `${item.title.replaceAll(' ', '')}-${item.year || 'unknown'
}-${item.episodes.length === 1 ? 'movie' : 'tv'}`;
const arr = map.get(key) || [];
arr.push(item);
map.set(key, arr);
@@ -94,6 +107,114 @@ function SearchPageClient() {
});
}, [searchResults]);
// 构建筛选选项
const filterOptions = useMemo(() => {
const sourcesSet = new Map<string, string>();
const titlesSet = new Set<string>();
const yearsSet = new Set<string>();
searchResults.forEach((item) => {
if (item.source && item.source_name) {
sourcesSet.set(item.source, item.source_name);
}
if (item.title) titlesSet.add(item.title);
if (item.year) yearsSet.add(item.year);
});
const sourceOptions: { label: string; value: string }[] = [
{ label: '全部来源', value: 'all' },
...Array.from(sourcesSet.entries())
.sort((a, b) => a[1].localeCompare(b[1]))
.map(([value, label]) => ({ label, value })),
];
const titleOptions: { label: string; value: string }[] = [
{ label: '全部标题', value: 'all' },
...Array.from(titlesSet.values())
.sort((a, b) => a.localeCompare(b))
.map((t) => ({ label: t, value: t })),
];
// 年份: 将 unknown 放末尾
const years = Array.from(yearsSet.values());
const knownYears = years.filter((y) => y !== 'unknown').sort((a, b) => parseInt(b) - parseInt(a));
const hasUnknown = years.includes('unknown');
const yearOptions: { label: string; value: string }[] = [
{ label: '全部年份', value: 'all' },
...knownYears.map((y) => ({ label: y, value: y })),
...(hasUnknown ? [{ label: '未知', value: 'unknown' }] : []),
];
const categoriesAll: SearchFilterCategory[] = [
{ key: 'source', label: '来源', options: sourceOptions },
{ key: 'title', label: '标题', options: titleOptions },
{ key: 'year', label: '年份', options: yearOptions },
];
const categoriesAgg: SearchFilterCategory[] = [
{ key: 'source', label: '来源', options: sourceOptions },
{ key: 'title', label: '标题', options: titleOptions },
{ key: 'year', label: '年份', options: yearOptions },
];
return { categoriesAll, categoriesAgg };
}, [searchResults]);
// 年份排序辅助
const compareYear = (aYear: string, bYear: string, order: 'asc' | 'desc') => {
if (aYear === bYear) return 0;
if (aYear === 'unknown') return 1;
if (bYear === 'unknown') return -1;
const diff = parseInt(aYear) - parseInt(bYear);
return order === 'asc' ? diff : -diff;
};
// 非聚合:应用筛选与排序
const filteredAllResults = useMemo(() => {
const { source, title, year, yearOrder } = filterAll;
const filtered = searchResults.filter((item) => {
if (source !== 'all' && item.source !== source) return false;
if (title !== 'all' && item.title !== title) return false;
if (year !== 'all' && item.year !== year) return false;
return true;
});
// 仍保持“精确标题优先”的二级排序
return filtered.sort((a, b) => {
const yearComp = compareYear(a.year, b.year, yearOrder);
if (yearComp !== 0) return yearComp;
const aExactMatch = a.title === searchQuery.trim();
const bExactMatch = b.title === searchQuery.trim();
if (aExactMatch && !bExactMatch) return -1;
if (!aExactMatch && bExactMatch) return 1;
return a.title.localeCompare(b.title);
});
}, [searchResults, filterAll, searchQuery]);
// 聚合:应用筛选与排序
const filteredAggResults = useMemo(() => {
const { source, title, year, yearOrder } = filterAgg as any;
const filtered = aggregatedResults.filter(([_, group]) => {
const gTitle = group[0]?.title ?? '';
const gYear = group[0]?.year ?? 'unknown';
const hasSource = source === 'all' ? true : group.some((item) => item.source === source);
if (!hasSource) return false;
if (title !== 'all' && gTitle !== title) return false;
if (year !== 'all' && gYear !== year) return false;
return true;
});
return filtered.sort((a, b) => {
const aExactMatch = a[1][0].title.replaceAll(' ', '').includes(searchQuery.trim().replaceAll(' ', ''));
const bExactMatch = b[1][0].title.replaceAll(' ', '').includes(searchQuery.trim().replaceAll(' ', ''));
if (aExactMatch && !bExactMatch) return -1;
if (!aExactMatch && bExactMatch) return 1;
const aYear = a[1][0].year;
const bYear = b[1][0].year;
const yearComp = compareYear(aYear, bYear, yearOrder);
if (yearComp !== 0) return yearComp;
return a[0].localeCompare(b[0]);
});
}, [aggregatedResults, filterAgg, searchQuery]);
useEffect(() => {
// 无搜索参数时聚焦搜索框
!searchParams.get('q') && document.getElementById('searchInput')?.focus();
@@ -318,24 +439,36 @@ function SearchPageClient() {
</div>
) : showResults ? (
<section className='mb-12'>
{/* 标题 + 聚合开关 */}
<div className='mb-8 flex items-center justify-between'>
<h2 className='text-xl font-bold text-gray-800 dark:text-gray-200'>
</h2>
{/* 标题 */}
<div className='mb-4'>
<h2 className='text-xl font-bold text-gray-800 dark:text-gray-200'></h2>
</div>
{/* 筛选器 + 聚合开关 同行 */}
<div className='mb-8 flex items-center justify-between gap-3'>
<div className='flex-1 min-w-0'>
{viewMode === 'agg' ? (
<SearchResultFilter
categories={filterOptions.categoriesAgg}
values={filterAgg}
onChange={(v) => setFilterAgg(v as any)}
/>
) : (
<SearchResultFilter
categories={filterOptions.categoriesAll}
values={filterAll}
onChange={(v) => setFilterAll(v as any)}
/>
)}
</div>
{/* 聚合开关 */}
<label className='flex items-center gap-2 cursor-pointer select-none'>
<span className='text-sm text-gray-700 dark:text-gray-300'>
</span>
<label className='flex items-center gap-2 cursor-pointer select-none shrink-0'>
<span className='text-sm text-gray-700 dark:text-gray-300'></span>
<div className='relative'>
<input
type='checkbox'
className='sr-only peer'
checked={viewMode === 'agg'}
onChange={() =>
setViewMode(viewMode === 'agg' ? 'all' : 'agg')
}
onChange={() => setViewMode(viewMode === 'agg' ? 'all' : 'agg')}
/>
<div className='w-9 h-5 bg-gray-300 rounded-full peer-checked:bg-green-500 transition-colors dark:bg-gray-600'></div>
<div className='absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full transition-transform peer-checked:translate-x-4'></div>
@@ -347,45 +480,45 @@ function SearchPageClient() {
className='justify-start grid grid-cols-3 gap-x-2 gap-y-14 sm:gap-y-20 px-0 sm:px-2 sm:grid-cols-[repeat(auto-fill,_minmax(11rem,_1fr))] sm:gap-x-8'
>
{viewMode === 'agg'
? aggregatedResults.map(([mapKey, group]) => {
return (
<div key={`agg-${mapKey}`} className='w-full'>
<VideoCard
from='search'
items={group}
query={
searchQuery.trim() !== group[0].title
? searchQuery.trim()
: ''
}
/>
</div>
);
})
: searchResults.map((item) => (
<div
key={`all-${item.source}-${item.id}`}
className='w-full'
>
? filteredAggResults.map(([mapKey, group]) => {
return (
<div key={`agg-${mapKey}`} className='w-full'>
<VideoCard
id={item.id}
title={item.title}
poster={item.poster}
episodes={item.episodes.length}
source={item.source}
source_name={item.source_name}
douban_id={item.douban_id}
from='search'
items={group}
query={
searchQuery.trim() !== item.title
searchQuery.trim() !== group[0].title
? searchQuery.trim()
: ''
}
year={item.year}
from='search'
type={item.episodes.length > 1 ? 'tv' : 'movie'}
/>
</div>
))}
);
})
: filteredAllResults.map((item) => (
<div
key={`all-${item.source}-${item.id}`}
className='w-full'
>
<VideoCard
id={item.id}
title={item.title}
poster={item.poster}
episodes={item.episodes.length}
source={item.source}
source_name={item.source_name}
douban_id={item.douban_id}
query={
searchQuery.trim() !== item.title
? searchQuery.trim()
: ''
}
year={item.year}
from='search'
type={item.episodes.length > 1 ? 'tv' : 'movie'}
/>
</div>
))}
{searchResults.length === 0 && (
<div className='col-span-full text-center text-gray-500 py-8 dark:text-gray-400'>
@@ -446,11 +579,10 @@ function SearchPageClient() {
{/* 返回顶部悬浮按钮 */}
<button
onClick={scrollToTop}
className={`fixed bottom-20 md:bottom-6 right-6 z-[500] w-12 h-12 bg-green-500/90 hover:bg-green-500 text-white rounded-full shadow-lg backdrop-blur-sm transition-all duration-300 ease-in-out flex items-center justify-center group ${
showBackToTop
? 'opacity-100 translate-y-0 pointer-events-auto'
: 'opacity-0 translate-y-4 pointer-events-none'
}`}
className={`fixed bottom-20 md:bottom-6 right-6 z-[500] w-12 h-12 bg-green-500/90 hover:bg-green-500 text-white rounded-full shadow-lg backdrop-blur-sm transition-all duration-300 ease-in-out flex items-center justify-center group ${showBackToTop
? 'opacity-100 translate-y-0 pointer-events-auto'
: 'opacity-0 translate-y-4 pointer-events-none'
}`}
aria-label='返回顶部'
>
<ChevronUp className='w-6 h-6 transition-transform group-hover:scale-110' />

View File

@@ -0,0 +1,224 @@
'use client';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import { ArrowDownWideNarrow, ArrowUpNarrowWide } from 'lucide-react';
export type SearchFilterKey = 'source' | 'title' | 'year' | 'yearOrder';
export interface SearchFilterOption {
label: string;
value: string;
}
export interface SearchFilterCategory {
key: SearchFilterKey;
label: string;
options: SearchFilterOption[];
}
interface SearchResultFilterProps {
categories: SearchFilterCategory[];
values: Partial<Record<SearchFilterKey, string>>;
onChange: (values: Record<SearchFilterKey, string>) => void;
}
const DEFAULTS: Record<SearchFilterKey, string> = {
source: 'all',
title: 'all',
year: 'all',
yearOrder: 'desc',
};
const SearchResultFilter: React.FC<SearchResultFilterProps> = ({ categories, values, onChange }) => {
const [activeCategory, setActiveCategory] = useState<SearchFilterKey | null>(null);
const [dropdownPosition, setDropdownPosition] = useState<{ x: number; y: number; width: number }>({ x: 0, y: 0, width: 0 });
const categoryRefs = useRef<Record<string, HTMLDivElement | null>>({});
const dropdownRef = useRef<HTMLDivElement>(null);
const mergedValues = useMemo(() => {
return {
...DEFAULTS,
...values,
} as Record<SearchFilterKey, string>;
}, [values]);
const calculateDropdownPosition = (categoryKey: SearchFilterKey) => {
const element = categoryRefs.current[categoryKey];
if (element) {
const rect = element.getBoundingClientRect();
const viewportWidth = window.innerWidth;
const isMobile = viewportWidth < 768;
let x = rect.left;
let dropdownWidth = Math.max(rect.width, 240);
let useFixedWidth = false;
if (isMobile) {
const padding = 16;
const maxWidth = viewportWidth - padding * 2;
dropdownWidth = Math.min(dropdownWidth, maxWidth);
useFixedWidth = true;
if (x + dropdownWidth > viewportWidth - padding) {
x = viewportWidth - dropdownWidth - padding;
}
if (x < padding) {
x = padding;
}
}
setDropdownPosition({ x, y: rect.bottom, width: useFixedWidth ? dropdownWidth : rect.width });
}
};
const handleCategoryClick = (categoryKey: SearchFilterKey) => {
if (activeCategory === categoryKey) {
setActiveCategory(null);
} else {
setActiveCategory(categoryKey);
calculateDropdownPosition(categoryKey);
}
};
const handleOptionSelect = (categoryKey: SearchFilterKey, optionValue: string) => {
const newValues = {
...mergedValues,
[categoryKey]: optionValue,
} as Record<SearchFilterKey, string>;
onChange(newValues);
setActiveCategory(null);
};
const getDisplayText = (categoryKey: SearchFilterKey) => {
const category = categories.find((cat) => cat.key === categoryKey);
if (!category) return '';
const value = mergedValues[categoryKey];
if (!value || value === DEFAULTS[categoryKey]) return category.label;
const option = category.options.find((opt) => opt.value === value);
return option?.label || category.label;
};
const isDefaultValue = (categoryKey: SearchFilterKey) => {
const value = mergedValues[categoryKey];
return !value || value === DEFAULTS[categoryKey];
};
const isOptionSelected = (categoryKey: SearchFilterKey, optionValue: string) => {
const value = mergedValues[categoryKey] ?? DEFAULTS[categoryKey];
return value === optionValue;
};
useEffect(() => {
const handleScroll = () => {
if (activeCategory) calculateDropdownPosition(activeCategory);
};
const handleResize = () => {
if (activeCategory) calculateDropdownPosition(activeCategory);
};
window.addEventListener('scroll', handleScroll);
window.addEventListener('resize', handleResize);
return () => {
window.removeEventListener('scroll', handleScroll);
window.removeEventListener('resize', handleResize);
};
}, [activeCategory]);
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (
dropdownRef.current &&
!dropdownRef.current.contains(event.target as Node) &&
!Object.values(categoryRefs.current).some((ref) => ref && ref.contains(event.target as Node))
) {
setActiveCategory(null);
}
};
document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
}, []);
return (
<>
<div className='relative inline-flex rounded-full p-0.5 sm:p-1 bg-transparent gap-1 sm:gap-2'>
{categories.map((category) => (
<div key={category.key} ref={(el) => { categoryRefs.current[category.key] = el; }} className='relative'>
<button
onClick={() => handleCategoryClick(category.key)}
className={`relative z-10 px-1.5 py-0.5 sm:px-2 sm:py-1 md:px-4 md:py-2 text-xs sm:text-sm font-medium rounded-full transition-all duration-200 whitespace-nowrap ${activeCategory === category.key
? isDefaultValue(category.key)
? 'text-gray-900 dark:text-gray-100 cursor-default'
: 'text-green-600 dark:text-green-400 cursor-default'
: isDefaultValue(category.key)
? 'text-gray-700 hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-100 cursor-pointer'
: 'text-green-600 hover:text-green-700 dark:text-green-400 dark:hover:text-green-300 cursor-pointer'
}`}
>
<span>{getDisplayText(category.key)}</span>
<svg className={`inline-block w-2.5 h-2.5 sm:w-3 sm:h-3 ml-0.5 sm:ml-1 transition-transform duration-200 ${activeCategory === category.key ? 'rotate-180' : ''}`} fill='none' stroke='currentColor' viewBox='0 0 24 24'>
<path strokeLinecap='round' strokeLinejoin='round' strokeWidth={2} d='M19 9l-7 7-7-7' />
</svg>
</button>
</div>
))}
{/* 通用年份排序切换按钮 */}
<div className='relative'>
<button
onClick={() => {
const next = mergedValues.yearOrder === 'desc' ? 'asc' : 'desc';
onChange({ ...mergedValues, yearOrder: next });
}}
className={`relative z-10 px-1.5 py-0.5 sm:px-2 sm:py-1 md:px-4 md:py-2 text-xs sm:text-sm font-medium rounded-full transition-all duration-200 whitespace-nowrap ${mergedValues.yearOrder === 'desc'
? 'text-gray-700 hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-100 cursor-pointer'
: 'text-green-600 hover:text-green-700 dark:text-green-400 dark:hover:text-green-300 cursor-pointer'
}`}
aria-label={`按年份${mergedValues.yearOrder === 'desc' ? '降序' : '升序'}排序`}
>
<span></span>
{mergedValues.yearOrder === 'desc' ? (
<ArrowDownWideNarrow className='inline-block ml-1 w-4 h-4 sm:w-4 sm:h-4' />
) : (
<ArrowUpNarrowWide className='inline-block ml-1 w-4 h-4 sm:w-4 sm:h-4' />
)}
</button>
</div>
</div>
{activeCategory && createPortal(
<div
ref={dropdownRef}
className='fixed z-[9999] bg-white/95 dark:bg-gray-800/95 rounded-xl border border-gray-200/50 dark:border-gray-700/50 backdrop-blur-sm'
style={{
left: `${dropdownPosition.x}px`,
top: `${dropdownPosition.y}px`,
...(typeof window !== 'undefined' && window.innerWidth < 768 ? { width: `${dropdownPosition.width}px` } : { minWidth: `${Math.max(dropdownPosition.width, 240)}px` }),
maxWidth: '600px',
position: 'fixed',
}}
>
<div className='p-2 sm:p-4'>
<div className='grid grid-cols-3 sm:grid-cols-4 md:grid-cols-5 gap-1 sm:gap-2'>
{categories.find((cat) => cat.key === activeCategory)?.options.map((option) => (
<button
key={option.value}
onClick={() => handleOptionSelect(activeCategory, option.value)}
className={`px-2 py-1.5 sm:px-3 sm:py-2 text-xs sm:text-sm rounded-lg transition-all duration-200 text-left ${isOptionSelected(activeCategory, option.value)
? 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400 border border-green-200 dark:border-green-700'
: 'text-gray-700 dark:text-gray-300 hover:bg-gray-100/80 dark:hover:bg-gray-700/80'
}`}
>
{option.label}
</button>
))}
</div>
</div>
</div>,
document.body
)}
</>
);
};
export default SearchResultFilter;

View File

@@ -110,9 +110,9 @@ export default function VideoCard({
: 'tv'
: type;
// 获取收藏状态
// 获取收藏状态(搜索结果页面不检查)
useEffect(() => {
if (from === 'douban' || !actualSource || !actualId) return;
if (from === 'douban' || from === 'search' || !actualSource || !actualId) return;
const fetchFavoriteStatus = async () => {
try {
@@ -143,7 +143,7 @@ export default function VideoCard({
async (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
if (from === 'douban' || !actualSource || !actualId) return;
if (from === 'douban' || from === 'search' || !actualSource || !actualId) return;
try {
if (favorited) {
// 如果已收藏,删除收藏
@@ -196,18 +196,15 @@ export default function VideoCard({
const handleClick = useCallback(() => {
if (from === 'douban') {
router.push(
`/play?title=${encodeURIComponent(actualTitle.trim())}${
actualYear ? `&year=${actualYear}` : ''
`/play?title=${encodeURIComponent(actualTitle.trim())}${actualYear ? `&year=${actualYear}` : ''
}${actualSearchType ? `&stype=${actualSearchType}` : ''}`
);
} else if (actualSource && actualId) {
router.push(
`/play?source=${actualSource}&id=${actualId}&title=${encodeURIComponent(
actualTitle
)}${actualYear ? `&year=${actualYear}` : ''}${
isAggregate ? '&prefer=true' : ''
}${
actualQuery ? `&stitle=${encodeURIComponent(actualQuery.trim())}` : ''
)}${actualYear ? `&year=${actualYear}` : ''}${isAggregate ? '&prefer=true' : ''
}${actualQuery ? `&stitle=${encodeURIComponent(actualQuery.trim())}` : ''
}${actualSearchType ? `&stype=${actualSearchType}` : ''}`
);
}
@@ -247,7 +244,7 @@ export default function VideoCard({
showSourceName: true,
showProgress: false,
showPlayButton: true,
showHeart: !isAggregate,
showHeart: false,
showCheckCircle: false,
showDoubanLink: !!actualDoubanId,
showRating: false,
@@ -323,11 +320,10 @@ export default function VideoCard({
<Heart
onClick={handleToggleFavorite}
size={20}
className={`transition-all duration-300 ease-out ${
favorited
? 'fill-red-600 stroke-red-600'
: 'fill-transparent stroke-white hover:stroke-red-400'
} hover:scale-[1.1]`}
className={`transition-all duration-300 ease-out ${favorited
? 'fill-red-600 stroke-red-600'
: 'fill-transparent stroke-white hover:stroke-red-400'
} hover:scale-[1.1]`}
/>
)}
</div>