Skip to content
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

Merged
merged 14 commits into from
Jun 20, 2024
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"@cosmjs/tendermint-rpc": "^0.32.1",
"@dydxprotocol/v4-abacus": "1.7.85",
"@dydxprotocol/v4-client-js": "^1.1.20",
"@dydxprotocol/v4-localization": "^1.1.127",
"@dydxprotocol/v4-localization": "^1.1.128",
"@ethersproject/providers": "^5.7.2",
"@hugocxl/react-to-image": "^0.0.9",
"@js-joda/core": "^5.5.3",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/components/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ const detailsLayoutVariants = {

const itemLayoutVariants = {
column: css`
isolation: isolate;

${layoutMixins.scrollArea}

${layoutMixins.stickyArea0}
Expand Down
36 changes: 36 additions & 0 deletions src/components/DropdownIcon.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice one

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);
`}
`;
4 changes: 3 additions & 1 deletion src/components/FormMaxInputToggleButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ElementProps = {
};

export const FormMaxInputToggleButton = ({
size = ButtonSize.XSmall,
size = ButtonSize.Small,
isInputEmpty,
isLoading,
onPressedChange,
Expand All @@ -42,4 +42,6 @@ const $FormMaxInputToggleButton = styled(ToggleButton)`
${formMixins.inputInnerToggleButton}

--button-padding: 0 0.5rem;
--button-backgroundColor: var(--color-accent);
--button-textColor: var(--color-text-2);
`;
3 changes: 3 additions & 0 deletions src/components/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type StyleProps = {
className?: string;
fullWidth?: boolean;
noBlur?: boolean;
align?: 'start' | 'center' | 'end';
sideOffset?: number;
triggerType?: TriggerType;
withPortal?: boolean;
Expand All @@ -38,6 +39,7 @@ export const Popover = ({
onOpenChange,
slotTrigger,
slotAnchor,
align = 'center',
sideOffset,
fullWidth,
noBlur,
Expand All @@ -59,6 +61,7 @@ export const Popover = ({
$noBlur={noBlur}
className={className}
sideOffset={sideOffset}
align={align}
>
{children}
</$Content>
Expand Down
19 changes: 4 additions & 15 deletions src/components/SearchSelectMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRef, useState, type ReactNode } from 'react';

import styled, { css } from 'styled-components';
import styled from 'styled-components';

import { type MenuConfig } from '@/constants/menus';

Expand All @@ -12,13 +12,14 @@ import { layoutMixins } from '@/styles/layoutMixins';

import { ComboboxMenu } from '@/components/ComboboxMenu';
import { type DetailsItem } from '@/components/Details';
import { Icon, IconName } from '@/components/Icon';
import { Popover, TriggerType } from '@/components/Popover';
import { WithDetailsReceipt } from '@/components/WithDetailsReceipt';
import { WithLabel } from '@/components/WithLabel';

import { getSimpleStyledOutputType } from '@/lib/genericFunctionalComponentUtils';

import { DropdownIcon } from './DropdownIcon';

type ElementProps = {
asChild?: boolean;
children: ReactNode;
Expand Down Expand Up @@ -58,7 +59,7 @@ export const SearchSelectMenu = ({
) : (
<$MenuTrigger>
{label ? <$WithLabel label={label}>{children}</$WithLabel> : children}
<$TriggerIcon iconName={IconName.Triangle} open={open} />
<DropdownIcon isOpen={open} />
</$MenuTrigger>
);

Expand Down Expand Up @@ -153,15 +154,3 @@ const $ComboboxMenu = styled(ComboboxMenu)<ComboboxMenuStyleProps>`
max-height: 30vh;
overflow: auto;
` as typeof ComboboxMenuStyleType;

const $TriggerIcon = styled(Icon)<{ open?: boolean }>`
width: 0.625rem;
height: 0.375rem;
color: var(--color-text-0);

${({ open }) =>
open &&
css`
transform: rotate(180deg);
`}
`;
25 changes: 15 additions & 10 deletions src/components/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,8 @@ const TableColumnHeader = <TableRowData extends BaseTableRowData>({
aria-hidden="true"
sortDirection={
state.sortDescriptor?.column === column.key
? state.sortDescriptor?.direction
: undefined
? state.sortDescriptor?.direction ?? 'none'
: 'none'
}
>
<Icon iconName={IconName.Triangle} aria-hidden="true" />
Expand Down Expand Up @@ -857,7 +857,7 @@ const $Td = styled.td`
}
`;

const $SortArrow = styled.span<{ sortDirection?: 'ascending' | 'descending' }>`
const $SortArrow = styled.span<{ sortDirection: 'ascending' | 'descending' | 'none' }>`
float: right;
margin-left: auto;

Expand All @@ -868,13 +868,18 @@ const $SortArrow = styled.span<{ sortDirection?: 'ascending' | 'descending' }>`

font-size: 0.375em;

${$Th}[aria-sort="none"] & {
visibility: hidden;
}

${$Th}[aria-sort="ascending"] & {
transform: scaleY(-1);
}
${({ sortDirection }) =>
({
ascending: css`
transform: scaleY(-1);
`,
descending: css`
transform: scaleY(1);
`,
none: css`
visibility: hidden;
`,
})[sortDirection]}
`;

const $Thead = styled.thead<TableStyleProps>`
Expand Down
185 changes: 185 additions & 0 deletions src/components/ValidatorDropdown.tsx
Copy link
Contributor

Choose a reason for hiding this comment

The 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 SearchSelectMenu since it would give us the search functionality for free, and more designed as a popover UI. But obviously won't column have sorting, which I thought was less important. But just throwing it out there.

Copy link
Contributor Author

@moo-onthelawn moo-onthelawn Jun 20, 2024

Choose a reason for hiding this comment

The 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 SearchSelectMenu. Tbh I was basing my implementation on the MarketsDropdown; I think there is opportunity to update SearchSelectMenu to be able to handle rendering something with a table (optionally) and then having MarketsDropdown/ValidatorDropdown reuse it as well // maybe one day I'll get around it to it 🫠

Copy link
Contributor

Choose a reason for hiding this comment

The 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),
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
`;
Loading
Loading