From ccc25f4142e24bddacf5cb8e475387279ae964f6 Mon Sep 17 00:00:00 2001 From: Felipe Mendes Date: Fri, 15 Nov 2024 10:40:50 -0300 Subject: [PATCH] refactor: logic for new address and chain event handler --- .../scaffold-ui/src/modal/w3m-modal/index.ts | 34 +++++-------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/packages/scaffold-ui/src/modal/w3m-modal/index.ts b/packages/scaffold-ui/src/modal/w3m-modal/index.ts index 9f701854a0..a562c25e56 100644 --- a/packages/scaffold-ui/src/modal/w3m-modal/index.ts +++ b/packages/scaffold-ui/src/modal/w3m-modal/index.ts @@ -13,7 +13,7 @@ import { UiHelperUtil, customElement, initializeTheming } from '@reown/appkit-ui import { LitElement, html } from 'lit' import { state } from 'lit/decorators.js' import styles from './styles.js' -import { type CaipAddress, type CaipNetwork } from '@reown/appkit-common' +import { type CaipAddress } from '@reown/appkit-common' // -- Helpers --------------------------------------------- // const SCROLL_LOCK = 'scroll-lock' @@ -32,8 +32,6 @@ export class W3mModal extends LitElement { @state() private caipAddress = ChainController.state.activeCaipAddress - @state() private caipNetwork = ChainController.state.activeCaipNetwork - @state() private shake = ModalController.state.shake public constructor() { @@ -44,7 +42,6 @@ export class W3mModal extends LitElement { ...[ ModalController.subscribeKey('open', val => (val ? this.onOpen() : this.onClose())), ModalController.subscribeKey('shake', val => (this.shake = val)), - ChainController.subscribeKey('activeCaipNetwork', val => this.onNewNetwork(val)), ChainController.subscribeKey('activeCaipAddress', val => this.onNewAddress(val)) ] ) @@ -165,32 +162,19 @@ export class W3mModal extends LitElement { } private async onNewAddress(caipAddress?: CaipAddress) { - const nextConnected = caipAddress ? CoreHelperUtil.getPlainAddress(caipAddress) : undefined - - this.caipAddress = caipAddress - - await SIWXUtil.initializeIfEnabled() + const currentNetwork = CoreHelperUtil.getNetworkId(this.caipAddress) + const nextNetwork = CoreHelperUtil.getNetworkId(caipAddress) + const isNextConnected = Boolean(CoreHelperUtil.getPlainAddress(caipAddress)) - if (!nextConnected) { - ModalController.close() - } - } - - private onNewNetwork(nextCaipNetwork: CaipNetwork | undefined) { - if (!this.caipAddress) { - this.caipNetwork = nextCaipNetwork + if (currentNetwork && nextNetwork && currentNetwork !== nextNetwork) { RouterController.goBack() - - return + } else if (!isNextConnected) { + ModalController.close() } - const prevCaipNetworkId = this.caipNetwork?.caipNetworkId?.toString() - const nextNetworkId = nextCaipNetwork?.caipNetworkId?.toString() + await SIWXUtil.initializeIfEnabled() - if (prevCaipNetworkId && nextNetworkId && prevCaipNetworkId !== nextNetworkId) { - RouterController.goBack() - } - this.caipNetwork = nextCaipNetwork + this.caipAddress = caipAddress } }