|
1 | 1 | "use client"; |
2 | | -import { ExclamationTriangleIcon } from "@radix-ui/react-icons"; |
3 | 2 | import { useQuery } from "@tanstack/react-query"; |
4 | 3 | import { useCallback, useEffect, useRef, useState } from "react"; |
5 | 4 | import type { Chain } from "../../../../../chains/types.js"; |
6 | 5 | import type { ThirdwebClient } from "../../../../../client/client.js"; |
7 | 6 | import type { Wallet } from "../../../../../wallets/interfaces/wallet.js"; |
8 | 7 | import type { SmartWalletOptions } from "../../../../../wallets/smart/types.js"; |
9 | 8 | import type { WalletInfo } from "../../../../../wallets/wallet-info.js"; |
10 | | -import { |
11 | | - fontSize, |
12 | | - iconSize, |
13 | | - spacing, |
14 | | -} from "../../../../core/design-system/index.js"; |
15 | 9 | import { useConnectionManager } from "../../../../core/providers/connection-manager.js"; |
16 | 10 | import { useWalletInfo } from "../../../../core/utils/wallet.js"; |
17 | 11 | import { LoadingScreen } from "../../../wallets/shared/LoadingScreen.js"; |
18 | 12 | import { getSmartWalletLocale } from "../../../wallets/smartWallet/locale/getSmartWalletLocale.js"; |
19 | 13 | import type { SmartWalletLocale } from "../../../wallets/smartWallet/locale/types.js"; |
20 | 14 | import { Spacer } from "../../components/Spacer.js"; |
21 | 15 | import { Spinner } from "../../components/Spinner.js"; |
22 | | -import { Container, ModalHeader } from "../../components/basic.js"; |
23 | | -import { Button } from "../../components/buttons.js"; |
| 16 | +import { Container } from "../../components/basic.js"; |
24 | 17 | import { Text } from "../../components/text.js"; |
25 | 18 | import type { LocaleId } from "../../types.js"; |
26 | 19 | import type { ConnectLocale } from "../locale/types.js"; |
@@ -109,32 +102,11 @@ function SmartWalletConnecting(props: { |
109 | 102 | queryKey: ["getSmartWalletLocale", props.localeId], |
110 | 103 | queryFn: () => getSmartWalletLocale(props.localeId), |
111 | 104 | }); |
112 | | - const { chain: smartWalletChain } = props.accountAbstraction; |
113 | | - |
114 | 105 | const { personalWallet } = props; |
115 | 106 | const { done } = props; |
116 | 107 |
|
117 | | - const [personalWalletChainId, setPersonalWalletChainId] = useState< |
118 | | - number | undefined |
119 | | - >(personalWallet.getChain()?.id); |
120 | | - |
121 | | - useEffect(() => { |
122 | | - const unsubChainChanged = personalWallet.subscribe( |
123 | | - "chainChanged", |
124 | | - (chain) => setPersonalWalletChainId(chain.id), |
125 | | - ); |
126 | | - |
127 | | - return () => { |
128 | | - unsubChainChanged(); |
129 | | - }; |
130 | | - }, [personalWallet]); |
131 | | - |
132 | | - const wrongNetwork = personalWalletChainId !== smartWalletChain.id; |
133 | | - |
134 | 108 | const [smartWalletConnectionStatus, setSmartWalletConnectionStatus] = |
135 | 109 | useState<"connecting" | "connect-error" | "idle">("idle"); |
136 | | - const [personalWalletChainSwitchStatus, setPersonalWalletChainSwitchStatus] = |
137 | | - useState<"switching" | "switch-error" | "idle">("idle"); |
138 | 110 | const connectionManager = useConnectionManager(); |
139 | 111 |
|
140 | 112 | const handleConnect = useCallback(async () => { |
@@ -168,117 +140,16 @@ function SmartWalletConnecting(props: { |
168 | 140 |
|
169 | 141 | const connectStarted = useRef(false); |
170 | 142 | useEffect(() => { |
171 | | - if (!wrongNetwork && !connectStarted.current) { |
| 143 | + if (!connectStarted.current) { |
172 | 144 | handleConnect(); |
173 | 145 | connectStarted.current = true; |
174 | 146 | } |
175 | | - }, [handleConnect, wrongNetwork]); |
| 147 | + }, [handleConnect]); |
176 | 148 |
|
177 | 149 | if (!localeQuery.data) { |
178 | 150 | return <LoadingScreen />; |
179 | 151 | } |
180 | 152 |
|
181 | | - if (wrongNetwork) { |
182 | | - return ( |
183 | | - <Container fullHeight animate="fadein" flex="column"> |
184 | | - <Container p="lg"> |
185 | | - <ModalHeader |
186 | | - title={props.personalWalletInfo.name} |
187 | | - onBack={props.onBack} |
188 | | - /> |
189 | | - </Container> |
190 | | - |
191 | | - {props.size === "compact" && <Spacer y="lg" />} |
192 | | - |
193 | | - <Container expand flex="column" center="both" p="lg"> |
194 | | - <Container p={props.size === "wide" ? "lg" : undefined}> |
195 | | - <Container flex="row" center="x" color="danger"> |
196 | | - <ExclamationTriangleIcon |
197 | | - width={iconSize.lg} |
198 | | - height={iconSize.lg} |
199 | | - /> |
200 | | - </Container> |
201 | | - |
202 | | - <Spacer y="md" /> |
203 | | - |
204 | | - <Text size="lg" color="primaryText" center weight={500}> |
205 | | - {localeQuery.data.wrongNetworkScreen.title} |
206 | | - </Text> |
207 | | - |
208 | | - <Spacer y="lg" /> |
209 | | - |
210 | | - <Text multiline center> |
211 | | - {localeQuery.data.wrongNetworkScreen.subtitle} |
212 | | - </Text> |
213 | | - |
214 | | - <Spacer y="xl" /> |
215 | | - |
216 | | - <Container flex="column" gap="md"> |
217 | | - <Button |
218 | | - type="button" |
219 | | - fullWidth |
220 | | - variant="accent" |
221 | | - style={{ |
222 | | - display: "flex", |
223 | | - alignItems: "center", |
224 | | - gap: spacing.sm, |
225 | | - }} |
226 | | - onClick={async () => { |
227 | | - if (!personalWallet.switchChain) { |
228 | | - setPersonalWalletChainSwitchStatus("switch-error"); |
229 | | - throw new Error("No switchChain method"); |
230 | | - } |
231 | | - |
232 | | - try { |
233 | | - setPersonalWalletChainSwitchStatus("switching"); |
234 | | - await personalWallet.switchChain(smartWalletChain); |
235 | | - const newChain = personalWallet.getChain(); |
236 | | - if (newChain) { |
237 | | - setPersonalWalletChainId(newChain.id); |
238 | | - } |
239 | | - setPersonalWalletChainSwitchStatus("idle"); |
240 | | - } catch (e) { |
241 | | - console.error(e); |
242 | | - setPersonalWalletChainSwitchStatus("switch-error"); |
243 | | - } |
244 | | - }} |
245 | | - > |
246 | | - {personalWalletChainSwitchStatus === "switching" |
247 | | - ? "Switching" |
248 | | - : "Switch Network"} |
249 | | - {personalWalletChainSwitchStatus === "switching" && ( |
250 | | - <Spinner size="sm" color="accentButtonText" /> |
251 | | - )} |
252 | | - </Button> |
253 | | - |
254 | | - <Container |
255 | | - flex="row" |
256 | | - gap="sm" |
257 | | - center="both" |
258 | | - color="danger" |
259 | | - style={{ |
260 | | - textAlign: "center", |
261 | | - fontSize: fontSize.sm, |
262 | | - opacity: |
263 | | - personalWalletChainSwitchStatus === "switch-error" ? 1 : 0, |
264 | | - transition: "opacity 200ms ease", |
265 | | - }} |
266 | | - > |
267 | | - <ExclamationTriangleIcon |
268 | | - width={iconSize.sm} |
269 | | - height={iconSize.sm} |
270 | | - /> |
271 | | - <span> |
272 | | - {localeQuery.data.wrongNetworkScreen.failedToSwitch} |
273 | | - </span> |
274 | | - </Container> |
275 | | - </Container> |
276 | | - </Container> |
277 | | - </Container> |
278 | | - </Container> |
279 | | - ); |
280 | | - } |
281 | | - |
282 | 153 | if (smartWalletConnectionStatus === "connect-error") { |
283 | 154 | return ( |
284 | 155 | <Container |
|
0 commit comments