11'use client'
22
3- import React from 'react'
3+ import React , { useContext , useEffect } from 'react'
44
55import MenuIcon from '@mui/icons-material/Menu'
66import Menu from '@mui/material/Menu'
@@ -14,19 +14,20 @@ import {
1414 useSelector ,
1515 selectWalletAddress ,
1616 selectIsLoggedIn ,
17- selectUsername ,
18- setUsername ,
1917 setWalletAddress ,
2018 setIsLoggedIn ,
2119} from '@/lib/redux'
20+ import { usePrivy } from '@privy-io/react-auth' ;
21+ import { PrivyAuthContext } from '../../../lib/PrivyContext' ;
2222
2323export const TopNav = ( ) => {
2424 const dispatch = useDispatch ( )
2525 const router = useRouter ( )
26- const isLoggedIn = useSelector ( selectIsLoggedIn )
27- const username = useSelector ( selectUsername )
26+ const { ready, authenticated, user, exportWallet } = usePrivy ( ) ;
2827 const walletAddress = useSelector ( selectWalletAddress )
2928
29+ const { logout } = usePrivy ( ) ;
30+
3031 // State and handlers for the dropdown menu
3132 const [ anchorEl , setAnchorEl ] = React . useState < null | SVGSVGElement > ( null )
3233
@@ -42,26 +43,30 @@ export const TopNav = () => {
4243 router . push ( path )
4344 }
4445
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+ }
5452 }
5553
54+ const handleLogout = async ( ) => {
55+ logout ( ) ;
56+ localStorage . removeItem ( 'walletAddress' ) ;
57+ dispatch ( setWalletAddress ( '' ) ) ;
58+ dispatch ( setIsLoggedIn ( false ) ) ;
59+ handleClose ( ) ;
60+ router . push ( '/login' ) ;
61+ }
5662
5763 return (
5864 < nav className = { styles . navbar } >
5965 < span className = { styles . link } onClick = { ( ) => handleNavigation ( '/' ) } >
6066 plex
6167 </ span >
62- { isLoggedIn && (
68+ { ready && authenticated && (
6369 < div className = { styles . userContainer } >
64- < span className = { styles . username } > { username } </ span >
6570 < MenuIcon style = { { color : 'white' , marginLeft : '10px' } } onClick = { ( e : any ) => handleClick ( e ) } />
6671 < Menu
6772 anchorEl = { anchorEl }
@@ -70,11 +75,18 @@ export const TopNav = () => {
7075 onClose = { handleClose }
7176 >
7277 < 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 >
7386 < MenuItem onClick = { handleLogout } > Logout</ MenuItem >
7487 </ Menu >
7588 </ div >
7689 ) }
77- { /* Other links or elements can be added here if required */ }
7890 </ nav >
7991 )
80- }
92+ }
0 commit comments