-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add search functionality to NFTs (#1039)
* feat: add search functionality to NFTs * refactor: rename variables, improve readability * refactor: modify search to include partial keywords * refactor: rewrite hook and add markup prop * refactor: remove query selector * fix: separator for breadcrumb in dark mode * refactor: remove breadcrumbs for popup mode
- Loading branch information
1 parent
dd13595
commit 4046387
Showing
19 changed files
with
643 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useMemo, useState } from 'react'; | ||
import { NftItemProps } from '@lace/core'; | ||
import { AssetOrHandleInfoMap } from './useAssetInfo'; | ||
import { Cardano } from '@cardano-sdk/core'; | ||
import debounce from 'lodash/debounce'; | ||
|
||
const DEBOUNCE_TIME = 1000; | ||
|
||
interface NftSearchResultProps { | ||
isSearching: boolean; | ||
filteredResults: NftItemProps[]; | ||
handleSearch: (items: NftItemProps[], searchValue: string) => void; | ||
} | ||
|
||
export const searchNfts = ( | ||
data: NftItemProps[], | ||
searchValue: string, | ||
assetsInfo: AssetOrHandleInfoMap | ||
): NftItemProps[] => | ||
data.filter( | ||
(item) => | ||
item.name.toLowerCase().includes(searchValue.toLowerCase()) || | ||
item.assetId === searchValue || | ||
assetsInfo.get(Cardano.AssetId(item.assetId)).policyId === searchValue | ||
); | ||
|
||
export const useNftSearch = (assetsInfo: AssetOrHandleInfoMap): NftSearchResultProps => { | ||
const [isSearching, setIsSearching] = useState(false); | ||
const [filteredResults, setFilteredResults] = useState<NftItemProps[]>([]); | ||
const searchDebounced = useMemo( | ||
() => | ||
debounce((items: NftItemProps[], searchValue: string) => { | ||
const filteredNfts = searchNfts(items, searchValue, assetsInfo); | ||
setFilteredResults(filteredNfts); | ||
setIsSearching(false); | ||
}, DEBOUNCE_TIME), | ||
[assetsInfo] | ||
); | ||
|
||
const handleSearch = (items: NftItemProps[], searchValue: string) => { | ||
setIsSearching(true); | ||
searchDebounced(items, searchValue); | ||
}; | ||
|
||
return { isSearching, filteredResults, handleSearch }; | ||
}; |
Oops, something went wrong.