-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(staking): ui of validator selection #691
Changes from 7 commits
fd68017
a3fd7b5
4724e2d
85526cd
1cc6700
9cd7ff7
cf6be37
574732d
42c7e8c
58110ae
7c05d1a
cbb0830
ac315b5
56b7cfe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import styled, { css } from 'styled-components'; | ||
|
||
import { Icon, IconName } from '@/components/Icon'; | ||
|
||
type ElementProps = { | ||
iconName?: IconName; | ||
isOpen?: boolean; | ||
}; | ||
|
||
type StyleProps = { | ||
className?: string; | ||
}; | ||
|
||
export const DropdownIcon = ({ | ||
iconName = IconName.Triangle, | ||
isOpen, | ||
className, | ||
}: ElementProps & StyleProps) => { | ||
return ( | ||
<$DropdownIcon aria-hidden="true" isOpen={isOpen} className={className}> | ||
<Icon iconName={iconName} aria-hidden="true" /> | ||
</$DropdownIcon> | ||
); | ||
}; | ||
|
||
const $DropdownIcon = styled.span<{ isOpen?: boolean }>` | ||
display: inline-flex; | ||
transition: transform 0.3s var(--ease-out-expo); | ||
font-size: 0.375em; | ||
|
||
${({ isOpen }) => | ||
isOpen && | ||
css` | ||
transform: scaleY(-1); | ||
`} | ||
`; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is implemented already so don't sweat it, and I think this approach is totally valid. Originally I was thinking we could have leveraged There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ooh good point; yeah my understanding of product was that sortability seemed more important than search but may have been easier to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All good! I think this reasoning make sense too |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
import { memo, useMemo, useState } from 'react'; | ||
|
||
import { Validator } from '@dydxprotocol/v4-client-js/build/node_modules/@dydxprotocol/v4-proto/src/codegen/cosmos/staking/v1beta1/staking'; | ||
import styled from 'styled-components'; | ||
|
||
import { STRING_KEYS } from '@/constants/localization'; | ||
import { ValidatorData } from '@/constants/validators'; | ||
|
||
import { useStakingValidator } from '@/hooks/useStakingValidator'; | ||
import { useStringGetter } from '@/hooks/useStringGetter'; | ||
import { useTokenConfigs } from '@/hooks/useTokenConfigs'; | ||
|
||
import { layoutMixins } from '@/styles/layoutMixins'; | ||
import { popoverMixins } from '@/styles/popoverMixins'; | ||
|
||
import { DropdownIcon } from '@/components/DropdownIcon'; | ||
import { Icon, IconName } from '@/components/Icon'; | ||
import { Link } from '@/components/Link'; | ||
import { Output, OutputType } from '@/components/Output'; | ||
import { Popover, TriggerType } from '@/components/Popover'; | ||
import { ColumnDef, Table } from '@/components/Table'; | ||
import { TableCell } from '@/components/Table/TableCell'; | ||
import { Tag } from '@/components/Tag'; | ||
import { ValidatorFaviconIcon } from '@/components/ValidatorName'; | ||
|
||
import { MustBigNumber } from '@/lib/numbers'; | ||
|
||
const ValidatorsDropdownContent = ({ | ||
availableValidators, | ||
}: { | ||
availableValidators: Validator[]; | ||
}) => { | ||
const stringGetter = useStringGetter(); | ||
const { chainTokenLabel } = useTokenConfigs(); | ||
|
||
const columns = useMemo( | ||
() => | ||
[ | ||
{ | ||
columnKey: 'commission', | ||
getCellValue: (row) => row.commissionRate.toNumber(), | ||
label: stringGetter({ key: STRING_KEYS.VALIDATOR }), | ||
allowsSorting: true, | ||
renderCell: ({ website, name, commissionRate }) => ( | ||
<TableCell | ||
stacked | ||
stackedWithSecondaryStyling | ||
slotLeft={<ValidatorFaviconIcon url={website} fallbackText={name} />} | ||
> | ||
<span>{name}</span> | ||
<span> | ||
{stringGetter({ | ||
key: STRING_KEYS.COMMISSION_PERCENTAGE, | ||
params: { | ||
PERCENTAGE: ( | ||
<$CommissionOutput type={OutputType.Percent} value={commissionRate} /> | ||
), | ||
}, | ||
})} | ||
</span> | ||
</TableCell> | ||
), | ||
}, | ||
{ | ||
columnKey: 'votingPower', | ||
getCellValue: (row) => row.votingPower.toNumber(), | ||
allowsSorting: true, | ||
label: stringGetter({ key: STRING_KEYS.VOTING_POWER }), | ||
tag: <Tag> {chainTokenLabel} </Tag>, | ||
renderCell: ({ votingPower }) => <Output type={OutputType.Number} value={votingPower} />, | ||
}, | ||
] satisfies ColumnDef<ValidatorData>[], | ||
[stringGetter, chainTokenLabel] | ||
); | ||
|
||
const filteredValidators = availableValidators.reduce((validators: ValidatorData[], val) => { | ||
if (val.description) { | ||
validators.push({ | ||
name: val.description.moniker, | ||
operatorAddress: val.operatorAddress, | ||
votingPower: MustBigNumber(val.delegatorShares).div(10 ** 36), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops forgot to update these hardcoded vals (36; 16) will do |
||
commissionRate: MustBigNumber(val.commission?.commissionRates?.rate).div(10 ** 16), | ||
website: val.description?.website, | ||
}); | ||
} | ||
return validators; | ||
}, []); | ||
|
||
return ( | ||
<$ScrollArea> | ||
<$Table | ||
key="validators" | ||
label="Validators" | ||
data={filteredValidators} | ||
getRowKey={(row: ValidatorData) => row.operatorAddress} | ||
// onRowAction // TODO: OTE-449 | ||
columns={columns} | ||
defaultSortDescriptor={{ | ||
column: 'commission', | ||
direction: 'ascending', | ||
}} | ||
paginationBehavior="showAll" | ||
slotEmpty={ | ||
<> | ||
<Icon iconName={IconName.OrderPending} /> | ||
<h4> | ||
{ | ||
'There are no validators currently available.' // TODO: localize | ||
} | ||
</h4> | ||
</> | ||
} | ||
/> | ||
</$ScrollArea> | ||
); | ||
}; | ||
|
||
export const ValidatorDropdown = memo(() => { | ||
const { selectedValidator, availableValidators } = useStakingValidator() ?? {}; | ||
|
||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
const output = ( | ||
<$Output | ||
type={OutputType.Text} | ||
value={selectedValidator?.description?.moniker} | ||
slotLeft={ | ||
<ValidatorFaviconIcon | ||
url={selectedValidator?.description?.website} | ||
fallbackText={selectedValidator?.description?.moniker} | ||
/> | ||
} | ||
slotRight={<$DropdownIcon iconName={IconName.Caret} isOpen={isOpen} />} | ||
/> | ||
); | ||
|
||
const slotTrigger = selectedValidator?.description?.website ? ( | ||
<Link href={selectedValidator?.description?.website} withIcon> | ||
{output} | ||
</Link> | ||
) : ( | ||
output | ||
); | ||
|
||
return ( | ||
<$Popover | ||
open={isOpen} | ||
onOpenChange={setIsOpen} | ||
slotTrigger={slotTrigger} | ||
triggerType={TriggerType.MarketDropdown} | ||
align="end" | ||
sideOffset={8} | ||
withPortal | ||
> | ||
<ValidatorsDropdownContent availableValidators={availableValidators ?? []} /> | ||
</$Popover> | ||
); | ||
}); | ||
|
||
const $ScrollArea = styled.div` | ||
${layoutMixins.scrollArea} | ||
|
||
max-height: 20rem; | ||
`; | ||
|
||
const $DropdownIcon = styled(DropdownIcon)` | ||
margin-left: 0.5rem; | ||
`; | ||
|
||
const $Popover = styled(Popover)` | ||
${popoverMixins.popover} | ||
`; | ||
|
||
const $Table = styled(Table)` | ||
--tableRow-backgroundColor: var(--color-layer-4); | ||
--tableCell-padding: 0.5rem 1rem; | ||
` as typeof Table; | ||
|
||
const $Output = styled(Output)` | ||
color: var(--color-text-1); | ||
`; | ||
|
||
const $CommissionOutput = styled(Output)` | ||
display: inline; | ||
`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice one