Skip to content

Commit

Permalink
Merge pull request #1576 from oasisprotocol/mz/validatorNames
Browse files Browse the repository at this point in the history
Show validator name instead of an address
  • Loading branch information
buberdds authored Oct 25, 2024
2 parents 6a11ce0 + 3bd883b commit f2fe95a
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 18 deletions.
1 change: 1 addition & 0 deletions .changelog/1576.trivial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Show validator name instead of an address
34 changes: 34 additions & 0 deletions src/app/components/Account/ConsensusAccountLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { FC } from 'react'
import { Layer, useGetConsensusValidatorsAddressNameMap } from './../../../oasis-nexus/api'
import { Network } from '../../../types/network'
import { ValidatorLink } from '../Validators/ValidatorLink'
import { AccountLink } from './AccountLink'

type ConsensusAccountLinkProps = {
address: string
alwaysTrim?: boolean
labelOnly?: boolean
network: Network
}

export const ConsensusAccountLink: FC<ConsensusAccountLinkProps> = ({
address,
alwaysTrim = true,
labelOnly,
network,
}) => {
const { data } = useGetConsensusValidatorsAddressNameMap(network)

if (data?.data?.[address]) {
return <ValidatorLink address={address} network={network} alwaysTrim={alwaysTrim} />
}

return (
<AccountLink
labelOnly={labelOnly}
scope={{ network, layer: Layer.consensus }}
address={address}
alwaysTrim={alwaysTrim}
/>
)
}
12 changes: 5 additions & 7 deletions src/app/components/Transactions/ConsensusTransactions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { Age } from '../Age'
import { TransactionLink } from './TransactionLink'
import { ConsensusTransactionMethod } from '../ConsensusTransactionMethod'
import { BlockLink } from '../Blocks/BlockLink'
import { AccountLink } from '../Account/AccountLink'
import { ConsensusAmount } from './ConsensusAmount'
import { TransferIcon } from '../TransferIcon'
import { ConsensusAccountLink } from '../Account/ConsensusAccountLink'

