2024-02-28 21:49:44 +05:00
|
|
|
import {
|
|
|
|
AppBar as AppBarMui,
|
|
|
|
Box,
|
|
|
|
Button,
|
|
|
|
Menu,
|
|
|
|
MenuItem,
|
|
|
|
Toolbar,
|
|
|
|
Typography
|
|
|
|
} from '@mui/material'
|
2024-04-08 17:45:51 +05:00
|
|
|
|
2024-02-28 21:49:44 +05:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { useDispatch, useSelector } from 'react-redux'
|
2024-05-28 12:22:31 +03:00
|
|
|
import {
|
|
|
|
setAuthState,
|
|
|
|
setMetadataEvent,
|
|
|
|
userLogOutAction
|
|
|
|
} from '../../store/actions'
|
2024-02-28 21:49:44 +05:00
|
|
|
import { State } from '../../store/rootReducer'
|
|
|
|
import { Dispatch } from '../../store/store'
|
|
|
|
import Username from '../username'
|
|
|
|
|
2024-05-14 14:27:05 +05:00
|
|
|
import { Link, useNavigate } from 'react-router-dom'
|
2024-05-16 16:45:00 +02:00
|
|
|
import { MetadataController, NostrController } from '../../controllers'
|
2024-05-28 12:22:31 +03:00
|
|
|
import {
|
|
|
|
appPublicRoutes,
|
|
|
|
appPrivateRoutes,
|
|
|
|
getProfileRoute
|
|
|
|
} from '../../routes'
|
2024-03-19 15:27:18 +05:00
|
|
|
import {
|
|
|
|
clearAuthToken,
|
2024-05-30 22:36:17 +05:00
|
|
|
hexToNpub,
|
2024-03-19 15:27:18 +05:00
|
|
|
saveNsecBunkerDelegatedKey,
|
|
|
|
shorten
|
|
|
|
} from '../../utils'
|
2024-02-28 21:49:44 +05:00
|
|
|
import styles from './style.module.scss'
|
2024-05-17 13:33:01 +02:00
|
|
|
import { setUserRobotImage } from '../../store/userRobotImage/action'
|
2024-07-29 11:22:04 +02:00
|
|
|
import { Container } from '../Container'
|
2024-07-29 14:39:30 +02:00
|
|
|
import { ButtonIcon } from '../ButtonIcon'
|
2024-09-19 09:54:19 +02:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
|
|
|
import { faClose } from '@fortawesome/free-solid-svg-icons'
|
|
|
|
import useMediaQuery from '@mui/material/useMediaQuery'
|
2024-02-28 21:49:44 +05:00
|
|
|
|
2024-09-13 17:57:19 +02:00
|
|
|
const metadataController = MetadataController.getInstance()
|
2024-05-16 16:45:00 +02:00
|
|
|
|
2024-02-28 21:49:44 +05:00
|
|
|
export const AppBar = () => {
|
|
|
|
const navigate = useNavigate()
|
2024-05-14 14:27:05 +05:00
|
|
|
|
2024-02-28 21:49:44 +05:00
|
|
|
const dispatch: Dispatch = useDispatch()
|
|
|
|
|
|
|
|
const [username, setUsername] = useState('')
|
2024-05-15 13:57:54 +02:00
|
|
|
const [userAvatar, setUserAvatar] = useState('')
|
2024-02-28 21:49:44 +05:00
|
|
|
const [anchorElUser, setAnchorElUser] = useState<null | HTMLElement>(null)
|
|
|
|
|
|
|
|
const authState = useSelector((state: State) => state.auth)
|
|
|
|
const metadataState = useSelector((state: State) => state.metadata)
|
2024-05-17 13:33:01 +02:00
|
|
|
const userRobotImage = useSelector((state: State) => state.userRobotImage)
|
2024-02-28 21:49:44 +05:00
|
|
|
|
|
|
|
useEffect(() => {
|
2024-05-16 16:45:00 +02:00
|
|
|
if (metadataState) {
|
|
|
|
if (metadataState.content) {
|
2024-05-17 13:35:37 +02:00
|
|
|
const { picture, display_name, name } = JSON.parse(
|
|
|
|
metadataState.content
|
|
|
|
)
|
|
|
|
|
2024-05-17 13:33:01 +02:00
|
|
|
if (picture || userRobotImage) {
|
|
|
|
setUserAvatar(picture || userRobotImage)
|
2024-05-16 16:45:00 +02:00
|
|
|
}
|
2024-05-17 13:59:03 +05:00
|
|
|
|
2024-05-30 22:36:17 +05:00
|
|
|
const npub = authState.usersPubkey
|
|
|
|
? hexToNpub(authState.usersPubkey)
|
|
|
|
: ''
|
|
|
|
|
|
|
|
setUsername(shorten(display_name || name || npub, 7))
|
2024-05-15 13:57:54 +02:00
|
|
|
} else {
|
2024-05-17 13:33:01 +02:00
|
|
|
setUserAvatar(userRobotImage || '')
|
2024-05-16 16:45:00 +02:00
|
|
|
setUsername('')
|
2024-05-15 13:57:54 +02:00
|
|
|
}
|
2024-02-28 21:49:44 +05:00
|
|
|
}
|
2024-05-30 22:36:17 +05:00
|
|
|
}, [metadataState, userRobotImage, authState.usersPubkey])
|
2024-02-28 21:49:44 +05:00
|
|
|
|
|
|
|
const handleOpenUserMenu = (event: React.MouseEvent<HTMLElement>) => {
|
|
|
|
setAnchorElUser(event.currentTarget)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleCloseUserMenu = () => {
|
|
|
|
setAnchorElUser(null)
|
|
|
|
}
|
|
|
|
|
2024-03-01 15:16:35 +05:00
|
|
|
const handleProfile = () => {
|
|
|
|
const hexKey = authState?.usersPubkey
|
|
|
|
if (hexKey) navigate(getProfileRoute(hexKey))
|
|
|
|
|
|
|
|
setAnchorElUser(null)
|
|
|
|
}
|
|
|
|
|
2024-02-28 21:49:44 +05:00
|
|
|
const handleLogout = () => {
|
2024-04-08 17:45:51 +05:00
|
|
|
handleCloseUserMenu()
|
2024-02-28 21:49:44 +05:00
|
|
|
dispatch(
|
|
|
|
setAuthState({
|
2024-05-16 16:45:00 +02:00
|
|
|
keyPair: undefined,
|
2024-02-28 21:49:44 +05:00
|
|
|
loggedIn: false,
|
2024-03-01 15:16:35 +05:00
|
|
|
usersPubkey: undefined,
|
2024-03-22 12:29:23 +05:00
|
|
|
loginMethod: undefined,
|
2024-03-25 10:30:01 +05:00
|
|
|
nsecBunkerPubkey: undefined
|
2024-02-28 21:49:44 +05:00
|
|
|
})
|
|
|
|
)
|
2024-05-17 13:35:37 +02:00
|
|
|
dispatch(setMetadataEvent(metadataController.getEmptyMetadataEvent()))
|
|
|
|
dispatch(setUserRobotImage(null))
|
2024-05-16 16:45:00 +02:00
|
|
|
|
2024-03-19 15:27:18 +05:00
|
|
|
// clear authToken saved in local storage
|
|
|
|
clearAuthToken()
|
2024-05-28 12:22:31 +03:00
|
|
|
|
|
|
|
dispatch(userLogOutAction())
|
2024-03-19 15:27:18 +05:00
|
|
|
|
2024-03-19 12:29:24 +05:00
|
|
|
// update nsecBunker delegated key after logout
|
|
|
|
const nostrController = NostrController.getInstance()
|
|
|
|
const newDelegatedKey = nostrController.generateDelegatedKey()
|
|
|
|
saveNsecBunkerDelegatedKey(newDelegatedKey)
|
|
|
|
|
2024-02-28 21:49:44 +05:00
|
|
|
navigate('/')
|
|
|
|
}
|
|
|
|
const isAuthenticated = authState?.loggedIn === true
|
|
|
|
|
2024-09-19 09:54:19 +02:00
|
|
|
const matches = useMediaQuery('(max-width:767px)')
|
|
|
|
const [isBannerVisible, setIsBannerVisible] = useState(true)
|
|
|
|
const handleBannerHide = () => {
|
|
|
|
setIsBannerVisible(false)
|
|
|
|
}
|
|
|
|
|
2024-02-28 21:49:44 +05:00
|
|
|
return (
|
2024-09-19 09:54:19 +02:00
|
|
|
<>
|
|
|
|
{isAuthenticated && isBannerVisible && (
|
|
|
|
<div className={styles.banner}>
|
|
|
|
<Container>
|
|
|
|
<div className={styles.bannerInner}>
|
|
|
|
SIGit is currently Alpha software (available for internal
|
|
|
|
testing), use at your own risk!
|
2024-07-25 15:05:47 +02:00
|
|
|
<Button
|
2024-09-19 09:54:19 +02:00
|
|
|
aria-label={`close banner`}
|
|
|
|
variant="text"
|
|
|
|
onClick={handleBannerHide}
|
2024-02-28 21:49:44 +05:00
|
|
|
>
|
2024-09-19 09:54:19 +02:00
|
|
|
<FontAwesomeIcon icon={faClose} />
|
2024-07-25 15:05:47 +02:00
|
|
|
</Button>
|
2024-09-19 09:54:19 +02:00
|
|
|
</div>
|
|
|
|
</Container>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<AppBarMui
|
|
|
|
position={matches ? 'sticky' : 'static'}
|
|
|
|
className={styles.AppBar}
|
|
|
|
sx={{
|
|
|
|
boxShadow: 'none'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Container>
|
|
|
|
<Toolbar className={styles.toolbar} disableGutters={true}>
|
|
|
|
<Box className={styles.logoWrapper}>
|
|
|
|
<img src="/logo.svg" alt="Logo" onClick={() => navigate('/')} />
|
|
|
|
</Box>
|
|
|
|
|
|
|
|
<Box className={styles.rightSideBox}>
|
|
|
|
{!isAuthenticated && (
|
|
|
|
<Button
|
|
|
|
startIcon={<ButtonIcon />}
|
|
|
|
onClick={() => {
|
|
|
|
navigate(appPublicRoutes.nostr)
|
2024-03-05 14:08:18 +05:00
|
|
|
}}
|
2024-09-19 09:54:19 +02:00
|
|
|
variant="contained"
|
2024-03-05 14:08:18 +05:00
|
|
|
>
|
2024-09-19 09:54:19 +02:00
|
|
|
LOGIN
|
|
|
|
</Button>
|
|
|
|
)}
|
2024-05-28 12:22:31 +03:00
|
|
|
|
2024-09-19 09:54:19 +02:00
|
|
|
{isAuthenticated && (
|
|
|
|
<>
|
|
|
|
<Username
|
|
|
|
username={username}
|
|
|
|
avatarContent={userAvatar}
|
|
|
|
handleClick={handleOpenUserMenu}
|
|
|
|
/>
|
|
|
|
<Menu
|
|
|
|
id="menu-appbar"
|
|
|
|
anchorEl={anchorElUser}
|
|
|
|
anchorOrigin={{
|
|
|
|
vertical: 'bottom',
|
|
|
|
horizontal: 'center'
|
2024-07-25 15:05:47 +02:00
|
|
|
}}
|
2024-09-19 09:54:19 +02:00
|
|
|
keepMounted
|
|
|
|
transformOrigin={{
|
|
|
|
vertical: 'top',
|
|
|
|
horizontal: 'center'
|
2024-07-25 15:05:47 +02:00
|
|
|
}}
|
2024-09-19 09:54:19 +02:00
|
|
|
open={!!anchorElUser}
|
|
|
|
onClose={handleCloseUserMenu}
|
2024-07-25 15:05:47 +02:00
|
|
|
>
|
2024-09-19 09:54:19 +02:00
|
|
|
<MenuItem
|
|
|
|
sx={{
|
|
|
|
justifyContent: 'center',
|
|
|
|
display: { md: 'none' },
|
|
|
|
fontWeight: 500,
|
|
|
|
fontSize: '14px',
|
|
|
|
color: 'var(--text-color)'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Typography variant="h6">{username}</Typography>
|
|
|
|
</MenuItem>
|
|
|
|
<MenuItem
|
|
|
|
onClick={handleProfile}
|
|
|
|
sx={{
|
|
|
|
justifyContent: 'center'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Profile
|
|
|
|
</MenuItem>
|
|
|
|
<MenuItem
|
|
|
|
onClick={() => {
|
|
|
|
setAnchorElUser(null)
|
2024-06-03 19:46:42 +05:00
|
|
|
|
2024-09-19 09:54:19 +02:00
|
|
|
navigate(appPrivateRoutes.settings)
|
|
|
|
}}
|
|
|
|
sx={{
|
|
|
|
justifyContent: 'center'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Settings
|
|
|
|
</MenuItem>
|
|
|
|
<MenuItem
|
|
|
|
onClick={() => {
|
|
|
|
setAnchorElUser(null)
|
|
|
|
|
|
|
|
navigate(appPublicRoutes.verify)
|
|
|
|
}}
|
|
|
|
sx={{
|
|
|
|
justifyContent: 'center'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Verify
|
|
|
|
</MenuItem>
|
|
|
|
<Link
|
|
|
|
to={appPublicRoutes.source}
|
|
|
|
target="_blank"
|
|
|
|
style={{ color: 'inherit', textDecoration: 'inherit' }}
|
|
|
|
>
|
|
|
|
<MenuItem
|
|
|
|
sx={{
|
|
|
|
justifyContent: 'center'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Source
|
|
|
|
</MenuItem>
|
|
|
|
</Link>
|
2024-07-25 15:05:47 +02:00
|
|
|
<MenuItem
|
2024-09-19 09:54:19 +02:00
|
|
|
onClick={handleLogout}
|
2024-07-25 15:05:47 +02:00
|
|
|
sx={{
|
|
|
|
justifyContent: 'center'
|
|
|
|
}}
|
|
|
|
>
|
2024-09-19 09:54:19 +02:00
|
|
|
Logout
|
2024-07-25 15:05:47 +02:00
|
|
|
</MenuItem>
|
2024-09-19 09:54:19 +02:00
|
|
|
</Menu>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</Toolbar>
|
|
|
|
</Container>
|
|
|
|
</AppBarMui>
|
|
|
|
</>
|
2024-02-28 21:49:44 +05:00
|
|
|
)
|
|
|
|
}
|