-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
70 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Enable search by validator name |
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 { hasTextMatch } from 'app/components/HighlightedText/text-matching' | ||
import { | ||
Layer, | ||
useGetConsensusValidatorsAddressNameMap, | ||
useGetConsensusAccountsAddresses, | ||
ValidatorAddressNameMap, | ||
} from 'oasis-nexus/api' | ||
import { Network } from 'types/network' | ||
import { AccountNameSearchResults, AccountNameSearchConsensusMatch } from '../data/named-accounts' | ||
|
||
function findAddressesWithMatch(addressMap: ValidatorAddressNameMap, nameFragment: string, network: Network) { | ||
const matchedAddresses: AccountNameSearchConsensusMatch[] = [] | ||
|
||
for (const [address, name] of Object.entries(addressMap)) { | ||
if (hasTextMatch(name, [nameFragment])) { | ||
matchedAddresses.push({ address, layer: Layer.consensus, network }) | ||
} | ||
} | ||
|
||
return matchedAddresses | ||
} | ||
|
||
export const useSearchForValidatorByName = ( | ||
network: Network, | ||
nameFragment: string | undefined, | ||
): AccountNameSearchResults => { | ||
const { isLoading, isError, error, data } = useGetConsensusValidatorsAddressNameMap(network) | ||
if (isError) { | ||
console.log('Failed to load Validator data', error) | ||
} | ||
const matches = data?.data && nameFragment ? findAddressesWithMatch(data?.data, nameFragment, network) : [] | ||
const consensusMatches = matches as AccountNameSearchConsensusMatch[] | ||
const { | ||
isLoading: areConsensusAccountsLoading, | ||
isError: areConsensusAccountsError, | ||
data: consensusResults, | ||
} = useGetConsensusAccountsAddresses(consensusMatches, { | ||
enabled: !isLoading && !isError, | ||
}) | ||
|
||
return { | ||
isLoading: isLoading || areConsensusAccountsLoading, | ||
isError: isError || areConsensusAccountsError, | ||
results: [...consensusResults], | ||
} | ||
} |
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