1
1
'use client'
2
2
3
- import React from 'react'
3
+ import React , { useContext , useEffect } from 'react'
4
4
5
5
import MenuIcon from '@mui/icons-material/Menu'
6
6
import Menu from '@mui/material/Menu'
@@ -14,19 +14,20 @@ import {
14
14
useSelector ,
15
15
selectWalletAddress ,
16
16
selectIsLoggedIn ,
17
- selectUsername ,
18
- setUsername ,
19
17
setWalletAddress ,
20
18
setIsLoggedIn ,
21
19
} from '@/lib/redux'
20
+ import { usePrivy } from '@privy-io/react-auth' ;
21
+ import { PrivyAuthContext } from '../../../lib/PrivyContext' ;
22
22
23
23
export const TopNav = ( ) => {
24
24
const dispatch = useDispatch ( )
25
25
const router = useRouter ( )
26
- const isLoggedIn = useSelector ( selectIsLoggedIn )
27
- const username = useSelector ( selectUsername )
26
+ const { ready, authenticated, user, exportWallet } = usePrivy ( ) ;
28
27
const walletAddress = useSelector ( selectWalletAddress )
29
28
29
+ const { logout } = usePrivy ( ) ;
30
+
30
31
// State and handlers for the dropdown menu
31
32
const [ anchorEl , setAnchorEl ] = React . useState < null | SVGSVGElement > ( null )
32
33
@@ -42,26 +43,30 @@ export const TopNav = () => {
42
43
router . push ( path )
43
44
}
44
45
45
- const handleLogout = ( ) => {
46
- // Clear data from localStorage
47
- localStorage . removeItem ( 'username' )
48
- localStorage . removeItem ( 'walletAddress' )
49
- dispatch ( setUsername ( '' ) )
50
- dispatch ( setWalletAddress ( '' ) )
51
- dispatch ( setIsLoggedIn ( false ) )
52
- handleClose ( )
53
- router . push ( '/login' )
46
+ const hasEmbeddedWallet = ready && authenticated && ! ! user ?. linkedAccounts . find ( ( account : any ) => account . type === 'wallet' && account . walletClient === 'privy' ) ;
47
+
48
+ const handleExportWallet = async ( ) => {
49
+ if ( hasEmbeddedWallet ) {
50
+ exportWallet ( ) ;
51
+ }
54
52
}
55
53
54
+ const handleLogout = async ( ) => {
55
+ logout ( ) ;
56
+ localStorage . removeItem ( 'walletAddress' ) ;
57
+ dispatch ( setWalletAddress ( '' ) ) ;
58
+ dispatch ( setIsLoggedIn ( false ) ) ;
59
+ handleClose ( ) ;
60
+ router . push ( '/login' ) ;
61
+ }
56
62
57
63
return (
58
64
< nav className = { styles . navbar } >
59
65
< span className = { styles . link } onClick = { ( ) => handleNavigation ( '/' ) } >
60
66
plex
61
67
</ span >
62
- { isLoggedIn && (
68
+ { ready && authenticated && (
63
69
< div className = { styles . userContainer } >
64
- < span className = { styles . username } > { username } </ span >
65
70
< MenuIcon style = { { color : 'white' , marginLeft : '10px' } } onClick = { ( e : any ) => handleClick ( e ) } />
66
71
< Menu
67
72
anchorEl = { anchorEl }
@@ -70,11 +75,18 @@ export const TopNav = () => {
70
75
onClose = { handleClose }
71
76
>
72
77
< MenuItem onClick = { handleClose } > Wallet: { walletAddress } </ MenuItem >
78
+ < div title = { ! hasEmbeddedWallet ? 'Export wallet only available for embedded wallets.' : '' } >
79
+ < MenuItem
80
+ onClick = { handleExportWallet }
81
+ disabled = { ! hasEmbeddedWallet }
82
+ >
83
+ Export Wallet
84
+ </ MenuItem >
85
+ </ div >
73
86
< MenuItem onClick = { handleLogout } > Logout</ MenuItem >
74
87
</ Menu >
75
88
</ div >
76
89
) }
77
- { /* Other links or elements can be added here if required */ }
78
90
</ nav >
79
91
)
80
- }
92
+ }
0 commit comments