diff --git a/src/app/search/page.tsx b/src/app/search/page.tsx index d43bae6..f8f8427 100644 --- a/src/app/search/page.tsx +++ b/src/app/search/page.tsx @@ -1,13 +1,14 @@ /* eslint-disable react-hooks/exhaustive-deps */ 'use client'; -import { Search } from 'lucide-react'; +import { Search, X } from 'lucide-react'; import { useRouter, useSearchParams } from 'next/navigation'; import { Suspense, useEffect, useMemo, useRef, useState } from 'react'; import { addSearchHistory, clearSearchHistory, + deleteSearchHistory, getSearchHistory, } from '@/lib/db.client'; import { SearchResult } from '@/lib/types'; @@ -224,17 +225,32 @@ function SearchPageClient() { )}
- {searchHistory.map((item, index) => ( - + {searchHistory.map((item) => ( +
+ + {/* 删除按钮 */} + +
))}
diff --git a/src/lib/db.client.ts b/src/lib/db.client.ts index 6d041d8..d39a973 100644 --- a/src/lib/db.client.ts +++ b/src/lib/db.client.ts @@ -265,6 +265,37 @@ export async function clearSearchHistory(): Promise { localStorage.removeItem(SEARCH_HISTORY_KEY); } +/** + * 删除单条搜索历史 + */ +export async function deleteSearchHistory(keyword: string): Promise { + const trimmed = keyword.trim(); + if (!trimmed) return; + + // 数据库模式 + if (STORAGE_TYPE === 'database') { + try { + await fetch(`/api/searchhistory?keyword=${encodeURIComponent(trimmed)}`, { + method: 'DELETE', + }); + } catch (err) { + console.error('删除搜索历史失败:', err); + } + return; + } + + // localStorage 模式 + if (typeof window === 'undefined') return; + + try { + const history = await getSearchHistory(); + const newHistory = history.filter((k) => k !== trimmed); + localStorage.setItem(SEARCH_HISTORY_KEY, JSON.stringify(newHistory)); + } catch (err) { + console.error('删除搜索历史失败:', err); + } +} + // ---------------- 收藏相关 API ---------------- // 收藏数据结构