Skip to content

Commit 597900b

Browse files
authored
Add onConnect prop on AutoConnect (#2879)
1 parent bfdfc23 commit 597900b

File tree

5 files changed

+44
-32
lines changed

5 files changed

+44
-32
lines changed

.changeset/gorgeous-maps-mate.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
- Add onConnect prop on `AutoConnect` component
6+
- Call the `onConnect` callback passed to `ConnectButton` and `ConnectEmbed` when wallet is auto-connected

packages/thirdweb/src/react/core/hooks/connection/useAutoConnect.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ export type AutoConnectProps = {
109109
* />
110110
*/
111111
accountAbstraction?: SmartWalletOptions;
112+
113+
/**
114+
* Callback to be called on successful auto-connection of last active wallet. The callback is called with the connected wallet
115+
*
116+
* ```tsx
117+
* <AutoConnect
118+
* onConnect={(wallet) => {
119+
* console.log("auto connected to", wallet)
120+
* }}
121+
* />
122+
* ```
123+
*/
124+
onConnect?: (wallet: Wallet) => void;
112125
};
113126

114127
/**
@@ -149,7 +162,7 @@ export function AutoConnect(props: AutoConnectProps) {
149162
accountAbstraction: props.accountAbstraction,
150163
});
151164
const { isAutoConnecting } = connectionManager;
152-
const { wallets } = props;
165+
const { wallets, onConnect } = props;
153166
const timeout = props.timeout ?? 15000;
154167
// get the supported wallets from thirdweb provider
155168
// check the storage for last connected wallets and connect them all
@@ -192,7 +205,20 @@ export function AutoConnect(props: AutoConnectProps) {
192205
message: `AutoConnect timeout : ${timeout}ms limit exceeded.`,
193206
});
194207

195-
connect(activeWallet);
208+
// connected wallet could be activeWallet or smart wallet
209+
const connectedWallet = await connect(activeWallet);
210+
211+
if (connectedWallet) {
212+
if (onConnect) {
213+
try {
214+
onConnect(connectedWallet);
215+
} catch {
216+
// ignore
217+
}
218+
}
219+
} else {
220+
setConnectionStatus("disconnected");
221+
}
196222
} catch (e) {
197223
console.error("Failed to auto connect last active wallet");
198224
console.error(e);

packages/thirdweb/src/react/web/ui/ConnectWallet/ConnectWallet.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export function ConnectButton(props: ConnectButtonProps) {
6363
: props.autoConnect?.timeout
6464
}
6565
accountAbstraction={props.accountAbstraction}
66+
onConnect={props.onConnect}
6667
/>
6768
);
6869

packages/thirdweb/src/react/web/ui/ConnectWallet/ConnectWalletProps.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -576,28 +576,16 @@ export type ConnectButtonProps = {
576576
supportedTokens?: SupportedTokens;
577577

578578
/**
579-
* Callback to be called on successful connection of wallet. The callback is called with the connected account
579+
* Callback to be called on successful connection of wallet - including auto connect.
580+
* The callback is called with the connected wallet
580581
*
581582
* ```tsx
582583
* <ConnectButton
583-
* onConnect={(account) => {
584-
* console.log("connected to", account)
584+
* onConnect={(wallet) => {
585+
* console.log("connected to", wallet)
585586
* }}
586587
* />
587588
* ```
588-
*
589-
* Note that this does not include the sign in, If you want to call a callback after user connects AND signs in with their wallet, use `auth.onLogin` prop instead
590-
*
591-
* ```tsx
592-
* <ConnectButton
593-
* auth={{
594-
* onLogin: () => {
595-
* console.log("wallet connected and signed in")
596-
* }
597-
* }}
598-
* />
599-
* ```
600-
*
601589
*/
602590
onConnect?: (wallet: Wallet) => void;
603591

packages/thirdweb/src/react/web/ui/ConnectWallet/Modal/ConnectEmbed.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -225,26 +225,16 @@ export type ConnectEmbedProps = {
225225
privacyPolicyUrl?: string;
226226

227227
/**
228-
* Callback to be called on successful connection of wallet. The callback is called with the connected account
228+
* Callback to be called on successful connection of wallet - including auto-connect.
229+
* The callback is called with the connected wallet
229230
*
230231
* ```tsx
231232
* <ConnectEmbed
232-
* onConnect={(account) => {
233-
* console.log("connected to", account)
233+
* onConnect={(wallet) => {
234+
* console.log("connected to", wallet)
234235
* }}
235236
* />
236237
* ```
237-
*
238-
* Note that this does not include the sign in, If you want to call a callback after user connects AND signs in with their wallet, use `auth.onLogin` prop instead
239-
*
240-
* ```tsx
241-
* <ConnectEmbed
242-
* auth={{
243-
* onLogin: () => {
244-
* console.log("wallet connected and signed in")
245-
* }
246-
* }}
247-
* />
248238
* ```
249239
*/
250240
onConnect?: (wallet: Wallet) => void;
@@ -366,6 +356,7 @@ export function ConnectEmbed(props: ConnectEmbedProps) {
366356
? undefined
367357
: props.autoConnect?.timeout
368358
}
359+
onConnect={props.onConnect}
369360
/>
370361
);
371362

0 commit comments

Comments
 (0)