55 * Stores up to 50 search queries with FIFO eviction.
66 */
77
8+ import { logger } from '@bibgraph/utils' ;
89import { openDB } from 'idb' ;
910import { useCallback , useEffect , useState } from 'react' ;
1011
11- import { logger } from '@bibgraph/utils' ;
12-
1312const SEARCH_HISTORY_DB_NAME = 'bibgraph-search-history' ;
1413const SEARCH_HISTORY_STORE_NAME = 'queries' ;
1514const SEARCH_HISTORY_VERSION = 1 ;
@@ -30,7 +29,7 @@ interface SearchHistoryDB {
3029
3130const initializeDB = async ( ) => {
3231 return openDB < SearchHistoryDB > ( SEARCH_HISTORY_DB_NAME , SEARCH_HISTORY_VERSION , {
33- upgrade ( db ) {
32+ upgrade : ( db ) => {
3433 if ( ! db . objectStoreNames . contains ( SEARCH_HISTORY_STORE_NAME ) ) {
3534 const store = db . createObjectStore ( SEARCH_HISTORY_STORE_NAME , {
3635 keyPath : 'id' ,
@@ -51,9 +50,9 @@ export const useSearchHistory = () => {
5150 try {
5251 const db = await initializeDB ( ) ;
5352 const allEntries = await db . getAll ( SEARCH_HISTORY_STORE_NAME ) ;
54- const sortedEntries = allEntries
55- . sort ( ( a , b ) => b . timestamp . getTime ( ) - a . timestamp . getTime ( ) )
56- . slice ( 0 , MAX_SEARCH_HISTORY ) ;
53+ // Sort by timestamp (newest first)
54+ const sortedByTime = [ ... allEntries ] . sort ( ( a , b ) => b . timestamp . getTime ( ) - a . timestamp . getTime ( ) ) ;
55+ const sortedEntries = sortedByTime . slice ( 0 , MAX_SEARCH_HISTORY ) ;
5756 setSearchHistory ( sortedEntries ) ;
5857 setIsInitialized ( true ) ;
5958 } catch ( error ) {
@@ -99,9 +98,9 @@ export const useSearchHistory = () => {
9998
10099 // Update state
101100 const updatedHistory = await db . getAll ( SEARCH_HISTORY_STORE_NAME ) ;
102- const sortedHistory = updatedHistory
103- . sort ( ( a , b ) => b . timestamp . getTime ( ) - a . timestamp . getTime ( ) )
104- . slice ( 0 , MAX_SEARCH_HISTORY ) ;
101+ // Sort by timestamp (newest first)
102+ const sortedByTimeNewest = [ ... updatedHistory ] . sort ( ( a , b ) => b . timestamp . getTime ( ) - a . timestamp . getTime ( ) ) ;
103+ const sortedHistory = sortedByTimeNewest . slice ( 0 , MAX_SEARCH_HISTORY ) ;
105104 setSearchHistory ( sortedHistory ) ;
106105
107106 logger . debug ( 'search-history' , 'Added search query to history' , { query } ) ;
0 commit comments