Skip to content

Commit a82f706

Browse files
committed
upd: search history
1 parent 925f0f1 commit a82f706

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "stability-ui",
33
"type": "module",
4-
"version": "0.8.20-alpha",
4+
"version": "0.8.21-alpha",
55
"scripts": {
66
"dev": "astro dev",
77
"start": "astro dev",

src/modules/SonicVaults/index.tsx

+52-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect, useRef, useMemo } from "react";
1+
import { useState, useEffect, useRef, useMemo, useCallback } from "react";
22

33
import { useWeb3Modal } from "@web3modal/wagmi/react";
44

@@ -29,7 +29,7 @@ import {
2929

3030
import { toVault, initFilters } from "./functions";
3131

32-
import { formatNumber, formatFromBigInt, dataSorter } from "@utils";
32+
import { formatNumber, formatFromBigInt, dataSorter, debounce } from "@utils";
3333

3434
import {
3535
SONIC_TABLE,
@@ -102,7 +102,7 @@ const SonicVaults = (): JSX.Element => {
102102
}
103103

104104
const search: React.RefObject<HTMLInputElement> = useRef(null);
105-
// const [searchHistory, setSearchHistory] = useState<string[]>([]);
105+
const [searchHistory, setSearchHistory] = useState<string[]>([]);
106106

107107
const [localVaults, setLocalVaults] = useState<TVault[]>([]);
108108
const [filteredVaults, setFilteredVaults] = useState<TVault[]>([]);
@@ -149,6 +149,15 @@ const SonicVaults = (): JSX.Element => {
149149
tableFilters.find((filter) => filter.name === "My vaults")?.state &&
150150
!$connected;
151151

152+
const handleSearch = (value: string) => {
153+
if (search?.current) {
154+
search.current.value = value;
155+
156+
tableHandler();
157+
setSearchHistory([]);
158+
}
159+
};
160+
152161
const activeNetworksHandler = async (chainIDs: string[]) => {
153162
let updatedNetworks = activeNetworks.map((network) =>
154163
chainIDs.includes(network.id)
@@ -237,6 +246,27 @@ const SonicVaults = (): JSX.Element => {
237246
tableHandler(_tableStates, DEFAULT_TABLE_PARAMS);
238247
};
239248

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+
240270
const tableHandler = (
241271
table: TTableColumn[] = tableStates,
242272
tableParams = activeTableParams
@@ -245,6 +275,9 @@ const SonicVaults = (): JSX.Element => {
245275

246276
const searchValue: string = String(search?.current?.value.toLowerCase());
247277

278+
//@ts-ignore
279+
updateHistorySearch(searchValue);
280+
248281
let activeNetworksVaults: { [key: string]: TVault[] } = {};
249282

250283
activeNetworks.forEach((network) => {
@@ -395,9 +428,22 @@ const SonicVaults = (): JSX.Element => {
395428
_activeTableParams = { ..._activeTableParams, sort: 0 };
396429
}
397430

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+
398443
setActiveTableParams(_activeTableParams);
399444
setFilteredVaults(sortedVaults);
400445
setTableStates(table);
446+
setSearchHistory(historyData);
401447
};
402448

403449
const initVaults = async () => {
@@ -482,18 +528,19 @@ const SonicVaults = (): JSX.Element => {
482528
onChange={() => tableHandler()}
483529
/>
484530
</label>
485-
{/* {searchHistory.length > 0 && (
531+
{searchHistory.length > 0 && (
486532
<ul className="absolute left-0 mt-2 w-full bg-accent-900 text-neutral-50 font-manrope rounded-2xl z-[10]">
487533
{searchHistory.map((text, index) => (
488534
<li
489535
key={text + index}
490536
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)}
491538
>
492539
{text}
493540
</li>
494541
))}
495542
</ul>
496-
)} */}
543+
)}
497544
</div>
498545

499546
<Filters

0 commit comments

Comments
 (0)