1
- import { useState , useEffect , useRef , useMemo } from "react" ;
1
+ import { useState , useEffect , useRef , useMemo , useCallback } from "react" ;
2
2
3
3
import { useWeb3Modal } from "@web3modal/wagmi/react" ;
4
4
@@ -29,7 +29,7 @@ import {
29
29
30
30
import { toVault , initFilters } from "./functions" ;
31
31
32
- import { formatNumber , formatFromBigInt , dataSorter } from "@utils" ;
32
+ import { formatNumber , formatFromBigInt , dataSorter , debounce } from "@utils" ;
33
33
34
34
import {
35
35
SONIC_TABLE ,
@@ -102,7 +102,7 @@ const SonicVaults = (): JSX.Element => {
102
102
}
103
103
104
104
const search : React . RefObject < HTMLInputElement > = useRef ( null ) ;
105
- // const [searchHistory, setSearchHistory] = useState<string[]>([]);
105
+ const [ searchHistory , setSearchHistory ] = useState < string [ ] > ( [ ] ) ;
106
106
107
107
const [ localVaults , setLocalVaults ] = useState < TVault [ ] > ( [ ] ) ;
108
108
const [ filteredVaults , setFilteredVaults ] = useState < TVault [ ] > ( [ ] ) ;
@@ -149,6 +149,15 @@ const SonicVaults = (): JSX.Element => {
149
149
tableFilters . find ( ( filter ) => filter . name === "My vaults" ) ?. state &&
150
150
! $connected ;
151
151
152
+ const handleSearch = ( value : string ) => {
153
+ if ( search ?. current ) {
154
+ search . current . value = value ;
155
+
156
+ tableHandler ( ) ;
157
+ setSearchHistory ( [ ] ) ;
158
+ }
159
+ } ;
160
+
152
161
const activeNetworksHandler = async ( chainIDs : string [ ] ) => {
153
162
let updatedNetworks = activeNetworks . map ( ( network ) =>
154
163
chainIDs . includes ( network . id )
@@ -237,6 +246,27 @@ const SonicVaults = (): JSX.Element => {
237
246
tableHandler ( _tableStates , DEFAULT_TABLE_PARAMS ) ;
238
247
} ;
239
248
249
+ const updateHistorySearch = useCallback (
250
+ debounce ( ( value : string ) => {
251
+ if ( ! value ) return ;
252
+
253
+ const history = JSON . parse (
254
+ localStorage . getItem ( "searchHistory" ) as string
255
+ ) ;
256
+
257
+ if ( Array . isArray ( history ) && history . includes ( value ) ) return ;
258
+
259
+ let newValues = history ? [ ...history , value ] : [ value ] ;
260
+
261
+ if ( newValues . length > 10 ) {
262
+ newValues . shift ( ) ;
263
+ }
264
+
265
+ localStorage . setItem ( "searchHistory" , JSON . stringify ( newValues ) ) ;
266
+ } , 2000 ) ,
267
+ [ ]
268
+ ) ;
269
+
240
270
const tableHandler = (
241
271
table : TTableColumn [ ] = tableStates ,
242
272
tableParams = activeTableParams
@@ -245,6 +275,9 @@ const SonicVaults = (): JSX.Element => {
245
275
246
276
const searchValue : string = String ( search ?. current ?. value . toLowerCase ( ) ) ;
247
277
278
+ //@ts -ignore
279
+ updateHistorySearch ( searchValue ) ;
280
+
248
281
let activeNetworksVaults : { [ key : string ] : TVault [ ] } = { } ;
249
282
250
283
activeNetworks . forEach ( ( network ) => {
@@ -395,9 +428,22 @@ const SonicVaults = (): JSX.Element => {
395
428
_activeTableParams = { ..._activeTableParams , sort : 0 } ;
396
429
}
397
430
431
+ // search history
432
+ const history = JSON . parse ( localStorage . getItem ( "searchHistory" ) as string ) ;
433
+
434
+ const historyData : string [ ] = [ ] ;
435
+ if ( Array . isArray ( history ) && searchValue ) {
436
+ for ( const historyValue of history ) {
437
+ if ( historyValue . includes ( searchValue ) ) {
438
+ historyData . push ( historyValue ) ;
439
+ }
440
+ }
441
+ }
442
+
398
443
setActiveTableParams ( _activeTableParams ) ;
399
444
setFilteredVaults ( sortedVaults ) ;
400
445
setTableStates ( table ) ;
446
+ setSearchHistory ( historyData ) ;
401
447
} ;
402
448
403
449
const initVaults = async ( ) => {
@@ -482,18 +528,19 @@ const SonicVaults = (): JSX.Element => {
482
528
onChange = { ( ) => tableHandler ( ) }
483
529
/>
484
530
</ label >
485
- { /* { searchHistory.length > 0 && (
531
+ { searchHistory . length > 0 && (
486
532
< ul className = "absolute left-0 mt-2 w-full bg-accent-900 text-neutral-50 font-manrope rounded-2xl z-[10]" >
487
533
{ searchHistory . map ( ( text , index ) => (
488
534
< li
489
535
key = { text + index }
490
536
className = { `p-2 cursor-pointer hover:bg-accent-800 ${ ! index ? "hover:rounded-t-2xl" : index === searchHistory . length - 1 ? "hover:rounded-b-2xl" : "" } ` }
537
+ onClick = { ( ) => handleSearch ( text ) }
491
538
>
492
539
{ text }
493
540
</ li >
494
541
) ) }
495
542
</ ul >
496
- )} */ }
543
+ ) }
497
544
</ div >
498
545
499
546
< Filters
0 commit comments