type TableConsensusTransaction = Transaction & {
markAsNew?: boolean
Expand Down Expand Up @@ -95,11 +95,10 @@ export const ConsensusTransactions: FC<ConsensusTransactionsProps> = ({
pr: 3,
}}
>
<AccountLink
<ConsensusAccountLink
labelOnly={!!ownAddress && transaction.sender === ownAddress}
scope={transaction}
network={transaction.network}
address={transaction.sender}
alwaysTrim
/>
{verbose && transaction.to && <TransferIcon />}
</Box>
Expand All @@ -110,11 +109,10 @@ export const ConsensusTransactions: FC<ConsensusTransactionsProps> = ({
? [
{
content: transaction.to ? (
<AccountLink
<ConsensusAccountLink
labelOnly={!!ownAddress && transaction.to === ownAddress}
scope={transaction}
network={transaction.network}
address={transaction.to}
alwaysTrim
/>
) : null,
key: 'to',
Expand Down
18 changes: 15 additions & 3 deletions src/app/components/Validators/ValidatorLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Typography from '@mui/material/Typography'
import { COLORS } from '../../../styles/theme/colors'
import { Network } from '../../../types/network'
import { HighlightedText } from '../HighlightedText'
import { useValidatorName } from '../../hooks/useValidatorName'

type ValidatorLinkProps = {
address: string
Expand All @@ -26,15 +27,22 @@ export const ValidatorLink: FC<ValidatorLinkProps> = ({
}) => {
const { isTablet } = useScreenSize()
const to = RouteUtils.getValidatorRoute(network, address)
const validatorName = useValidatorName(network, address)

return (
<Typography variant="mono" component="span" sx={{ color: COLORS.brandDark, fontWeight: 700 }}>
{isTablet ? (
<TabletValidatorLink address={address} name={name} to={to} highlightedPart={highlightedPart} />
<TabletValidatorLink
address={address}
name={name || validatorName}
to={to}
highlightedPart={highlightedPart}
/>
) : (
<DesktopValidatorLink
address={address}
alwaysTrim={alwaysTrim}
name={name}
name={name || validatorName}
to={to}
highlightedPart={highlightedPart}
/>
Expand Down Expand Up @@ -79,7 +87,11 @@ const DesktopValidatorLink: FC<DesktopValidatorLinkProps> = ({
highlightedPart,
}) => {
if (alwaysTrim) {
return <TrimLinkLabel label={address} to={to} />
return name ? (
<TrimValidatorEndLinkLabel name={name} to={to} highlightedPart={highlightedPart} />
) : (
<TrimLinkLabel label={address} to={to} />
)
}
return (
<Link component={RouterLink} to={to}>
Expand Down
9 changes: 9 additions & 0 deletions src/app/hooks/useValidatorName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Network } from 'types/network'
import { useGetConsensusValidatorsAddressNameMap } from '../../oasis-nexus/api'

export const useValidatorName = (network: Network, address: string): string | undefined => {
const { data } = useGetConsensusValidatorsAddressNameMap(network)
const validatorName = data?.data?.[address]

return validatorName
}
9 changes: 3 additions & 6 deletions src/app/pages/ConsensusTransactionDetailPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { BlockLink } from 'app/components/Blocks/BlockLink'
import { ConsensusTransactionMethod } from 'app/components/ConsensusTransactionMethod'
import { useFormattedTimestampStringWithDistance } from 'app/hooks/useFormattedTimestamp'
import { RoundedBalance } from 'app/components/RoundedBalance'
import { AccountLink } from 'app/components/Account/AccountLink'
import { ConsensusAccountLink } from 'app/components/Account/ConsensusAccountLink'
import { getPreciseNumberFormat } from 'locales/getPreciseNumberFormat'
import { CurrentFiatValue } from '../../components/CurrentFiatValue'
import { ConsensusTransactionEvents } from '../../components/Transactions/ConsensusTransactionEvents'
Expand Down Expand Up @@ -109,17 +109,14 @@ export const ConsensusTransactionDetailView: FC<{
</dd>
<dt>{t('common.from')}</dt>
<dd>
<AccountLink scope={transaction} address={transaction.sender} />
<ConsensusAccountLink network={transaction.network} address={transaction.sender} alwaysTrim={false} />
<CopyToClipboard value={transaction.sender} />
</dd>
{transaction.to && (
<>
<dt>{t('common.to')}</dt>
<dd>
<AccountLink
scope={{ layer: transaction.layer, network: transaction.network }}
address={transaction.to}
/>
<ConsensusAccountLink network={transaction.network} address={transaction.to} alwaysTrim={false} />
<CopyToClipboard value={transaction.to} />
</dd>
</>
Expand Down
4 changes: 2 additions & 2 deletions src/app/pages/ProposalDetailsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { DashboardLink } from '../ParatimeDashboardPage/DashboardLink'
import { useScreenSize } from '../../hooks/useScreensize'
import { ProposalStatusIcon } from '../../components/Proposals/ProposalStatusIcon'
import { TextSkeleton } from '../../components/Skeleton'
import { AccountLink } from '../../components/Account/AccountLink'
import { ConsensusAccountLink } from '../../components/Account/ConsensusAccountLink'
import { HighlightedText } from '../../components/HighlightedText'
import { ProposalIdLoaderData } from '../../utils/route-utils'
import { COLORS } from 'styles/theme/colors'
Expand Down Expand Up @@ -119,7 +119,7 @@ export const ProposalDetailView: FC<{

<dt>{t('common.submitter')}</dt>
<dd>
<AccountLink scope={proposal} address={proposal.submitter} />
<ConsensusAccountLink network={proposal.network} address={proposal.submitter} alwaysTrim={false} />
</dd>

{(totalVotes || totalVotesLoading || totalVotesProblematic) && (
Expand Down
29 changes: 29 additions & 0 deletions src/oasis-nexus/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,35 @@ export type ExtendedValidatorList = generated.ValidatorList & {
map?: Map<string, generated.Validator>
}

export type ValidatorAddressNameMap = { [oasisAddress: string]: string | undefined }

export const useGetConsensusValidatorsAddressNameMap: typeof generated.useGetConsensusValidators<
AxiosResponse<ValidatorAddressNameMap>
> = (network, params?, options?) => {
return generated.useGetConsensusValidators(network, params, {
...options,
query: {
staleTime: options?.query?.staleTime ?? 5 * 60 * 1000, // Defaults to 5 minutes
...options?.query,
},
request: {
...options?.request,
transformResponse: [
...arrayify(axios.defaults.transformResponse),
(data: generated.ValidatorList, headers, status) => {
if (status !== 200) return data
const validators: ValidatorAddressNameMap = {}
data.validators.forEach(validator => {
validators[validator.entity_address] = validator.media?.name
})
return validators
},
...arrayify(options?.request?.transformResponse),
],
},
})
}

export const useGetConsensusValidators: typeof generated.useGetConsensusValidators = (
network,
params?,
Expand Down

0 comments on commit f2fe95a

Please sign in to comment.