11import { useReverification , useUser } from '@clerk/shared/react' ;
22import type { Web3Provider , Web3Strategy } from '@clerk/shared/types' ;
33
4- import { useCardState , withCardStateProvider } from '@/ui/elements/contexts' ;
4+ import { Web3SelectSolanaWalletScreen } from '@/ui/components/UserProfile/Web3SelectSolanaWalletScreen' ;
5+ import { Action } from '@/ui/elements/Action' ;
6+ import { useActionContext } from '@/ui/elements/Action/ActionRoot' ;
7+ import { useCardState } from '@/ui/elements/contexts' ;
58import { ProfileSection } from '@/ui/elements/Section' ;
69import { getFieldError , handleError } from '@/ui/utils/errorHandler' ;
710
811import { generateWeb3Signature , getWeb3Identifier } from '../../../utils/web3' ;
912import { descriptors , Image , localizationKeys , Text } from '../../customizables' ;
1013import { useEnabledThirdPartyProviders } from '../../hooks' ;
1114
12- export const AddWeb3WalletActionMenu = withCardStateProvider ( ( { onClick } : { onClick ?: ( ) => void } ) => {
15+ export const AddWeb3WalletActionMenu = ( ) => {
1316 const card = useCardState ( ) ;
17+ const { open } = useActionContext ( ) ;
1418 const { user } = useUser ( ) ;
1519 const { strategies, strategyToDisplayData } = useEnabledThirdPartyProviders ( ) ;
16-
1720 const enabledStrategies = strategies . filter ( s => s . startsWith ( 'web3' ) ) as Web3Strategy [ ] ;
18- const connectedStrategies = user ?. verifiedWeb3Wallets . map ( w => w . verification . strategy ) as Web3Strategy [ ] ;
21+ const connectedStrategies = user ?. verifiedWeb3Wallets ? .map ( w => w . verification . strategy ) ?? ( [ ] as Web3Strategy [ ] ) ;
1922 const unconnectedStrategies = enabledStrategies . filter ( strategy => {
20- return ! connectedStrategies . includes ( strategy ) ;
23+ return ! connectedStrategies . includes ( strategy ) && strategyToDisplayData [ strategy ] ;
2124 } ) ;
25+
26+ if ( unconnectedStrategies . length === 0 ) {
27+ return null ;
28+ }
29+
2230 const createWeb3Wallet = useReverification ( ( identifier : string ) =>
2331 user ?. createWeb3Wallet ( { web3Wallet : identifier } ) ,
2432 ) ;
2533
26- const connect = async ( strategy : Web3Strategy ) => {
34+ // If the user selects `web3_solana_signature` as their strategy,
35+ // we need to obtain the wallet name to use when connecting and signing the message during the auth flow
36+ //
37+ // Otherwise, our current Web3 providers are all based on the wallet provider name,
38+ // which is sufficient for our current use case when connecting to a wallet.
39+ const connect = async ( { strategy, walletName } : { strategy : Web3Strategy ; walletName ?: string } ) => {
40+ if ( strategy === 'web3_solana_signature' && ! walletName ) {
41+ open ( 'web3Wallets' ) ;
42+ return ;
43+ }
2744 const provider = strategy . replace ( 'web3_' , '' ) . replace ( '_signature' , '' ) as Web3Provider ;
2845 card . setError ( undefined ) ;
2946
3047 try {
3148 card . setLoading ( strategy ) ;
32- const identifier = await getWeb3Identifier ( { provider } ) ;
49+ const identifier = await getWeb3Identifier ( { provider, walletName } ) ;
3350
3451 if ( ! user ) {
3552 throw new Error ( 'user is not defined' ) ;
@@ -38,7 +55,7 @@ export const AddWeb3WalletActionMenu = withCardStateProvider(({ onClick }: { onC
3855 let web3Wallet = await createWeb3Wallet ( identifier ) ;
3956 web3Wallet = await web3Wallet ?. prepareVerification ( { strategy } ) ;
4057 const message = web3Wallet ?. verification . message as string ;
41- const signature = await generateWeb3Signature ( { identifier, nonce : message , provider } ) ;
58+ const signature = await generateWeb3Signature ( { identifier, nonce : message , provider, walletName } ) ;
4259 await web3Wallet ?. attemptVerification ( { signature } ) ;
4360 card . setIdle ( ) ;
4461 } catch ( err ) {
@@ -52,45 +69,48 @@ export const AddWeb3WalletActionMenu = withCardStateProvider(({ onClick }: { onC
5269 }
5370 } ;
5471
55- if ( unconnectedStrategies . length === 0 ) {
56- return null ;
57- }
58-
5972 return (
6073 < >
61- < ProfileSection . ActionMenu
62- id = 'web3Wallets'
63- triggerLocalizationKey = { localizationKeys ( 'userProfile.start.web3WalletsSection.primaryButton' ) }
64- onClick = { onClick }
65- >
66- { unconnectedStrategies . map ( strategy => (
67- < ProfileSection . ActionMenuItem
68- key = { strategy }
69- id = { strategyToDisplayData [ strategy ] . id }
70- onClick = { ( ) => connect ( strategy ) }
71- isLoading = { card . loadingMetadata === strategy }
72- isDisabled = { card . isLoading }
73- localizationKey = { localizationKeys ( 'userProfile.web3WalletPage.web3WalletButtonsBlockButton' , {
74- provider : strategyToDisplayData [ strategy ] . name ,
75- } ) }
76- sx = { t => ( {
77- justifyContent : 'start' ,
78- gap : t . space . $2 ,
79- } ) }
80- leftIcon = {
81- < Image
82- elementDescriptor = { descriptors . providerIcon }
83- elementId = { descriptors . providerIcon . setId ( strategyToDisplayData [ strategy ] . id ) }
84- isLoading = { card . loadingMetadata === strategy }
85- isDisabled = { card . isLoading }
86- src = { strategyToDisplayData [ strategy ] . iconUrl }
87- alt = { `Connect ${ strategyToDisplayData [ strategy ] . name } ` }
88- sx = { theme => ( { width : theme . sizes . $5 } ) }
89- />
90- }
91- />
92- ) ) }
93- </ ProfileSection . ActionMenu >
74+ < Action . Closed value = 'web3Wallets' >
75+ < ProfileSection . ActionMenu
76+ id = 'web3Wallets'
77+ triggerLocalizationKey = { localizationKeys ( 'userProfile.start.web3WalletsSection.primaryButton' ) }
78+ >
79+ { unconnectedStrategies . map ( strategy => (
80+ < ProfileSection . ActionMenuItem
81+ key = { strategy }
82+ id = { strategyToDisplayData [ strategy ] . id }
83+ onClick = { ( ) => connect ( { strategy } ) }
84+ isLoading = { card . loadingMetadata === strategy }
85+ isDisabled = { card . isLoading }
86+ localizationKey = { localizationKeys ( 'userProfile.web3WalletPage.web3WalletButtonsBlockButton' , {
87+ provider : strategyToDisplayData [ strategy ] . name ,
88+ } ) }
89+ sx = { t => ( {
90+ justifyContent : 'start' ,
91+ gap : t . space . $2 ,
92+ } ) }
93+ leftIcon = {
94+ < Image
95+ elementDescriptor = { descriptors . providerIcon }
96+ elementId = { descriptors . providerIcon . setId ( strategyToDisplayData [ strategy ] . id ) }
97+ isLoading = { card . loadingMetadata === strategy }
98+ isDisabled = { card . isLoading }
99+ src = { strategyToDisplayData [ strategy ] . iconUrl }
100+ alt = { `Connect ${ strategyToDisplayData [ strategy ] . name } ` }
101+ sx = { theme => ( { width : theme . sizes . $5 } ) }
102+ />
103+ }
104+ />
105+ ) ) }
106+ </ ProfileSection . ActionMenu >
107+ </ Action . Closed >
108+ < Action . Open value = 'web3Wallets' >
109+ < Action . Card >
110+ < Web3SelectSolanaWalletScreen onConnect = { connect } />
111+ </ Action . Card >
112+ </ Action . Open >
113+
94114 { card . error && (
95115 < Text
96116 colorScheme = 'danger'
@@ -104,4 +124,4 @@ export const AddWeb3WalletActionMenu = withCardStateProvider(({ onClick }: { onC
104124 ) }
105125 </ >
106126 ) ;
107- } ) ;
127+ } ;
0 commit comments