Skip to content

Commit

Permalink
Merge pull request #81 from AudiusProject/jowlee-fix-user-sort
Browse files Browse the repository at this point in the history
Add user sortby
  • Loading branch information
jowlee authored and michellebrier committed Oct 9, 2023
1 parent f81c4c3 commit 2f31d36
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Table from 'components/Table'
import { formatWeight } from 'utils/format'

import { useUsers } from 'store/cache/user/hooks'
import { Address, Status } from 'types'
import { Address, SortUser, Status } from 'types'
import { usePushRoute } from 'utils/effects'
import { useIsMobile } from 'utils/hooks'
import getActiveStake from 'utils/activeStake'
Expand Down Expand Up @@ -58,7 +58,7 @@ const TopAddressesTable: React.FC<TopAddressesTableProps> = ({
[pushRoute]
)

const { status, users } = useUsers()
const { status, users } = useUsers({ sortBy: SortUser.activeStake })
let columns = [{ title: 'Rank', className: styles.rankColumn }]
if (!isMobile) {
columns = columns.concat([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import styles from './TopOperatorsTable.module.css'
import Table from 'components/Table'

import { useUsers } from 'store/cache/user/hooks'
import { Address, Operator, Status } from 'types'
import { Address, Operator, SortUser, Status } from 'types'
import { usePushRoute } from 'utils/effects'
import { useIsMobile } from 'utils/hooks'
import getActiveStake, { getTotalActiveDelegatedStake } from 'utils/activeStake'
Expand Down Expand Up @@ -56,7 +56,11 @@ const TopOperatorsTable: React.FC<TopOperatorsTableProps> = ({
[pushRoute]
)

const { status, users } = useUsers({ limit, filter: 'isOperator' })
const { status, users } = useUsers({
limit,
filter: 'isOperator',
sortBy: SortUser.stakePlusDelegates
})

let columns = [{ title: 'Rank', className: styles.rankColumn }]
if (!isMobile) {
Expand Down
29 changes: 21 additions & 8 deletions packages/protocol-dashboard/src/store/cache/user/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from '../contentNode/hooks'
import { useAccountUser } from 'store/account/hooks'
import { GetPendingDecreaseStakeRequestResponse } from 'services/Audius/service-provider/types'
import getActiveStake from 'utils/activeStake'
import getActiveStake, { getTotalActiveDelegatedStake } from 'utils/activeStake'
import {
useUser as useGraphUser,
useUsers as useGraphUsers
Expand All @@ -46,6 +46,21 @@ export const getStatus = (state: AppState) => state.cache.user.status
export const getUser = (wallet: Address) => (state: AppState) =>
state.cache.user.accounts[wallet]

const sortActiveStakeFunc = (u1: User | Operator, u2: User | Operator) => {
let u1Total = getActiveStake(u1)
let u2Total = getActiveStake(u2)
return u2Total.cmp(u1Total)
}

const sortStakePlusDelegatedFunc = (
u1: User | Operator,
u2: User | Operator
) => {
const u1Total = getActiveStake(u1).add(getTotalActiveDelegatedStake(u1))
const u2Total = getActiveStake(u2).add(getTotalActiveDelegatedStake(u2))
return u2Total.cmp(u1Total)
}

export const getUsers = ({ sortBy, limit, filter }: UseUsersProp) => (
state: AppState
) => {
Expand All @@ -57,14 +72,12 @@ export const getUsers = ({ sortBy, limit, filter }: UseUsersProp) => (
return true
}

const sortFunc = (u1: User | Operator, u2: User | Operator) => {
let u1Total = getActiveStake(u1)
let u2Total = getActiveStake(u2)
return u2Total.cmp(u1Total)
}

let serviceProviders: (User | Operator)[] = accounts.filter(filterFunc) as any
serviceProviders = serviceProviders.sort(sortFunc)
if (sortBy === SortUser.activeStake) {
serviceProviders = serviceProviders.sort(sortActiveStakeFunc)
} else if (sortBy === SortUser.stakePlusDelegates) {
serviceProviders = serviceProviders.sort(sortStakePlusDelegatedFunc)
}
if (limit) serviceProviders = serviceProviders.slice(0, limit)

return serviceProviders
Expand Down
3 changes: 2 additions & 1 deletion packages/protocol-dashboard/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ export enum SortNode {
}

export enum SortUser {
Token = 'Token'
activeStake = 'active-stake',
stakePlusDelegates = 'stake-plus-delegated'
}

export enum PendingTransactionName {
Expand Down

0 comments on commit 2f31d36

Please sign in to comment.