-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add search for fellowshipMember selector, #5271
- Loading branch information
Showing
3 changed files
with
63 additions
and
5 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
55 changes: 55 additions & 0 deletions
55
packages/next-common/components/fellowshipMemberSelector/useSearchFellowshipMember.js
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,55 @@ | ||
import { useChainSettings } from "next-common/context/chain"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { isAddress } from "@polkadot/util-crypto"; | ||
import { useDebounce } from "react-use"; | ||
import { fetchBatchIdentities } from "next-common/components/data/hooks/useSearchByAddressIdentity"; | ||
|
||
export default function useSearchFellowshipMember( | ||
searchValue = "", | ||
members = [], | ||
) { | ||
const [identityMapping, setIdentityMapping] = useState({}); | ||
const [debouncedSearchValue, setDebouncedSearchValue] = useState(""); | ||
const { identity: identityChain } = useChainSettings(); | ||
|
||
useDebounce( | ||
() => { | ||
setDebouncedSearchValue(searchValue); | ||
}, | ||
500, | ||
[searchValue], | ||
); | ||
|
||
useEffect(() => { | ||
const allAccounts = new Set(); | ||
|
||
members?.forEach(({ address }) => { | ||
allAccounts.add(address); | ||
}); | ||
|
||
const accountList = Array.from(allAccounts); | ||
|
||
const fetchIdentities = async () => { | ||
const identities = await fetchBatchIdentities(identityChain, accountList); | ||
setIdentityMapping(identities); | ||
}; | ||
|
||
fetchIdentities(); | ||
}, [members, identityChain]); | ||
|
||
return useMemo(() => { | ||
const search = (debouncedSearchValue || "").toLowerCase(); | ||
|
||
if (!search) { | ||
return members; | ||
} | ||
|
||
if (isAddress(debouncedSearchValue)) { | ||
return members?.filter(({ address }) => address.toLowerCase() === search); | ||
} | ||
|
||
return members?.filter(({ address }) => { | ||
return identityMapping[address]?.includes(search); | ||
}); | ||
}, [debouncedSearchValue, members, identityMapping]); | ||
} |