Skip to content

Commit e8384ca

Browse files
committed
fix(web): resolve lint issues in useSearchHistory (task-3)
- Separated chained array sort operations into discrete statements - Fixed sonarjs/no-misleading-array-reverse lint errors - Improves code readability and maintains same functionality
1 parent 8464f58 commit e8384ca

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

apps/web/src/hooks/useSearchHistory.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
* Stores up to 50 search queries with FIFO eviction.
66
*/
77

8+
import { logger } from '@bibgraph/utils';
89
import { openDB } from 'idb';
910
import { useCallback, useEffect, useState } from 'react';
1011

11-
import { logger } from '@bibgraph/utils';
12-
1312
const SEARCH_HISTORY_DB_NAME = 'bibgraph-search-history';
1413
const SEARCH_HISTORY_STORE_NAME = 'queries';
1514
const SEARCH_HISTORY_VERSION = 1;
@@ -30,7 +29,7 @@ interface SearchHistoryDB {
3029

3130
const 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

Comments
 (0)