feat: add NEXT_PUBLIC_SEARCH_MAX_PAGE & NEXT_PUBLIC_AGGREGATE_SEARCH_RESULT

This commit is contained in:
shinya
2025-06-26 13:57:17 +08:00
parent 1d75e85264
commit 1e9e8fb07a
4 changed files with 21 additions and 12 deletions

View File

@@ -3,6 +3,10 @@ import { NextResponse } from 'next/server';
import { API_CONFIG, ApiSite, getApiSites, getCacheTime } from '@/lib/config';
import { cleanHtmlTags } from '@/lib/utils';
// 根据环境变量决定最大搜索页数,默认 5
const MAX_SEARCH_PAGES: number =
Number(process.env.NEXT_PUBLIC_SEARCH_MAX_PAGE) || 5;
export interface SearchResult {
id: string;
title: string;
@@ -104,10 +108,7 @@ async function searchFromApi(
// 获取总页数
const pageCount = data.pagecount || 1;
// 确定需要获取的额外页数
const pagesToFetch = Math.min(
pageCount - 1,
API_CONFIG.search.maxPages - 1
);
const pagesToFetch = Math.min(pageCount - 1, MAX_SEARCH_PAGES - 1);
// 如果有额外页数,获取更多页的结果
if (pagesToFetch > 0) {

View File

@@ -41,8 +41,15 @@ function SearchPageClient() {
const [searchResults, setSearchResults] = useState<SearchResult[]>([]);
const searchInputRef = useRef<HTMLInputElement>(null);
// 视图模式:聚合(agg) 或 全部(all)
const [viewMode, setViewMode] = useState<'agg' | 'all'>('agg');
// 视图模式:聚合(agg) 或 全部(all),默认值由环境变量 NEXT_PUBLIC_AGGREGATE_SEARCH_RESULT 决定
const [viewMode, setViewMode] = useState<'agg' | 'all'>(() => {
const envVal = process.env.NEXT_PUBLIC_AGGREGATE_SEARCH_RESULT;
// 默认聚合('agg')。当显式设置为 'false' 或 '0' 时使用 'all'
if (envVal === 'false' || envVal === '0') {
return 'all';
}
return 'agg';
});
// 聚合后的结果(按标题和年份分组)
const aggregatedResults = useMemo(() => {