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" ;
26import { useEffect , useState } from "react" ;
37import { OnboardingModal } from "./Modal" ;
48import { OnboardingGeneral } from "./General" ;
@@ -7,12 +11,23 @@ import { useRouter } from "next/router";
711import { OnboardingBilling } from "./Billing" ;
812import { useTrack } from "hooks/analytics/useTrack" ;
913import { 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+ } ;
1023
1124export const Onboarding : React . FC = ( ) => {
1225 const meQuery = useAccount ( ) ;
1326 const router = useRouter ( ) ;
1427 const { isLoggedIn } = useLoggedInUser ( ) ;
1528 const trackEvent = useTrack ( ) ;
29+ const wallet = useWallet ( ) ;
30+ const paperConfirmMutation = useConfirmPaperEmail ( ) ;
1631
1732 const [ state , setState ] = useState <
1833 "onboarding" | "confirming" | "billing" | "skipped" | undefined
@@ -45,11 +60,7 @@ export const Onboarding: React.FC = () => {
4560 } ,
4661 } ) ;
4762 } 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" ;
5364 setState ( newState ) ;
5465
5566 trackEvent ( {
@@ -72,28 +83,47 @@ export const Onboarding: React.FC = () => {
7283 }
7384 } ;
7485
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+
75104 useEffect ( ( ) => {
76- if ( ! isLoggedIn || ! account || state ) {
105+ if ( ! isLoggedIn || ! account || state || ! wallet ) {
77106 return ;
78107 }
79-
80108 // user hasn't confirmed email
81109 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+ }
83116 }
84117 // user has changed email and needs to confirm
85118 else if ( account . unconfirmedEmail ) {
86119 setState ( "confirming" ) ;
87120 }
88121 // 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 ) ) {
94123 setState ( "billing" ) ;
95124 }
96- } , [ isLoggedIn , account , router , state ] ) ;
125+ // eslint-disable-next-line react-hooks/exhaustive-deps
126+ } , [ isLoggedIn , account , router , state , wallet ] ) ;
97127
98128 if ( ! isLoggedIn || ! account || state === "skipped" || ! state ) {
99129 return null ;
0 commit comments