Skip to content

Commit

Permalink
filter inactive positions
Browse files Browse the repository at this point in the history
  • Loading branch information
saml33 committed Sep 15, 2023
1 parent b75b116 commit 839be09
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
29 changes: 26 additions & 3 deletions components/Positions.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import useMangoGroup from 'hooks/useMangoGroup'
import { useMemo } from 'react'
import { STAKEABLE_TOKENS } from 'utils/constants'
import { SHOW_INACTIVE_POSITIONS_KEY, STAKEABLE_TOKENS } from 'utils/constants'
import TokenLogo from './shared/TokenLogo'
import Button from './shared/Button'
import { formatTokenSymbol } from 'utils/tokens'
import mangoStore from '@store/mangoStore'
import Switch from './forms/Switch'
import useLocalStorageState from 'hooks/useLocalStorageState'

const set = mangoStore.getState().set

Expand All @@ -14,6 +16,8 @@ const Positions = ({
setActiveTab: (tab: string) => void
}) => {
const { group } = useMangoGroup()
const [showInactivePositions, setShowInactivePositions] =
useLocalStorageState(SHOW_INACTIVE_POSITIONS_KEY, true)

const banks = useMemo(() => {
if (!group) return []
Expand All @@ -35,8 +39,16 @@ const Positions = ({
}
positions.push({ balance, bank })
}
return positions.sort((a, b) => b.balance - a.balance)
}, [banks])
const sortedPositions = positions.sort((a, b) => b.balance - a.balance)
return showInactivePositions
? sortedPositions
: sortedPositions.filter((pos) => pos.balance > 0)
}, [banks, showInactivePositions])

const numberOfPositions = useMemo(() => {
if (!positions.length) return 0
return positions.filter((pos) => pos.balance > 0).length
}, [positions])

const handleAddOrManagePosition = (token: string) => {
setActiveTab('Stake')
Expand All @@ -47,6 +59,17 @@ const Positions = ({

return positions.length ? (
<div className="space-y-3">
<div className="mb-4 flex items-center justify-between">
<p>{`You have ${numberOfPositions} active position${
numberOfPositions > 1 ? 's' : ''
}`}</p>
<Switch
checked={showInactivePositions}
onChange={(checked) => setShowInactivePositions(checked)}
>
Show Inactive
</Switch>
</div>
{positions.map((position, i) => {
const { balance, bank } = position
return bank ? (
Expand Down
2 changes: 1 addition & 1 deletion components/forms/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Switch: FunctionComponent<SwitchProps> = ({

return (
<div className={`flex items-center ${className}`}>
<span className="mr-2 text-xs text-th-fgd-2">{children}</span>
<span className="mr-2 text-th-fgd-1">{children}</span>
<button
type="button"
className={`${
Expand Down
2 changes: 2 additions & 0 deletions utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { PublicKey } from '@metaplex-foundation/js'

// lev stake
export const STAKEABLE_TOKENS = ['MSOL', 'JitoSOL', 'stSOL', 'bSOL', 'LDO']

export const SHOW_INACTIVE_POSITIONS_KEY = 'showInactivePositions-0.1'
// end

export const LAST_ACCOUNT_KEY = 'mangoAccount-0.4'
Expand Down

0 comments on commit 839be09

Please sign in to comment.