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:
@@ -15,12 +15,25 @@ export interface HistoryItem {
|
||||
export function useSearchHistory() {
|
||||
const history = ref<HistoryItem[]>([])
|
||||
const loading = ref(false)
|
||||
const HISTORY_HOURS = 2 // Keep last 2 hours of history
|
||||
|
||||
async function loadHistory() {
|
||||
loading.value = true
|
||||
try {
|
||||
const result = await apiCall('get_search_history', { limit: 30 })
|
||||
history.value = Array.isArray(result) ? result : []
|
||||
const result = await apiCall('get_search_history', { limit: 50 })
|
||||
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) {
|
||||
console.error('Failed to load history:', e)
|
||||
} 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) {
|
||||
const id = `h_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user