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

refactor: move dialogs to a single component (part 3) #2299

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
709e1ca
dialog refactoring
brtkx Feb 27, 2025
e587845
fix
brtkx Feb 27, 2025
1e22e8b
fixes and clean ups
brtkx Feb 28, 2025
51a0e09
clean ups
brtkx Feb 28, 2025
13b7ce3
dialogs refactoring 2
brtkx Feb 28, 2025
9aa2ea6
native token approval dialog refactor
brtkx Feb 28, 2025
d466feb
rename
brtkx Feb 28, 2025
c275be1
Merge branch 'dialogs-refactoring' of https://github.com/OffchainLabs…
brtkx Feb 28, 2025
4e2bdf8
Merge branch 'dialogs-refactoring-2' of https://github.com/OffchainLa…
brtkx Feb 28, 2025
5ec3f12
dialogs refactoring 3
brtkx Feb 28, 2025
8cd5e96
token import refactored dialog
brtkx Feb 28, 2025
57ceed8
clean up
brtkx Feb 28, 2025
d88aabd
Merge branch 'master' of https://github.com/OffchainLabs/arbitrum-tok…
brtkx Mar 3, 2025
bcb72ea
rename to dialog2
brtkx Mar 3, 2025
f90ce7a
Merge branch 'dialogs-refactoring' of https://github.com/OffchainLabs…
brtkx Mar 3, 2025
d8562b3
Merge branch 'dialogs-refactoring-2' of https://github.com/OffchainLa…
brtkx Mar 3, 2025
59e93e4
remove import
brtkx Mar 3, 2025
8b74788
Merge branch 'dialogs-refactoring-3' of https://github.com/OffchainLa…
brtkx Mar 3, 2025
40bfdc2
fix cctp for approvals
brtkx Mar 3, 2025
cab713a
run prettier
brtkx Mar 3, 2025
54797e5
better fix
brtkx Mar 3, 2025
fa446ca
typo
brtkx Mar 3, 2025
0303eec
Merge branch 'master' of https://github.com/OffchainLabs/arbitrum-tok…
brtkx Mar 3, 2025
a2d6476
Merge branch 'dialogs-refactoring' of https://github.com/OffchainLabs…
brtkx Mar 3, 2025
d46826e
Merge branch 'dialogs-refactoring-2' of https://github.com/OffchainLa…
brtkx Mar 3, 2025
c3c5cfa
Merge branch 'dialogs-refactoring-3' of https://github.com/OffchainLa…
brtkx Mar 3, 2025
c439c84
Merge branch 'master' of https://github.com/OffchainLabs/arbitrum-tok…
brtkx Mar 6, 2025
7ccafcf
clean up
brtkx Mar 6, 2025
c8ebad5
clean up
brtkx Mar 6, 2025
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
@@ -1,6 +1,5 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useLatest } from 'react-use'
import { create } from 'zustand'

import { useERC20L1Address } from '../../hooks/useERC20L1Address'
import { useAppState } from '../../state'
Expand Down Expand Up @@ -39,25 +38,12 @@ type TokenListSearchResult =
status: ImportStatus
}

type TokenImportDialogStore = {
isOpen: boolean
openDialog: () => void
closeDialog: () => void
}

export const useTokenImportDialogStore = create<TokenImportDialogStore>(
set => ({
isOpen: false,
openDialog: () => set({ isOpen: true }),
closeDialog: () => set({ isOpen: false })
})
)

