Skip to content

Commit

Permalink
update sort validator with votingpower and uptime
Browse files Browse the repository at this point in the history
  • Loading branch information
haunv3 committed May 16, 2024
1 parent 4400e33 commit ab40c77
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/components/ValidatorList/ValidatorTable/ValidatorTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ const toggleDirection = direction => {
};

const ValidatorTable = memo(({ data = [] }) => {
const [sortField, setSortField] = useState(sortFields.SELFBONDED);
const [sortField, setSortField] = useState(sortFields.UPTIME);
const [sortDirection, setSortDirection] = useState(sortDirections.DESC);
const [canSort, setCanSort] = useState(false);
const [isFirstSort, setIsFirstSort] = useState(true);

const totalVotingPower = useMemo(() => computeTotalVotingPower(data), [data]);

Expand All @@ -88,6 +89,7 @@ const ValidatorTable = memo(({ data = [] }) => {
const sortBy = field => {
// trigger can sort to true so the list can be sorted
if (!canSort) setCanSort(true);
if (isFirstSort) setIsFirstSort(false);
if (field === sortField) {
setSortDirection(toggleDirection(sortDirection));
} else {
Expand Down Expand Up @@ -211,8 +213,20 @@ const ValidatorTable = memo(({ data = [] }) => {
};

const sortData = (data, extraSortField = sortFields.RANK) => {
if (!data) {
return [];
if (!data) return [];

if (isFirstSort) {
return [...data]
.map(e => {
return { ...e, votingPowerMixUpTime: e.voting_power * e.uptime };
})
.sort(function(a, b) {
if (a["votingPowerMixUpTime"] === b["votingPowerMixUpTime"]) {
return compareTwoValues(a["votingPowerMixUpTime"], b["votingPowerMixUpTime"], toggleDirection(sortDirection));
} else {
return compareTwoValues(a["votingPowerMixUpTime"], b["votingPowerMixUpTime"], sortDirection);
}
});
}

if (canSort) {
Expand Down

0 comments on commit ab40c77

Please sign in to comment.