2024-02-28 16:49:44 +00:00
|
|
|
import {
|
|
|
|
AppBar as AppBarMui,
|
|
|
|
Box,
|
|
|
|
Button,
|
|
|
|
Menu,
|
|
|
|
MenuItem,
|
|
|
|
Toolbar,
|
|
|
|
Typography
|
|
|
|
} from '@mui/material'
|
2024-04-08 12:45:51 +00:00
|
|
|
|
2024-02-28 16:49:44 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { useDispatch, useSelector } from 'react-redux'
|
|
|
|
import { setAuthState } from '../../store/actions'
|
|
|
|
import { State } from '../../store/rootReducer'
|
|
|
|
import { Dispatch } from '../../store/store'
|
|
|
|
import Username from '../username'
|
|
|
|
|
2024-05-14 09:27:05 +00:00
|
|
|
import { Link, useNavigate } from 'react-router-dom'
|
2024-02-28 16:49:44 +00:00
|
|
|
import nostrichAvatar from '../../assets/images/avatar.png'
|
2024-03-25 05:30:01 +00:00
|
|
|
import { NostrController } from '../../controllers'
|
2024-05-14 09:27:05 +00:00
|
|
|
import { appPublicRoutes, getProfileRoute } from '../../routes'
|
2024-03-19 10:27:18 +00:00
|
|
|
import {
|
|
|
|
clearAuthToken,
|
|
|
|
saveNsecBunkerDelegatedKey,
|
|
|
|
shorten
|
|
|
|
} from '../../utils'
|
2024-02-28 16:49:44 +00:00
|
|
|
import styles from './style.module.scss'
|
|
|
|
|
|
|
|
export const AppBar = () => {
|
|
|
|
const navigate = useNavigate()
|
2024-05-14 09:27:05 +00:00
|
|
|
|
2024-02-28 16:49:44 +00:00
|
|
|
const dispatch: Dispatch = useDispatch()
|
|
|
|
|
|
|
|
const [username, setUsername] = useState('')
|
|
|
|
const [userAvatar, setUserAvatar] = useState(nostrichAvatar)
|
|
|
|
const [anchorElUser, setAnchorElUser] = useState<null | HTMLElement>(null)
|
|
|
|
|
|
|
|
const authState = useSelector((state: State) => state.auth)
|
|
|
|
const metadataState = useSelector((state: State) => state.metadata)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (metadataState && metadataState.content) {
|
|
|
|
const { picture, display_name, name } = JSON.parse(metadataState.content)
|
|
|
|
|
|
|
|
if (picture) setUserAvatar(picture)
|
|
|
|
|
|
|
|
setUsername(shorten(display_name || name || '', 7))
|
|
|
|
}
|
|
|
|
}, [metadataState])
|
|
|
|
|
|
|
|
const handleOpenUserMenu = (event: React.MouseEvent<HTMLElement>) => {
|
|
|
|
setAnchorElUser(event.currentTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleCloseUserMenu = () => {
|
|
|
|
setAnchorElUser(null)
|
|
|
|
}
|
|
|
|
|
2024-03-01 10:16:35 +00:00
|
|
|
const handleProfile = () => {
|
|
|
|
const hexKey = authState?.usersPubkey
|
|
|
|
if (hexKey) navigate(getProfileRoute(hexKey))
|
|
|
|
|
|
|
|
setAnchorElUser(null)
|
|
|
|
}
|
|
|
|
|
2024-02-28 16:49:44 +00:00
|
|
|
const handleLogout = () => {
|
2024-04-08 12:45:51 +00:00
|
|
|
handleCloseUserMenu()
|
2024-02-28 16:49:44 +00:00
|
|
|
dispatch(
|
|
|
|
setAuthState({
|
|
|
|
loggedIn: false,
|
2024-03-01 10:16:35 +00:00
|
|
|
usersPubkey: undefined,
|
2024-03-22 07:29:23 +00:00
|
|
|
loginMethod: undefined,
|
2024-03-25 05:30:01 +00:00
|
|
|
nsecBunkerPubkey: undefined
|
2024-02-28 16:49:44 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2024-03-19 10:27:18 +00:00
|
|
|
// clear authToken saved in local storage
|
|
|
|
clearAuthToken()
|
|
|
|
|
2024-03-19 07:29:24 +00:00
|
|
|
// update nsecBunker delegated key after logout
|
|
|
|
const nostrController = NostrController.getInstance()
|
|
|
|
const newDelegatedKey = nostrController.generateDelegatedKey()
|
|
|
|
saveNsecBunkerDelegatedKey(newDelegatedKey)
|
|
|
|
|
2024-02-28 16:49:44 +00:00
|
|
|
navigate('/')
|
|
|
|
}
|
|
|
|
const isAuthenticated = authState?.loggedIn === true
|
|
|
|
|
|
|
|
return (
|
2024-05-15 08:50:21 +00:00
|
|
|
<AppBarMui position="fixed" className={styles.AppBar}>
|
2024-02-29 07:05:05 +00:00
|
|
|
<Toolbar className={styles.toolbar}>
|
2024-05-14 09:27:05 +00:00
|
|
|
<Box className={styles.logoWrapper}>
|
2024-05-15 08:50:21 +00:00
|
|
|
<img src="/logo.png" alt="Logo" onClick={() => navigate('/')} />
|
2024-02-28 16:49:44 +00:00
|
|
|
</Box>
|
|
|
|
|
2024-02-29 07:05:05 +00:00
|
|
|
<Box className={styles.rightSideBox}>
|
|
|
|
{!isAuthenticated && (
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
navigate(appPublicRoutes.login)
|
2024-02-28 16:49:44 +00:00
|
|
|
}}
|
2024-05-15 08:50:21 +00:00
|
|
|
variant="contained"
|
2024-02-28 16:49:44 +00:00
|
|
|
>
|
2024-02-29 07:05:05 +00:00
|
|
|
Sign in
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{isAuthenticated && (
|
|
|
|
<>
|
|
|
|
<Username
|
|
|
|
username={username}
|
|
|
|
avatarContent={userAvatar}
|
|
|
|
handleClick={handleOpenUserMenu}
|
|
|
|
/>
|
|
|
|
<Menu
|
2024-05-15 08:50:21 +00:00
|
|
|
id="menu-appbar"
|
2024-02-29 07:05:05 +00:00
|
|
|
anchorEl={anchorElUser}
|
|
|
|
anchorOrigin={{
|
|
|
|
vertical: 'bottom',
|
|
|
|
horizontal: 'center'
|
2024-02-28 16:49:44 +00:00
|
|
|
}}
|
2024-02-29 07:05:05 +00:00
|
|
|
keepMounted
|
|
|
|
transformOrigin={{
|
|
|
|
vertical: 'top',
|
|
|
|
horizontal: 'center'
|
|
|
|
}}
|
|
|
|
open={!!anchorElUser}
|
|
|
|
onClose={handleCloseUserMenu}
|
2024-02-28 16:49:44 +00:00
|
|
|
>
|
|
|
|
<MenuItem
|
2024-02-29 07:05:05 +00:00
|
|
|
sx={{
|
|
|
|
justifyContent: 'center',
|
2024-03-05 09:08:18 +00:00
|
|
|
display: { md: 'none' }
|
2024-02-29 07:05:05 +00:00
|
|
|
}}
|
|
|
|
>
|
2024-05-15 08:50:21 +00:00
|
|
|
<Typography variant="h6">{username}</Typography>
|
2024-03-05 09:08:18 +00:00
|
|
|
</MenuItem>
|
|
|
|
<MenuItem
|
|
|
|
onClick={handleProfile}
|
|
|
|
sx={{
|
|
|
|
justifyContent: 'center'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Profile
|
2024-02-29 07:05:05 +00:00
|
|
|
</MenuItem>
|
|
|
|
<Link
|
|
|
|
to={appPublicRoutes.help}
|
2024-05-15 08:50:21 +00:00
|
|
|
target="_blank"
|
2024-02-29 07:05:05 +00:00
|
|
|
style={{ color: 'inherit', textDecoration: 'inherit' }}
|
|
|
|
>
|
|
|
|
<MenuItem
|
|
|
|
sx={{
|
2024-03-05 09:08:18 +00:00
|
|
|
justifyContent: 'center'
|
2024-02-29 07:05:05 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
Help
|
|
|
|
</MenuItem>
|
|
|
|
</Link>
|
|
|
|
<MenuItem
|
|
|
|
onClick={handleLogout}
|
2024-02-28 16:49:44 +00:00
|
|
|
sx={{
|
2024-03-05 09:08:18 +00:00
|
|
|
justifyContent: 'center'
|
2024-02-28 16:49:44 +00:00
|
|
|
}}
|
|
|
|
>
|
2024-02-29 07:05:05 +00:00
|
|
|
Logout
|
2024-02-28 16:49:44 +00:00
|
|
|
</MenuItem>
|
2024-02-29 07:05:05 +00:00
|
|
|
</Menu>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Box>
|
2024-02-28 16:49:44 +00:00
|
|
|
</Toolbar>
|
|
|
|
</AppBarMui>
|
|
|
|
)
|
|
|
|
}
|