type TokenImportDialogProps = Omit<UseDialogProps, 'isOpen'> & {
type TokenImportDialogProps = UseDialogProps & {
tokenAddress: string
}

export function TokenImportDialog({
isOpen,
onClose,
tokenAddress
}: TokenImportDialogProps): JSX.Element {
Expand All @@ -82,7 +68,6 @@ export function TokenImportDialog({
const [status, setStatus] = useState<ImportStatus>(ImportStatus.LOADING)
const [isImportingToken, setIsImportingToken] = useState<boolean>(false)
const [tokenToImport, setTokenToImport] = useState<ERC20BridgeToken>()
const { isOpen } = useTokenImportDialogStore()
const [isDialogVisible, setIsDialogVisible] = useState(false)
const { data: l1Address, isLoading: isL1AddressLoading } = useERC20L1Address({
eitherL1OrL2Address: tokenAddress,
Expand Down Expand Up @@ -281,9 +266,11 @@ export function TokenImportDialog({
selectToken(tokenToImport!)
} else {
// Token is not added to the bridge, so we add it
storeNewToken(l1Address).catch(() => {
setStatus(ImportStatus.ERROR)
})
storeNewToken(l1Address)
.catch(() => {
setStatus(ImportStatus.ERROR)
})
.then(() => onClose(true))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import {
TokenDepositCheckDialog,
TokenDepositCheckDialogType
} from './TokenDepositCheckDialog'
import {
TokenImportDialog,
useTokenImportDialogStore
} from './TokenImportDialog'
import { useArbQueryParams } from '../../hooks/useArbQueryParams'
import { useDialog } from '../common/Dialog'
import { TransferPanelSummary } from './TransferPanelSummary'
Expand All @@ -40,10 +36,7 @@ import {
AssetType,
DepositGasEstimates
} from '../../hooks/arbTokenBridge.types'
import {
ImportTokenModalStatus,
getWarningTokenDescription
} from './TransferPanelUtils'
import { getWarningTokenDescription } from './TransferPanelUtils'
import { useTransactionHistory } from '../../hooks/useTransactionHistory'
import { useNetworks } from '../../hooks/useNetworks'
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship'
Expand Down Expand Up @@ -104,8 +97,6 @@ export function TransferPanel() {
const [{ token: tokenFromSearchParams }] = useArbQueryParams()
const [tokenDepositCheckDialogType, setTokenDepositCheckDialogType] =
useState<TokenDepositCheckDialogType>('new-token')
const [importTokenModalStatus, setImportTokenModalStatus] =
useState<ImportTokenModalStatus>(ImportTokenModalStatus.IDLE)
const [showSmartContractWalletTooltip, setShowSmartContractWalletTooltip] =
useState(false)

Expand All @@ -115,7 +106,7 @@ export function TransferPanel() {
warningTokens
}
} = useAppState()
const [selectedToken, setSelectedToken] = useSelectedToken()
const [selectedToken] = useSelectedToken()
const { address: walletAddress } = useAccount()
const { switchNetworkAsync } = useSwitchNetworkWithConfig({
isSwitchingNetworkBeforeTx: true
Expand Down Expand Up @@ -169,8 +160,8 @@ export function TransferPanel() {

const [dialogProps, openDialog] = useDialog2()

const [tokenImportDialogProps] = useDialog()
const [tokenCheckDialogProps, openTokenCheckDialog] = useDialog()

const [
usdcWithdrawalConfirmationDialogProps,
openUSDCWithdrawalConfirmationDialog
Expand All @@ -180,8 +171,6 @@ export function TransferPanel() {
openUSDCDepositConfirmationDialog
] = useDialog()

const { openDialog: openTokenImportDialog } = useTokenImportDialogStore()

const isCustomDestinationTransfer = !!latestDestinationAddress.current

const {
Expand All @@ -201,20 +190,6 @@ export function TransferPanel() {
setShowProjectsListing(false)
}, [childChain.id, parentChain.id])

useEffect(() => {
if (importTokenModalStatus !== ImportTokenModalStatus.IDLE) {
return
}

openTokenImportDialog()
}, [importTokenModalStatus, openTokenImportDialog])

function closeWithResetTokenImportDialog() {
setSelectedToken(null)
setImportTokenModalStatus(ImportTokenModalStatus.CLOSED)
tokenImportDialogProps.onClose(false)
}

function clearAmountInput() {
// clear amount input on transfer panel
setAmount('')
Expand Down Expand Up @@ -255,6 +230,12 @@ export function TransferPanel() {
tokensFromUser
])

useEffect(() => {
if (isTokenAlreadyImported === false && tokenFromSearchParams) {
openDialog('import_token')
}
}, [isTokenAlreadyImported, tokenFromSearchParams, openDialog])

const isBridgingANewStandardToken = useMemo(() => {
const isUnbridgedToken =
selectedToken !== null && typeof selectedToken.l2Address === 'undefined'
Expand Down Expand Up @@ -1182,14 +1163,6 @@ export function TransferPanel() {
/>
<MoveFundsButton onClick={moveFundsButtonOnClick} />

{isTokenAlreadyImported === false && tokenFromSearchParams && (
<TokenImportDialog
{...tokenImportDialogProps}
onClose={closeWithResetTokenImportDialog}
tokenAddress={tokenFromSearchParams}
/>
)}

<TokenDepositCheckDialog
{...tokenCheckDialogProps}
type={tokenDepositCheckDialogType}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
export enum ImportTokenModalStatus {
// "IDLE" is here to distinguish between the modal never being opened, and being closed after a user interaction
IDLE,
OPEN,
CLOSED
}

export function getWarningTokenDescription(warningTokenType: number) {
switch (warningTokenType) {
case 0:
Expand Down
20 changes: 18 additions & 2 deletions packages/arb-token-bridge-ui/src/components/common/Dialog2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useNativeCurrency } from '../../hooks/useNativeCurrency'
import { useNetworks } from '../../hooks/useNetworks'
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship'
import { CustomDestinationAddressConfirmationDialog } from '../TransferPanel/CustomDestinationAddressConfirmationDialog'
import { TokenImportDialog } from '../TransferPanel/TokenImportDialog'
/**
* Returns a promise which resolves to an array [boolean, unknown] value,
* `false` if the action was canceled and `true` if it was confirmed.
Expand All @@ -31,6 +32,7 @@ type DialogType =
| 'approve_token'
| 'approve_cctp_usdc'
| 'approve_custom_fee_token'
| 'import_token'
| 'withdraw'
| 'scw_custom_destination_address'

Expand Down Expand Up @@ -79,8 +81,8 @@ type DialogProps = {

export function DialogWrapper(props: DialogProps) {
const isOftTransfer = useIsOftV2Transfer()
const [selectedToken] = useSelectedToken()
const [{ amount }] = useArbQueryParams()
const [selectedToken, setSelectedToken] = useSelectedToken()
const [{ amount, token: tokenFromSearchParams }] = useArbQueryParams()
const [networks] = useNetworks()
const { childChainProvider } = useNetworksRelationship(networks)
const nativeCurrency = useNativeCurrency({ provider: childChainProvider })
Expand All @@ -106,6 +108,20 @@ export function DialogWrapper(props: DialogProps) {
isOft={isOftTransfer}
/>
)
case 'import_token':
if (tokenFromSearchParams) {
return (
<TokenImportDialog
{...commonProps}
onClose={imported => {
setSelectedToken(imported ? tokenFromSearchParams : null)
commonProps.onClose(imported)
}}
tokenAddress={tokenFromSearchParams}
/>
)
}
return null
case 'approve_custom_fee_token':
if (nativeCurrency.isCustom) {
return (
Expand Down
Loading