-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into chore/lw-10236-use-dr…
…ep-registration-tracker
- Loading branch information
Showing
56 changed files
with
967 additions
and
405 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
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.