修正search页面僵尸历史记录tag
This commit is contained in:
@@ -372,11 +372,11 @@ function SearchPageClient() {
|
||||
const yearsSet = new Set<string>();
|
||||
|
||||
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 }[] = [
|
||||
|
||||
@@ -860,13 +860,15 @@ export async function getSearchHistory(): Promise<string[]> {
|
||||
// 返回缓存数据,同时后台异步更新
|
||||
fetchFromApi<string[]>(`/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<string[]> {
|
||||
// 缓存为空,直接从 API 获取并缓存
|
||||
try {
|
||||
const freshData = await fetchFromApi<string[]>(`/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<string[]> {
|
||||
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('读取搜索历史失败');
|
||||
|
||||
Reference in New Issue
Block a user