From f3877811c55a91419efce3561fed7bc2f72e784d Mon Sep 17 00:00:00 2001 From: mtvpls Date: Wed, 11 Mar 2026 15:27:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3search=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E5=83=B5=E5=B0=B8=E5=8E=86=E5=8F=B2=E8=AE=B0=E5=BD=95tag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/search/page.tsx | 6 +++--- src/lib/db.client.ts | 19 ++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/app/search/page.tsx b/src/app/search/page.tsx index 221d304..a10fed1 100644 --- a/src/app/search/page.tsx +++ b/src/app/search/page.tsx @@ -372,11 +372,11 @@ function SearchPageClient() { const yearsSet = new Set(); searchResults.forEach((item) => { - if (item.source && item.source_name) { + if (item.source && item.source_name && item.source.trim() !== '' && item.source_name.trim() !== '') { sourcesSet.set(item.source, item.source_name); } - if (item.title) titlesSet.add(item.title); - if (item.year) yearsSet.add(item.year); + if (item.title && item.title.trim() !== '') titlesSet.add(item.title); + if (item.year && item.year.trim() !== '') yearsSet.add(item.year); }); const sourceOptions: { label: string; value: string }[] = [ diff --git a/src/lib/db.client.ts b/src/lib/db.client.ts index 9adf9c4..5fcfcde 100644 --- a/src/lib/db.client.ts +++ b/src/lib/db.client.ts @@ -860,13 +860,15 @@ export async function getSearchHistory(): Promise { // 返回缓存数据,同时后台异步更新 fetchFromApi(`/api/searchhistory`) .then((freshData) => { + // 去重处理 + const uniqueData = Array.from(new Set(freshData)); // 只有数据真正不同时才更新缓存 - if (JSON.stringify(cachedData) !== JSON.stringify(freshData)) { - cacheManager.cacheSearchHistory(freshData); + if (JSON.stringify(cachedData) !== JSON.stringify(uniqueData)) { + cacheManager.cacheSearchHistory(uniqueData); // 触发数据更新事件 window.dispatchEvent( new CustomEvent('searchHistoryUpdated', { - detail: freshData, + detail: uniqueData, }) ); } @@ -881,8 +883,10 @@ export async function getSearchHistory(): Promise { // 缓存为空,直接从 API 获取并缓存 try { const freshData = await fetchFromApi(`/api/searchhistory`); - cacheManager.cacheSearchHistory(freshData); - return freshData; + // 去重处理 + const uniqueData = Array.from(new Set(freshData)); + cacheManager.cacheSearchHistory(uniqueData); + return uniqueData; } catch (err) { console.error('获取搜索历史失败:', err); triggerGlobalError('获取搜索历史失败'); @@ -896,8 +900,9 @@ export async function getSearchHistory(): Promise { const raw = localStorage.getItem(SEARCH_HISTORY_KEY); if (!raw) return []; const arr = JSON.parse(raw) as string[]; - // 仅返回字符串数组 - return Array.isArray(arr) ? arr : []; + // 仅返回字符串数组,并去重 + const validArray = Array.isArray(arr) ? arr : []; + return Array.from(new Set(validArray)); } catch (err) { console.error('读取搜索历史失败:', err); triggerGlobalError('读取搜索历史失败');