Skip to content

Commit

Permalink
Enable search by validator name
Browse files Browse the repository at this point in the history
  • Loading branch information
buberdds committed Nov 13, 2024
1 parent a9c936d commit c7b79f5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions .changelog/1633.trivial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enable search by validator name
5 changes: 5 additions & 0 deletions src/app/components/Search/search-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ export const validateAndNormalize = {
return searchTerm.toLowerCase()
}
},
validatorNameFragment: (searchTerm: string) => {
if (searchTerm?.length >= textSearchMinimumLength) {
return searchTerm.toLowerCase()
}
},
} satisfies { [name: string]: (searchTerm: string) => string | undefined }

export function isSearchValid(searchTerm: string) {
Expand Down
46 changes: 46 additions & 0 deletions src/app/hooks/useSearchForValidatorByName.ts
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],
}
}
18 changes: 18 additions & 0 deletions src/app/pages/SearchResultsPage/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { RouteUtils } from '../../utils/route-utils'
import { SearchParams } from '../../components/Search/search-utils'
import { SearchScope } from '../../../types/searchScope'
import { useSearchForAccountsByName } from '../../hooks/useAccountMetadata'
import { useSearchForValidatorByName } from '../../hooks/useSearchForValidatorByName'

function isDefined<T>(item: T): item is NonNullable<T> {
return item != null
Expand Down Expand Up @@ -234,6 +235,21 @@ export function useNamedAccountConditionally(
}
}

export function useNamedValidatorConditionally(nameFragment: string | undefined) {
const queries = RouteUtils.getEnabledNetworksForLayer(Layer.consensus).map(network =>
// eslint-disable-next-line react-hooks/rules-of-hooks
useSearchForValidatorByName(network, nameFragment),
)
return {
isLoading: queries.some(query => query.isLoading),
isError: queries.some(query => query.isError),
results: queries
.map(query => query.results)
.filter(isDefined)
.flat(),
}
}

export const useSearch = (currentScope: SearchScope | undefined, q: SearchParams) => {
const queries = {
blockHeight: useBlocksByHeightConditionally(currentScope, q.blockHeight),
Expand All @@ -243,6 +259,7 @@ export const useSearch = (currentScope: SearchScope | undefined, q: SearchParams
oasisRuntimeAccount: useRuntimeAccountConditionally(currentScope, q.consensusAccount),
evmAccount: useRuntimeAccountConditionally(currentScope, q.evmAccount),
accountsByName: useNamedAccountConditionally(currentScope, q.accountNameFragment),
validatorByName: useNamedValidatorConditionally(q.validatorNameFragment),
tokens: useRuntimeTokenConditionally(currentScope, q.evmTokenNameFragment),
proposals: useNetworkProposalsConditionally(q.networkProposalNameFragment),
}
Expand All @@ -255,6 +272,7 @@ export const useSearch = (currentScope: SearchScope | undefined, q: SearchParams
...(queries.oasisRuntimeAccount.results || []),
...(queries.evmAccount.results || []),
...(queries.accountsByName.results || []),
...(queries.validatorByName.results || []),
].filter(isAccountNonEmpty)
const tokens = queries.tokens.results
.map(l => l.evm_tokens)
Expand Down

0 comments on commit c7b79f5

Please sign in to comment.