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: abstract hooks from transfer panel #1838

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2334562
refactor: abstract hooks from transfer panel
fionnachan Aug 13, 2024
e1c2134
remove unneeded hook and move code
fionnachan Aug 13, 2024
e813c92
Merge remote-tracking branch 'origin/master' into abstract-hooks-from…
fionnachan Aug 15, 2024
5f67b0e
abstract same tx check logic
fionnachan Aug 15, 2024
0cc7c14
Merge remote-tracking branch 'origin/master' into abstract-hooks-from…
fionnachan Aug 16, 2024
09ced78
Merge branch 'master' into abstract-hooks-from-txpanel
fionnachan Aug 16, 2024
eccb808
Merge remote-tracking branch 'origin/master' into abstract-hooks-from…
fionnachan Aug 16, 2024
6a23084
clean up get destination address error logic
fionnachan Aug 16, 2024
4aa69ca
clean up
fionnachan Aug 19, 2024
b081e3c
Merge remote-tracking branch 'origin/master' into abstract-hooks-from…
fionnachan Aug 19, 2024
c0ff0ef
Merge remote-tracking branch 'origin/master' into abstract-hooks-from…
fionnachan Aug 20, 2024
e94954b
update
fionnachan Aug 20, 2024
4a5a5c8
Merge branch 'master' into abstract-hooks-from-txpanel
fionnachan Aug 20, 2024
a050cdc
Merge branch 'master' into abstract-hooks-from-txpanel
fionnachan Aug 28, 2024
8f82293
Merge branch 'master' into abstract-hooks-from-txpanel
fionnachan Aug 28, 2024
5ef62e1
Merge branch 'master' into abstract-hooks-from-txpanel
fionnachan Aug 30, 2024
2e28673
Merge remote-tracking branch 'origin/master' into abstract-hooks-from…
fionnachan Sep 3, 2024
48bd7c4
Merge branch 'master' into abstract-hooks-from-txpanel
fionnachan Sep 9, 2024
940f84c
Merge remote-tracking branch 'origin/master' into abstract-hooks-from…
fionnachan Sep 12, 2024
a7b0330
Merge remote-tracking branch 'origin/master' into abstract-hooks-from…
fionnachan Sep 18, 2024
7ec77bb
conflict
fionnachan Sep 18, 2024
cc8ca93
Merge remote-tracking branch 'origin/master' into abstract-hooks-from…
fionnachan Sep 19, 2024
7ecd609
Merge remote-tracking branch 'origin/master' into abstract-hooks-from…
fionnachan Sep 20, 2024
12be011
Merge remote-tracking branch 'origin/master' into abstract-hooks-from…
fionnachan Sep 20, 2024
0dfd4ef
update
fionnachan Sep 20, 2024
a6c2d1e
Merge remote-tracking branch 'origin/master' into abstract-hooks-from…
fionnachan Sep 20, 2024
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 @@ -11,13 +11,11 @@ import { ExternalLink } from '../common/ExternalLink'

import { useAppState } from '../../state'
import { useAccountType } from '../../hooks/useAccountType'
import {
addressIsSmartContract,
addressIsDenylisted
} from '../../util/AddressUtils'
import { addressIsSmartContract } from '../../util/AddressUtils'
import { useNetworks } from '../../hooks/useNetworks'
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship'
import { Transition } from '../common/Transition'
import { useDestinationAddressError } from './hooks/useDestinationAddressError'

export enum DestinationAddressErrors {
INVALID_ADDRESS = 'The destination address is not a valid address.',
Expand All @@ -31,55 +29,18 @@ enum DestinationAddressWarnings {
}

type DestinationAddressStore = {
error: DestinationAddressErrors | null
destinationAddress: string | undefined
setError: (error: DestinationAddressErrors | null) => void
setDestinationAddress: (destinationAddress: string | undefined) => void
}

export const useDestinationAddressStore = create<DestinationAddressStore>(
set => ({
error: null,
destinationAddress: undefined,
setError: error => set(() => ({ error })),
setDestinationAddress: destinationAddress =>
set(() => ({ destinationAddress }))
})
)

export async function getDestinationAddressError({
destinationAddress,
isSmartContractWallet,
isTeleportMode
}: {
destinationAddress?: string
isSmartContractWallet: boolean
isTeleportMode: boolean
}): Promise<DestinationAddressErrors | null> {
if (!destinationAddress && isSmartContractWallet) {
// destination address required for contract wallets
return DestinationAddressErrors.REQUIRED_ADDRESS
}

if (!destinationAddress) {
return null
}

if (!isAddress(destinationAddress)) {
return DestinationAddressErrors.INVALID_ADDRESS
}
if (await addressIsDenylisted(destinationAddress)) {
return DestinationAddressErrors.DENYLISTED_ADDRESS
}

if (isTeleportMode) {
return DestinationAddressErrors.TELEPORT_DISABLED
}

// no error
return null
}

async function getDestinationAddressWarning({
destinationAddress,
isEOA,
Expand Down Expand Up @@ -121,8 +82,7 @@ export const AdvancedSettings = () => {
childChainProvider,
parentChain,
parentChainProvider,
isDepositMode,
isTeleportMode
isDepositMode
} = useNetworksRelationship(networks)
const { address } = useAccount()
const { isEOA, isSmartContractWallet } = useAccountType()
Expand All @@ -131,29 +91,18 @@ export const AdvancedSettings = () => {
const [inputLocked, setInputLocked] = useState(true)
const [warning, setWarning] = useState<string | null>(null)

const { error, setError, destinationAddress, setDestinationAddress } =
const { destinationAddress, setDestinationAddress } =
useDestinationAddressStore()

const { destinationAddressError: error } = useDestinationAddressError()

useEffect(() => {
// Initially hide for EOA
setCollapsed(isEOA)
// Initially lock for EOA
setInputLocked(isEOA)
}, [isEOA])

useEffect(() => {
async function updateError() {
setError(
await getDestinationAddressError({
destinationAddress,
isSmartContractWallet,
isTeleportMode
})
)
}
updateError()
}, [destinationAddress, isSmartContractWallet, setError, isTeleportMode])

useEffect(() => {
// isSubscribed makes sure that only the latest state is written
let isSubscribed = true
Expand All @@ -180,7 +129,8 @@ export const AdvancedSettings = () => {
childChainProvider,
parentChainProvider,
childChain.id,
parentChain.id
parentChain.id,
networks.destinationChain.id
])

const collapsible = useMemo(() => {
Expand Down
Loading
Loading