Skip to content

Commit

Permalink
Namadillo: Adding sortable headers to tables (#986)
Browse files Browse the repository at this point in the history
* feat(components): adding sorting capability to StyledTableHeader

* feat: implementing sortable headers

* feat(namadillo): adding sorting to headers

* fix: ops. rolling back changes on config.toml

* refactor(namadillo): reusing hook instead of a custom function

* feat(components): enabling third state for sortable table headers
  • Loading branch information
pedrorezende authored Aug 8, 2024
1 parent eac7ec8 commit a46d927
Show file tree
Hide file tree
Showing 11 changed files with 373 additions and 55 deletions.
26 changes: 19 additions & 7 deletions apps/namadillo/src/App/Staking/AllValidatorsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { atomsAreLoading, atomsAreNotInitialized } from "atoms/utils";
import { allValidatorsAtom } from "atoms/validators";
import BigNumber from "bignumber.js";
import { useValidatorFilter } from "hooks/useValidatorFilter";
import { useValidatorTableSorting } from "hooks/useValidatorTableSorting";
import { useAtomValue } from "jotai";
import { useState } from "react";
import { useNavigate } from "react-router-dom";
Expand All @@ -32,23 +33,34 @@ export const AllValidatorsTable = ({
const isConnected = useAtomValue(namadaExtensionConnectedAtom);
const navigate = useNavigate();
const [filter, setFilter] = useState("");

const filteredValidators = useValidatorFilter({
validators: validators.isSuccess ? validators.data : [],
myValidatorsAddresses: [],
searchTerm: filter,
onlyMyValidators: false,
});

const { sortedValidators: sortedAndFilteredValidators, sortableColumns } =
useValidatorTableSorting({
validators: filteredValidators,
stakedAmountByAddress: {},
});

const headers = [
"",
"Validator",
"Address",
<div key={`all-validators-voting-power`} className="text-right">
Voting Power
</div>,
<div key={`all-validators-comission`} className="text-right">
Commission
</div>,
{
children: "Voting Power",
className: "text-right",
...sortableColumns["votingPowerInNAM"],
},
{
children: "Comission",
className: "text-right",
...sortableColumns["commission"],
},
];

const renderRow = (validator: Validator): TableRow => ({
Expand Down Expand Up @@ -123,7 +135,7 @@ export const AllValidatorsTable = ({
<div className="flex flex-col h-[490px] overflow-hidden">
<ValidatorsTable
id="all-validators"
validatorList={filteredValidators}
validatorList={sortedAndFilteredValidators}
headers={headers}
initialPage={initialPage}
resultsPerPage={resultsPerPage}
Expand Down
19 changes: 16 additions & 3 deletions apps/namadillo/src/App/Staking/IncrementBondingTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NamCurrency } from "App/Common/NamCurrency";
import { NamInput } from "App/Common/NamInput";
import BigNumber from "bignumber.js";
import clsx from "clsx";
import { useValidatorTableSorting } from "hooks/useValidatorTableSorting";
import { twMerge } from "tailwind-merge";
import { Validator } from "types";
import { ValidatorCard } from "./ValidatorCard";
Expand All @@ -24,6 +25,11 @@ export const IncrementBondingTable = ({
onChangeValidatorAmount,
resultsPerPage = 100,
}: IncrementBondingTableProps): JSX.Element => {
const { sortableColumns, sortedValidators } = useValidatorTableSorting({
validators,
stakedAmountByAddress,
});

const headers = [
{ children: "Validator" },
"Amount to Stake",
Expand All @@ -35,9 +41,16 @@ export const IncrementBondingTable = ({
</div>
),
className: "text-right",
...sortableColumns["stakedAmount"],
},
{
children: <div className="w-full text-right">Voting Power</div>,
...sortableColumns["votingPowerInNAM"],
},
{
children: <div className="w-full text-right">Commission</div>,
...sortableColumns["commission"],
},
{ children: "Voting Power", className: "text-right" },
{ children: "Commission", className: "text-right" },
];

const renderRow = (validator: Validator): TableRow => {
Expand Down Expand Up @@ -136,7 +149,7 @@ export const IncrementBondingTable = ({
<ValidatorsTable
id="increment-bonding-table"
tableClassName="flex-1 overflow-auto mt-2"
validatorList={validators}
validatorList={sortedValidators}
updatedAmountByAddress={updatedAmountByAddress}
headers={headers}
renderRow={renderRow}
Expand Down
65 changes: 41 additions & 24 deletions apps/namadillo/src/App/Staking/MyValidatorsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,59 @@ import { NamCurrency } from "App/Common/NamCurrency";
import { WalletAddress } from "App/Common/WalletAddress";
import { myValidatorsAtom } from "atoms/validators";
import BigNumber from "bignumber.js";
import { useValidatorTableSorting } from "hooks/useValidatorTableSorting";
import { useAtomValue } from "jotai";
import { useNavigate } from "react-router-dom";
import { MyValidator, Validator } from "types";
import { Address, MyValidator, Validator } from "types";
import { ValidatorCard } from "./ValidatorCard";
import { ValidatorsTable } from "./ValidatorsTable";
import StakingRoutes from "./routes";

export const MyValidatorsTable = (): JSX.Element => {
const navigate = useNavigate();
const myValidators = useAtomValue(myValidatorsAtom);
const myValidatorsObj: Record<string, MyValidator> =

const validators =
myValidators.isSuccess ?
myValidators.data.map((v: MyValidator) => v.validator)
: [];

const stakedAmountByAddress: Record<Address, BigNumber> =
myValidators.isSuccess ?
myValidators.data.reduce(
(acc: Record<string, MyValidator>, current: MyValidator) => {
return { ...acc, [current.validator.address]: current };
},
{}
)
myValidators.data.reduce((prev, current) => {
return {
...prev,
[current.validator.address]: current.stakedAmount,
};
}, {})
: {};

const { sortableColumns, sortedValidators } = useValidatorTableSorting({
validators,
stakedAmountByAddress,
});

const head = [
"My Validators",
"Address",
<div key="my-validators-staked-amount" className="text-right">
Staked Amount
</div>,
<div key="my-validators-vp" className="text-right">
Voting Power
</div>,
<div key="my-validators-comission" className="text-right">
Commission
</div>,
{
children: "Staked Amount",
className: "text-right",
...sortableColumns["stakedAmount"],
},
{
children: "Voting Power",
className: "text-right",
...sortableColumns["votingPowerInNAM"],
},
{
children: "Commission",
className: "text-right",
...sortableColumns["commission"],
},
];

const renderRow = (validator: Validator): TableRow => {
const stakedAmount = myValidatorsObj[validator.address].stakedAmount;
return {
className: "",
cells: [
Expand All @@ -57,7 +74,11 @@ export const MyValidatorsTable = (): JSX.Element => {
key={`my-validator-currency-${validator.address}`}
className="text-right leading-tight"
>
<NamCurrency amount={stakedAmount || new BigNumber(0)} />
<NamCurrency
amount={
stakedAmountByAddress[validator.address] || new BigNumber(0)
}
/>
</div>,
<div
className="flex flex-col text-right leading-tight"
Expand Down Expand Up @@ -126,11 +147,7 @@ export const MyValidatorsTable = (): JSX.Element => {
<ValidatorsTable
id="my-validators"
tableClassName="mt-2"
validatorList={
myValidators.isSuccess ?
myValidators.data.map((v: MyValidator) => v.validator)
: []
}
validatorList={sortedValidators}
headers={head}
renderRow={renderRow}
/>
Expand Down
21 changes: 18 additions & 3 deletions apps/namadillo/src/App/Staking/ReDelegateTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NamCurrency } from "App/Common/NamCurrency";
import { NamInput } from "App/Common/NamInput";
import BigNumber from "bignumber.js";
import clsx from "clsx";
import { useValidatorTableSorting } from "hooks/useValidatorTableSorting";
import { twMerge } from "tailwind-merge";
import { Validator } from "types";
import { ValidatorCard } from "./ValidatorCard";
Expand All @@ -27,6 +28,11 @@ export const ReDelegateTable = ({
renderInfoColumn,
onChangeValidatorAmount,
}: IncrementBondingTableProps): JSX.Element => {
const { sortableColumns, sortedValidators } = useValidatorTableSorting({
validators,
stakedAmountByAddress,
});

const headers = [
{ children: "Validator" },
"Amount to Re-delegate",
Expand All @@ -40,9 +46,18 @@ export const ReDelegateTable = ({
</div>
),
className: "text-right",
...sortableColumns["stakedAmount"],
},
{
children: "Voting Power",
className: "text-right",
...sortableColumns["votingPowerInNAM"],
},
{
children: "Commission",
className: "text-right",
...sortableColumns["commission"],
},
{ children: "Voting Power", className: "text-right" },
{ children: "Commission", className: "text-right" },
];

const renderRow = (validator: Validator): TableRow => {
Expand Down Expand Up @@ -128,7 +143,7 @@ export const ReDelegateTable = ({
<ValidatorsTable
id="increment-bonding-table"
tableClassName="mt-2"
validatorList={validators}
validatorList={sortedValidators}
updatedAmountByAddress={updatedAmountByAddress}
headers={headers}
renderRow={renderRow}
Expand Down
47 changes: 36 additions & 11 deletions apps/namadillo/src/App/Staking/UnstakeBondingTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NamCurrency } from "App/Common/NamCurrency";
import { NamInput } from "App/Common/NamInput";
import BigNumber from "bignumber.js";
import clsx from "clsx";
import { useValidatorTableSorting } from "hooks/useValidatorTableSorting";
import { twMerge } from "tailwind-merge";
import { MyValidator, Validator } from "types";
import { ValidatorCard } from "./ValidatorCard";
Expand All @@ -22,19 +23,43 @@ export const UnstakeBondingTable = ({
stakedAmountByAddress,
onChangeValidatorAmount,
}: UnstakeBondingTableProps): JSX.Element => {
const validators = myValidators.map((mv) => mv.validator);

const { sortableColumns, sortedValidators } = useValidatorTableSorting({
validators,
stakedAmountByAddress,
});

const headers = [
{ children: "Validator" },
"Amount to Unstake",
<div key={`unstake-new-total`} className="text-right">
<span className="block">Stake</span>
<small className="text-xs text-neutral-500 block">New total Stake</small>
</div>,
<div key={`unstake-voting-power`} className="text-right">
Voting Power
</div>,
<div key={`unstake-commission`} className="text-right">
Commission
</div>,
{
children: (
<div key={`unstake-new-total`} className="text-right">
<span className="block">Stake</span>
<small className="text-xs text-neutral-500 block">
New total Stake
</small>
</div>
),
...sortableColumns["stakedAmount"],
},
{
children: (
<div key={`unstake-voting-power`} className="text-right">
Voting Power
</div>
),
...sortableColumns["stakedAmount"],
},
{
children: (
<div key={`unstake-commission`} className="text-right">
Commission
</div>
),
...sortableColumns["commission"],
},
];

const renderRow = (validator: Validator): TableRow => {
Expand Down Expand Up @@ -127,7 +152,7 @@ export const UnstakeBondingTable = ({
<ValidatorsTable
id="increment-bonding-table"
tableClassName="mt-2"
validatorList={myValidators.map((mv) => mv.validator)}
validatorList={sortedValidators}
headers={headers}
renderRow={renderRow}
/>
Expand Down
Loading

0 comments on commit a46d927

Please sign in to comment.