1
1
import { useEffect , useState } from 'react' ;
2
2
import styled , { useTheme } from 'styled-components' ;
3
+ import { wallets } from '@cosmos-kit/leap-extension' ;
4
+ import { QRCodeModal , useWalletConnect } from '@provenanceio/walletconnect-js' ;
5
+ import { Link as BaseLink } from 'react-router-dom' ;
3
6
// @ts -ignore
4
7
import useOnClickOutside from 'react-tiny-hooks/use-on-click-outside' ;
5
8
// @ts -ignore
@@ -8,13 +11,16 @@ import useOnEscape from 'react-tiny-hooks/use-on-escape';
8
11
import useToggle from 'react-tiny-hooks/use-toggle' ;
9
12
import { useChain } from '@cosmos-kit/react' ;
10
13
import { cosmos } from '@provlabs/provenancejs' ;
14
+ import { maxLength } from '../../utils' ;
11
15
import { signJWT } from '../../utils/jwt' ;
12
16
import { CHAIN_NAME } from '../../config' ;
13
- import { ICON_NAMES } from '../../consts' ;
17
+ import { breakpoints , ICON_NAMES , isProd } from '../../consts' ;
14
18
import { useApp } from '../../redux/hooks' ;
15
19
import { PopupNote } from '../../Components/PopupNote' ;
16
20
import Button from '../Button' ;
17
21
import Sprite from '../Sprite' ;
22
+ import Modal from '../../Components/Modal' ;
23
+ import figureWallet from '../../assets/figureMobileWalletIcon.png' ;
18
24
19
25
const Container = styled . div `
20
26
position: relative;
@@ -30,59 +36,128 @@ const AccountBtn = styled(Button)<{ isLoggedIn?: boolean }>`
30
36
animation-iteration-count: ${ ( { isLoggedIn } ) => ( isLoggedIn ? 0 : 2 ) } ;
31
37
` ;
32
38
39
+ const PopupTxt = styled . p `
40
+ text-align: center;
41
+ @media ${ breakpoints . up ( 'md' ) } {
42
+ white-space: nowrap;
43
+ }
44
+ ` ;
45
+
46
+ const LogoutButton = styled ( Button ) `
47
+ float: right;
48
+ ` ;
49
+
50
+ const Link = styled ( BaseLink ) `
51
+ &&& {
52
+ :hover {
53
+ opacity: 1;
54
+ text-decoration: underline;
55
+ }
56
+ :visited {
57
+ color: ${ ( { theme } ) => theme . FONT_NAV_VISITED } ;
58
+ }
59
+ color: ${ ( { theme } ) => theme . FONT_NAV } ;
60
+ }
61
+ ` ;
62
+
63
+ const ModalContent = styled . div `
64
+ display: grid;
65
+ grid-template-columns: 1fr;
66
+ justify-content: center;
67
+ justify-items: center;
68
+ grid-auto-columns: max-content;
69
+ @media ${ breakpoints . up ( 'md' ) } {
70
+ grid-template-columns: 1fr 1fr;
71
+ }
72
+ ` ;
73
+
74
+ const ModalTitle = styled . div `
75
+ font-weight: ${ ( { theme } ) => theme . FONT_WEIGHT_BOLD } ;
76
+ margin: 5px 0 10px 0;
77
+ font-size: 1.063rem;
78
+ ` ;
79
+
80
+ const ModalWalletButton = styled . div `
81
+ padding: 0 20px 20px 20px;
82
+ border-radius: 8px;
83
+ font-size: 1rem;
84
+ width: 100%;
85
+ font-weight: ${ ( { theme } ) => theme . FONT_WEIGHT_BOLD } ;
86
+ display: flex;
87
+ flex-direction: column;
88
+ text-align: center;
89
+ justify-items: center;
90
+ justify-content: center;
91
+ max-width: 200px;
92
+ cursor: pointer;
93
+ :hover {
94
+ background-color: ${ ( { theme } ) => theme . BACKGROUND_LIGHT } ;
95
+ }
96
+ ` ;
97
+
98
+ const WalletTitle = styled . p `
99
+ margin-bottom: 4px;
100
+ ` ;
101
+
33
102
const UserAccount = ( { isMobile } : { isMobile : boolean } ) => {
34
- const { isLoggedIn, setIsLoggedIn, setWalletAddress } = useApp ( ) ;
103
+ const { isLoggedIn, setIsLoggedIn, setWalletAddress, setAuthToken , authToken , setWalletUrl } = useApp ( ) ;
35
104
const theme = useTheme ( ) ;
36
105
const position = isMobile ? 'above' : 'left' ;
37
106
const [ visible , setVisible ] = useState ( false ) ;
38
107
39
- const [ , , , deactivateShowPopup ] = useToggle ( ) ;
108
+ const [ showPopup , toggleShowPopup , , deactivateShowPopup ] = useToggle ( ) ;
40
109
const containerRef = useOnClickOutside ( deactivateShowPopup ) ;
41
110
useOnEscape ( deactivateShowPopup ) ;
42
-
43
- const { status, connect, address, signArbitrary, getSigningStargateClient, getAccount, wallet } =
44
- useChain ( CHAIN_NAME ) ;
45
- const { setAuthToken, authToken } = useApp ( ) ;
111
+ // This is the old Figure Wallet stuff
112
+ const { walletConnectService : wcs , walletConnectState } = useWalletConnect ( ) ;
113
+ // This is Leap
114
+ const {
115
+ status,
116
+ connect,
117
+ address,
118
+ signArbitrary,
119
+ isWalletDisconnected,
120
+ wallet,
121
+ getSigningStargateClient,
122
+ getAccount,
123
+ } = useChain ( CHAIN_NAME ) ;
46
124
const provJWT = localStorage . getItem ( 'provenanceJWT' ) ;
47
125
const jwtInfo = provJWT ? JSON . parse ( provJWT ) : '' ;
48
126
const signedJWT = jwtInfo . expires < Date . now ( ) / 1000 ? '' : jwtInfo . jwt ;
49
127
50
128
useEffect ( ( ) => {
51
- if ( status === 'Disconnected' ) {
129
+ if ( isWalletDisconnected && ! walletConnectState . connected ) {
52
130
localStorage . removeItem ( 'provenanceJWT' ) ;
53
131
setIsLoggedIn ( false ) ;
54
- setAuthToken ( '' ) ;
55
132
setWalletAddress ( '' ) ;
56
133
}
57
- } , [ setAuthToken , setWalletAddress , status , setIsLoggedIn ] ) ;
134
+ } , [ setAuthToken , setWalletAddress , status , isWalletDisconnected , setIsLoggedIn , walletConnectState ] ) ;
58
135
59
136
useEffect ( ( ) => {
60
137
if ( jwtInfo && jwtInfo . expires < Date . now ( ) / 1000 ) {
61
138
localStorage . removeItem ( 'provenanceJWT' ) ;
62
- setIsLoggedIn ( false ) ;
63
- setAuthToken ( '' ) ;
64
139
setWalletAddress ( '' ) ;
140
+ setIsLoggedIn ( false ) ;
65
141
}
66
- } , [ jwtInfo , setAuthToken , setIsLoggedIn , setWalletAddress ] ) ;
142
+ } , [ jwtInfo , setIsLoggedIn , setWalletAddress ] ) ;
67
143
68
144
useEffect ( ( ) => {
69
- if ( signedJWT ) {
145
+ if ( signedJWT && status === 'Connected' ) {
70
146
setAuthToken ( signedJWT ) ;
71
147
}
72
- } , [ setAuthToken , signedJWT ] ) ;
148
+ } , [ setAuthToken , signedJWT , status ] ) ;
73
149
74
150
useEffect ( ( ) => {
75
- setIsLoggedIn ( status === 'Connected' ) ;
76
- if ( address ) {
77
- setWalletAddress ( address ) ;
151
+ setIsLoggedIn ( status === 'Connected' || walletConnectState . status === 'connected' ) ;
152
+ if ( address || walletConnectState . address ) {
153
+ setWalletAddress ( address || walletConnectState . address ) ;
78
154
}
79
- } , [ status , address , setIsLoggedIn , setWalletAddress ] ) ;
155
+ } , [ status , address , setIsLoggedIn , setWalletAddress , walletConnectState ] ) ;
80
156
81
- // This is the effect that signs the local JWT to access the explorer service
157
+ // This only occurs when connecting to Leap wallets
82
158
useEffect ( ( ) => {
83
159
const initialSigningEvent = async ( ) => {
84
160
if (
85
- ! authToken &&
86
161
status === 'Connected' &&
87
162
address &&
88
163
wallet &&
@@ -161,9 +236,24 @@ const UserAccount = ({ isMobile }: { isMobile: boolean }) => {
161
236
] ) ;
162
237
163
238
const handleLoginClick = ( ) => {
164
- connect ( ) ;
239
+ if ( wallet ?. name . includes ( 'leap' ) ) {
240
+ connect ( )
241
+ } else {
242
+ toggleShowPopup ( ) ;
243
+ wcs . connect ( ) ;
244
+ }
165
245
} ;
166
246
247
+ const handleLogoutFigureWallet = ( ) => {
248
+ setWalletAddress ( '' )
249
+ setWalletUrl ( '' )
250
+ setIsLoggedIn ( false )
251
+ wcs . disconnect ( ) ;
252
+ setVisible ( false ) ;
253
+ }
254
+
255
+ const [ openSelectWalletModal , setOpenSelectWalletModal ] = useState ( false ) ;
256
+
167
257
return (
168
258
< Container
169
259
ref = { containerRef }
@@ -173,13 +263,68 @@ const UserAccount = ({ isMobile }: { isMobile: boolean }) => {
173
263
< PopupNote show = { ! isLoggedIn && visible } position = { position } zIndex = "201" >
174
264
Login
175
265
</ PopupNote >
176
- < AccountBtn onClick = { handleLoginClick } isLoggedIn = { isLoggedIn } >
266
+ < AccountBtn
267
+ onClick = { isLoggedIn ? handleLoginClick : ( ) => setOpenSelectWalletModal ( true ) }
268
+ isLoggedIn = { isLoggedIn }
269
+ >
177
270
< Sprite
178
271
icon = { isLoggedIn ? ICON_NAMES . ACCOUNT : ICON_NAMES . KEY }
179
272
color = { theme . FONT_NAV }
180
273
size = "20px"
181
274
/>
182
275
</ AccountBtn >
276
+ { isLoggedIn && (
277
+ < PopupNote show = { showPopup } position = { position } delay = { 0 } zIndex = "201" >
278
+ < PopupTxt > You are currently logged in as</ PopupTxt >
279
+ < PopupTxt >
280
+ < Link to = { `/accounts/${ walletConnectState . address } ` } >
281
+ { isMobile ? maxLength ( walletConnectState . address , 11 , '3' ) : walletConnectState . address }
282
+ </ Link >
283
+ </ PopupTxt >
284
+ < LogoutButton color = "secondary" onClick = { handleLogoutFigureWallet } icon = { ICON_NAMES . LOGOUT } >
285
+ Sign Out
286
+ </ LogoutButton >
287
+ </ PopupNote >
288
+ ) }
289
+ < Modal isOpen = { openSelectWalletModal } onClose = { ( ) => setOpenSelectWalletModal ( false ) } >
290
+ < div >
291
+ < ModalTitle > Select a Wallet Provider</ ModalTitle >
292
+ < ModalContent >
293
+ < ModalWalletButton
294
+ onClick = { ( ) => {
295
+ setOpenSelectWalletModal ( false ) ;
296
+ connect ( ) ;
297
+ } }
298
+ >
299
+ < WalletTitle > Leap Wallet</ WalletTitle >
300
+ < img src = { wallets [ 0 ] . walletInfo . logo as string } alt = "Leap Wallet" />
301
+ </ ModalWalletButton >
302
+ < ModalWalletButton
303
+ onClick = { ( ) => {
304
+ setOpenSelectWalletModal ( false ) ;
305
+ wcs . connect ( ) ;
306
+ } }
307
+ >
308
+ < WalletTitle > Figure Wallet</ WalletTitle >
309
+ < img src = { figureWallet } alt = "Figure Wallet" />
310
+ </ ModalWalletButton >
311
+ </ ModalContent >
312
+ </ div >
313
+ </ Modal >
314
+ < QRCodeModal
315
+ walletConnectService = { wcs }
316
+ title = "Scan the QRCode with your mobile Provenance Blockchain Wallet."
317
+ className = "QR-Code-Modal"
318
+ devWallets = { [
319
+ 'figure_mobile_test' ,
320
+ 'figure_hosted_test' ,
321
+ // @ts -ignore
322
+ 'provenance_extension' ,
323
+ // @ts -ignore
324
+ 'provenance_mobile' ,
325
+ ] }
326
+ hideWallets = { isProd ? [ 'figure_hosted_test' ] : [ 'figure_hosted' ] }
327
+ />
183
328
</ Container >
184
329
) ;
185
330
} ;
0 commit comments