Skip to content

Cleanup wallet connected into a hook #11497

@gomesalexandre

Description

@gomesalexandre

Overview

Smol follow-up of #11475

Consolidates the repetitive isConnected || isLedgerReadOnly check pattern into a reusable hook. This eliminates 85-119 lines of boilerplate across 17 files and establishes a conventional pattern to prevent similar bugs.

Current pattern (5 lines, repeated 17+ times):

const {
  state: { isConnected: isWalletConnected },
} = useWallet()
const walletType = useAppSelector(selectWalletType)
const isLedgerReadOnlyEnabled = useFeatureFlag('LedgerReadOnly')
const isLedgerReadOnly = isLedgerReadOnlyEnabled && walletType === KeyManager.Ledger
const isConnected = isWalletConnected || isLedgerReadOnly

Proposed hook implementation:

// src/hooks/useIsWalletConnected/useIsWalletConnected.tsx
import { KeyManager } from '@/context/WalletProvider/KeyManager'
import { useFeatureFlag } from '@/hooks/useFeatureFlag/useFeatureFlag'
import { useWallet } from '@/hooks/useWallet/useWallet'
import { selectWalletType } from '@/state/slices/localWalletSlice/selectors'
import { useAppSelector } from '@/state/store'

/**
 * Returns true if wallet is connected OR if Ledger read-only mode is active.
 * Use this when gating UI that should display for both connected wallets and Ledger read-only mode.
 */
export const useIsWalletConnected = () => {
  const {
    state: { isConnected: isWalletConnected },
  } = useWallet()
  const walletType = useAppSelector(selectWalletType)
  const isLedgerReadOnlyEnabled = useFeatureFlag('LedgerReadOnly')
  const isLedgerReadOnly = isLedgerReadOnlyEnabled && walletType === KeyManager.Ledger
  
  return isWalletConnected || isLedgerReadOnly
}

Usage (1 line):

const isConnected = useIsWalletConnected()

References and additional details

Files to refactor (17):

  • Fox components (5): FoxTokenBalances, FoxGovernance, RFOXSection, FoxFarming, FoxHeader
  • Dashboard (3): DashboardHeaderTop, AccountTable, Dashboard
  • Trade/TX (3): TradeAssetInput, TransactionHistoryContent, WalletBalanceChange
  • Common (5): AccountDropdown, AssetRow, GroupedAssetRow, AssetActions, ActionCenter
  • Other (1): TCY page

Acceptance Criteria

Things work the exact same as with #11475 (current develop) - no behavioral changes, just consolidated logic.

Need By Date

No response

Screenshots/Mockups

No response

Estimated effort

1 hour

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Status

Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions