sigit.io/src/components/AppBar/AppBar.tsx

175 lines
4.8 KiB
TypeScript
Raw Normal View History

import {
AppBar as AppBarMui,
Box,
Button,
Menu,
MenuItem,
Toolbar,
Typography
} from '@mui/material'
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'
import nostrichAvatar from '../../assets/images/avatar.png'
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'
import styles from './style.module.scss'
export const AppBar = () => {
const navigate = useNavigate()
2024-05-14 09:27:05 +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)
}
const handleLogout = () => {
handleCloseUserMenu()
dispatch(
setAuthState({
loggedIn: false,
2024-03-01 10:16:35 +00:00
usersPubkey: undefined,
loginMethod: undefined,
nsecBunkerPubkey: undefined
})
)
2024-03-19 10:27:18 +00:00
// clear authToken saved in local storage
clearAuthToken()
// update nsecBunker delegated key after logout
const nostrController = NostrController.getInstance()
const newDelegatedKey = nostrController.generateDelegatedKey()
saveNsecBunkerDelegatedKey(newDelegatedKey)
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('/')} />
</Box>
2024-02-29 07:05:05 +00:00
<Box className={styles.rightSideBox}>
{!isAuthenticated && (
<Button
onClick={() => {
navigate(appPublicRoutes.login)
}}
2024-05-15 08:50:21 +00:00
variant="contained"
>
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-29 07:05:05 +00:00
keepMounted
transformOrigin={{
vertical: 'top',
horizontal: 'center'
}}
open={!!anchorElUser}
onClose={handleCloseUserMenu}
>
<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}
sx={{
2024-03-05 09:08:18 +00:00
justifyContent: 'center'
}}
>
2024-02-29 07:05:05 +00:00
Logout
</MenuItem>
2024-02-29 07:05:05 +00:00
</Menu>
</>
)}
</Box>
</Toolbar>
</AppBarMui>
)
}