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

fix: portfolio table sorting #334

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,14 @@ export function PoolActivityTable() {

if (!isMounted) return <Skeleton height="500px" w="full" />

const items = sortedPoolEvents.map(item => ({
...item,
id: item[2].id,
}))

return (
<>
<Box className="hide-scrollbar" overflowX="auto" w="full">
<Box minWidth="800px">
<PaginatedTable
alignItems="flex-start"
items={items}
getRowId={([, , poolActivityMetaData]) => poolActivityMetaData.id}
items={sortedPoolEvents}
loading={isLoading}
noItemsFoundLabel="No pool events found"
paginationProps={paginationProps}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function PoolListTable({ pools, count, loading }: Props) {
w={{ base: '100vw', lg: 'full' }}
>
<PaginatedTable
getRowId={item => item.id}
alter-eggo marked this conversation as resolved.
Show resolved Hide resolved
items={pools}
loading={loading}
noItemsFoundLabel="No pools found"
Expand Down
17 changes: 13 additions & 4 deletions packages/lib/modules/portfolio/PortfolioTable/PortfolioTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useUserAccount } from '../../web3/UserAccountProvider'
import { ConnectWallet } from '../../web3/ConnectWallet'
import { getCanStake } from '../../pool/actions/stake.helpers'
import { getProjectConfig, isBalancerProject } from '@repo/lib/config/getProjectConfig'
import { bn } from '@repo/lib/shared/utils/numbers'

export type PortfolioTableSortingId = 'staking' | 'vebal' | 'liquidity' | 'apr'
export interface PortfolioSortingData {
Expand Down Expand Up @@ -131,10 +132,17 @@ export function PortfolioTable() {
}

if (currentSortingObj.id === 'apr') {
const [aApr] = getTotalApr(a.dynamicData.aprItems)
const [bApr] = getTotalApr(b.dynamicData.aprItems)

return currentSortingObj.desc ? bApr.minus(aApr).toNumber() : aApr.minus(bApr).toNumber()
const [aApr] =
a.poolType === ExpandedPoolType.StakedAura
? [a.staking?.aura?.apr ?? 0]
: getTotalApr(a.dynamicData.aprItems)
const [bApr] =
b.poolType === ExpandedPoolType.StakedAura
? [b.staking?.aura?.apr ?? 0]
: getTotalApr(b.dynamicData.aprItems)
return currentSortingObj.desc
? bn(bApr).minus(aApr).toNumber()
: bn(aApr).minus(bApr).toNumber()
}

return 0
Expand Down Expand Up @@ -163,6 +171,7 @@ export function PortfolioTable() {
>
<PaginatedTable
alignItems="flex-start"
getRowId={row => row.uniqueKey}
items={sortedPools}
left={{ base: '-4px', sm: '0' }}
loading={isLoadingPortfolio}
Expand Down
21 changes: 17 additions & 4 deletions packages/lib/modules/portfolio/PortfolioTable/useExpandedPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export enum ExpandedPoolType {
export type ExpandedPoolInfo = Pool & {
poolType: ExpandedPoolType
poolPositionUsd: number
uniqueKey: string
}

function generateUniqueKey(...args: string[]) {
return args.join(' - ')
}

export function useExpandedPools(pools: Pool[]) {
Expand All @@ -37,34 +42,42 @@ export function useExpandedPools(pools: Pool[]) {
const walletBalanceUsd = pool.userBalance?.walletBalanceUsd || 0

if (stakedBalancesBalUsd > 0) {
const poolType = isVeBal ? ExpandedPoolType.Locked : ExpandedPoolType.StakedBal
expandedPools.push({
...pool,
poolType: isVeBal ? ExpandedPoolType.Locked : ExpandedPoolType.StakedBal,
poolType,
poolPositionUsd: stakedBalancesBalUsd,
uniqueKey: generateUniqueKey(pool.id, poolType),
})
}

if (stakedBalancesAuraUsd > 0) {
const poolType = ExpandedPoolType.StakedAura
expandedPools.push({
...pool,
poolType: ExpandedPoolType.StakedAura,
poolType,
poolPositionUsd: stakedBalancesAuraUsd,
uniqueKey: generateUniqueKey(pool.id, poolType),
})
}

if (walletBalanceUsd > 0) {
const poolType = isVeBal ? ExpandedPoolType.Unlocked : ExpandedPoolType.Unstaked
expandedPools.push({
...pool,
poolType: isVeBal ? ExpandedPoolType.Unlocked : ExpandedPoolType.Unstaked,
poolType,
poolPositionUsd: walletBalanceUsd,
uniqueKey: generateUniqueKey(pool.id, poolType),
})
}

if (stakedBalancesBalUsd === 0 && stakedBalancesAuraUsd === 0 && walletBalanceUsd === 0) {
const poolType = ExpandedPoolType.Default
expandedPools.push({
...pool,
poolType: ExpandedPoolType.Default,
poolType,
poolPositionUsd: 0,
uniqueKey: generateUniqueKey(pool.id, poolType),
})
}
})
Expand Down
8 changes: 5 additions & 3 deletions packages/lib/shared/components/tables/PaginatedTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ interface Props<T> extends BoxProps {
showPagination: boolean
paginationProps: any // TODO: type this
noItemsFoundLabel: string
getRowId: (item: T, index: number) => React.Key
}

export function PaginatedTable({
export function PaginatedTable<T>({
items,
loading,
renderTableRow,
renderTableHeader,
showPagination,
paginationProps,
noItemsFoundLabel,
}: Props<any>) {
getRowId,
}: Props<T>) {
return (
<>
<VStack className="hide-scrollbar" overflowX="scroll" w="full">
Expand All @@ -29,7 +31,7 @@ export function PaginatedTable({
{items.length > 0 && (
<VStack gap="0">
{items.map((item, index) => (
<Box key={item.id} w="full">
<Box key={getRowId(item, index)} w="full">
{renderTableRow(item, index)}
</Box>
))}
Expand Down
Loading