feat: keep only last 2 hours of search history

- Filter history to show only items from last 2 hours
- Pinned items are always kept regardless of age
- Auto-cleanup old unpinned items from database
- Increased fetch limit to 50 to ensure enough items for filtering
This commit is contained in:
2026-10-20 17:57:27 +08:00
parent a75250ed17
commit 55c39e7732

View File

@@ -15,12 +15,25 @@ export interface HistoryItem {
export function useSearchHistory() { export function useSearchHistory() {
const history = ref<HistoryItem[]>([]) const history = ref<HistoryItem[]>([])
const loading = ref(false) const loading = ref(false)
const HISTORY_HOURS = 2 // Keep last 2 hours of history
async function loadHistory() { async function loadHistory() {
loading.value = true loading.value = true
try { try {
const result = await apiCall('get_search_history', { limit: 30 }) const result = await apiCall('get_search_history', { limit: 50 })
history.value = Array.isArray(result) ? result : [] const allHistory = Array.isArray(result) ? result : []
// Filter to last 2 hours (keep pinned items regardless of age)
const cutoffTime = Date.now() - (HISTORY_HOURS * 60 * 60 * 1000)
history.value = allHistory.filter((item: HistoryItem) => {
if (item.pinned) return true // Always keep pinned items
if (!item.updated_at) return false
const itemTime = new Date(item.updated_at).getTime()
return itemTime >= cutoffTime
})
// Clean up old unpinned items from database
cleanupOldHistory(cutoffTime)
} catch (e) { } catch (e) {
console.error('Failed to load history:', e) console.error('Failed to load history:', e)
} finally { } finally {
@@ -28,6 +41,23 @@ export function useSearchHistory() {
} }
} }
async function cleanupOldHistory(cutoffTime: number) {
try {
const result = await apiCall('get_search_history', { limit: 100 })
const allHistory = Array.isArray(result) ? result : []
for (const item of allHistory) {
if (item.pinned) continue
if (!item.updated_at) continue
const itemTime = new Date(item.updated_at).getTime()
if (itemTime < cutoffTime) {
await apiCall('delete_search_history', { id: item.id })
}
}
} catch (e) {
console.error('Failed to cleanup old history:', e)
}
}
async function saveToHistory(query: string, chatState: string, mode: string) { async function saveToHistory(query: string, chatState: string, mode: string) {
const id = `h_${Date.now()}_${Math.random().toString(36).slice(2, 8)}` const id = `h_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`
try { try {