Skip to content

Commit

Permalink
feat: Use useCanFastUnstake query (#2377)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossbulat authored Jan 2, 2025
1 parent 0c9051d commit 0d99cbb
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 455 deletions.
2 changes: 1 addition & 1 deletion packages/app/src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const RouterInner = () => {
return (
<ErrorBoundary FallbackComponent={ErrorFallbackApp}>
{pluginEnabled('staking_api') && !inSetup() && activeAccount && (
<StakingApi activeAccount={activeAccount} />
<StakingApi activeAccount={activeAccount} network={network} />
)}
<NotificationPrompts />
<Body>
Expand Down
24 changes: 24 additions & 0 deletions packages/app/src/StakingApi/FastUnstakeApi.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import { useFastUnstake } from 'contexts/FastUnstake'
import { useCanFastUnstake } from 'plugin-staking-api'
import { useEffect } from 'react'
import type { Props } from './types'

export const FastUnstakeApi = ({ activeAccount, network }: Props) => {
const { setFastUnstakeStatus } = useFastUnstake()
const { data, loading, error } = useCanFastUnstake({
chain: network,
who: activeAccount,
})

// Update fast unstake status on active account change. Must be bonding
useEffect(() => {
if (!loading && !error && data?.canFastUnstake) {
setFastUnstakeStatus(data.canFastUnstake)
}
}, [JSON.stringify(data?.canFastUnstake)])

return null
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
// SPDX-License-Identifier: GPL-3.0-only

import { useApi } from 'contexts/Api'
import { useNetwork } from 'contexts/Network'
import { usePayouts } from 'contexts/Payouts'
import { ApolloProvider, client, useUnclaimedRewards } from 'plugin-staking-api'
import { useUnclaimedRewards } from 'plugin-staking-api'
import { useEffect } from 'react'
import type { Props } from './types'

interface Props {
activeAccount: string
}

const Inner = ({ activeAccount }: Props) => {
export const UnclaimedRewardsApi = ({ activeAccount, network }: Props) => {
const { activeEra } = useApi()
const { network } = useNetwork()
const { setUnclaimedRewards } = usePayouts()

const { data, loading, error } = useUnclaimedRewards({
Expand All @@ -22,6 +17,7 @@ const Inner = ({ activeAccount }: Props) => {
fromEra: Math.max(activeEra.index.minus(1).toNumber(), 0),
})

// Update unclaimed rewards on total change
useEffect(() => {
if (!loading && !error && data?.unclaimedRewards) {
setUnclaimedRewards(data?.unclaimedRewards)
Expand All @@ -30,9 +26,3 @@ const Inner = ({ activeAccount }: Props) => {

return null
}

export const StakingApi = (props: Props) => (
<ApolloProvider client={client}>
<Inner {...props} />
</ApolloProvider>
)
28 changes: 28 additions & 0 deletions packages/app/src/StakingApi/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import { useFastUnstake } from 'contexts/FastUnstake'
import { useStaking } from 'contexts/Staking'
import { ApolloProvider, client } from 'plugin-staking-api'
import { useEffect } from 'react'
import { FastUnstakeApi } from './FastUnstakeApi'
import type { Props } from './types'
import { UnclaimedRewardsApi } from './UnclaimedRewardsApi'

export const StakingApi = (props: Props) => {
const { isBonding } = useStaking()
const { setFastUnstakeStatus } = useFastUnstake()

useEffect(() => {
if (!isBonding()) {
setFastUnstakeStatus(null)
}
}, [isBonding()])

return (
<ApolloProvider client={client}>
<UnclaimedRewardsApi {...props} />
{isBonding() && <FastUnstakeApi {...props} />}
</ApolloProvider>
)
}
9 changes: 9 additions & 0 deletions packages/app/src/StakingApi/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2024 @polkadot-cloud/polkadot-staking-dashboard authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import type { NetworkId } from 'common-types'

export interface Props {
activeAccount: string
network: NetworkId
}
13 changes: 4 additions & 9 deletions packages/app/src/contexts/FastUnstake/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
// SPDX-License-Identifier: GPL-3.0-only
/* eslint-disable @typescript-eslint/no-unused-vars */

import type { FastUnstakeContextInterface, MetaInterface } from './types'

export const defaultMeta: MetaInterface = {
checked: [],
}
import type { FastUnstakeContextInterface } from './types'

export const defaultFastUnstakeContext: FastUnstakeContextInterface = {
getLocalkey: (address) => '',
checking: false,
meta: defaultMeta,
isExposed: null,
exposed: false,
head: undefined,
queueDeposit: undefined,
counterForQueue: undefined,
fastUnstakeStatus: null,
setFastUnstakeStatus: (status) => {},
}
Loading

0 comments on commit 0d99cbb

Please sign in to comment.