1
- import { Account , useAccount } from "@3rdweb-sdk/react/hooks/useApi" ;
1
+ import {
2
+ Account ,
3
+ useAccount ,
4
+ useConfirmPaperEmail ,
5
+ } from "@3rdweb-sdk/react/hooks/useApi" ;
2
6
import { useEffect , useState } from "react" ;
3
7
import { OnboardingModal } from "./Modal" ;
4
8
import { OnboardingGeneral } from "./General" ;
@@ -7,12 +11,23 @@ import { useRouter } from "next/router";
7
11
import { OnboardingBilling } from "./Billing" ;
8
12
import { useTrack } from "hooks/analytics/useTrack" ;
9
13
import { useLoggedInUser } from "@3rdweb-sdk/react/hooks/useLoggedInUser" ;
14
+ import { useWallet } from "@thirdweb-dev/react" ;
15
+ import { GLOBAL_PAPER_AUTH_TOKEN_KEY } from "constants/app" ;
16
+
17
+ const skipBilling = ( account : Account ) => {
18
+ return (
19
+ [ "validPayment" , "paymentVerification" ] . includes ( account . status ) ||
20
+ account . onboardSkipped
21
+ ) ;
22
+ } ;
10
23
11
24
export const Onboarding : React . FC = ( ) => {
12
25
const meQuery = useAccount ( ) ;
13
26
const router = useRouter ( ) ;
14
27
const { isLoggedIn } = useLoggedInUser ( ) ;
15
28
const trackEvent = useTrack ( ) ;
29
+ const wallet = useWallet ( ) ;
30
+ const paperConfirmMutation = useConfirmPaperEmail ( ) ;
16
31
17
32
const [ state , setState ] = useState <
18
33
"onboarding" | "confirming" | "billing" | "skipped" | undefined
@@ -45,11 +60,7 @@ export const Onboarding: React.FC = () => {
45
60
} ,
46
61
} ) ;
47
62
} else if ( state === "confirming" ) {
48
- const newState =
49
- [ "validPayment" , "paymentVerification" ] . includes ( account . status ) ||
50
- account . onboardSkipped
51
- ? "skipped"
52
- : "billing" ;
63
+ const newState = skipBilling ( account ) ? "skipped" : "billing" ;
53
64
setState ( newState ) ;
54
65
55
66
trackEvent ( {
@@ -72,28 +83,47 @@ export const Onboarding: React.FC = () => {
72
83
}
73
84
} ;
74
85
86
+ const handlePaperWallet = ( ) => {
87
+ const paperJwt = ( window as any ) [ GLOBAL_PAPER_AUTH_TOKEN_KEY ] ;
88
+
89
+ if ( paperJwt ) {
90
+ paperConfirmMutation . mutate (
91
+ { paperJwt } ,
92
+ {
93
+ onSuccess : ( data ) => {
94
+ if ( ! skipBilling ( data as Account ) ) {
95
+ setState ( "billing" ) ;
96
+ }
97
+ ( window as any ) [ GLOBAL_PAPER_AUTH_TOKEN_KEY ] = undefined ;
98
+ } ,
99
+ } ,
100
+ ) ;
101
+ }
102
+ } ;
103
+
75
104
useEffect ( ( ) => {
76
- if ( ! isLoggedIn || ! account || state ) {
105
+ if ( ! isLoggedIn || ! account || state || ! wallet ) {
77
106
return ;
78
107
}
79
-
80
108
// user hasn't confirmed email
81
109
if ( ! account . emailConfirmedAt && ! account . unconfirmedEmail ) {
82
- setState ( "onboarding" ) ;
110
+ // if its a paper email wallet, try to confirm it
111
+ if ( wallet . walletId === "paper" ) {
112
+ handlePaperWallet ( ) ;
113
+ } else {
114
+ setState ( "onboarding" ) ;
115
+ }
83
116
}
84
117
// user has changed email and needs to confirm
85
118
else if ( account . unconfirmedEmail ) {
86
119
setState ( "confirming" ) ;
87
120
}
88
121
// user hasn't skipped onboarding, has valid email and no valid payment yet
89
- else if (
90
- account . email &&
91
- ! account . onboardSkipped &&
92
- ! [ "validPayment" , "paymentVerification" ] . includes ( account . status )
93
- ) {
122
+ else if ( ! skipBilling ( account ) ) {
94
123
setState ( "billing" ) ;
95
124
}
96
- } , [ isLoggedIn , account , router , state ] ) ;
125
+ // eslint-disable-next-line react-hooks/exhaustive-deps
126
+ } , [ isLoggedIn , account , router , state , wallet ] ) ;
97
127
98
128
if ( ! isLoggedIn || ! account || state === "skipped" || ! state ) {
99
129
return null ;
0 commit comments