sigit.io/src/components/username.tsx

96 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-05-16 11:22:05 +00:00
import { Typography, IconButton, Box, useTheme } from '@mui/material'
import { useSelector } from 'react-redux'
import { State } from '../store/rootReducer'
2024-05-16 11:22:05 +00:00
import { hexToNpub } from '../utils'
import { Link } from 'react-router-dom'
import { getProfileRoute } from '../routes'
type Props = {
username: string
avatarContent: string
handleClick: (event: React.MouseEvent<HTMLElement>) => void
}
const Username = ({ username, avatarContent, handleClick }: Props) => {
const hexKey = useSelector((state: State) => state.auth.usersPubkey)
return (
<IconButton
2024-05-15 08:50:21 +00:00
aria-label="account of current user"
aria-controls="menu-appbar"
aria-haspopup="true"
onClick={handleClick}
2024-05-15 08:50:21 +00:00
color="inherit"
>
<img
src={avatarContent}
2024-05-15 08:50:21 +00:00
alt="user-avatar"
className="profile-image"
style={{
borderWidth: '3px',
borderStyle: hexKey ? 'solid' : 'none',
borderColor: `#${hexKey?.substring(0, 6)}`
}}
/>
<Typography
2024-05-15 08:50:21 +00:00
variant="h6"
sx={{
color: '#3e3e3e',
padding: '0 8px',
display: { xs: 'none', md: 'flex' }
}}
>
{username}
</Typography>
</IconButton>
)
}
export default Username
2024-05-16 11:22:05 +00:00
type UserProps = {
pubkey: string
name: string
image?: string
}
2024-05-17 08:59:03 +00:00
/**
* This component will be used for the displaying username and profile picture.
* If image is not available, robohash image will be displayed
*/
2024-05-16 11:22:05 +00:00
export const UserComponent = ({ pubkey, name, image }: UserProps) => {
const theme = useTheme()
const npub = hexToNpub(pubkey)
const roboImage = `https://robohash.org/${npub}.png?set=set3`
return (
2024-05-22 06:19:40 +00:00
<Box
sx={{ display: 'flex', alignItems: 'center', gap: '10px', flexGrow: 1 }}
>
2024-05-16 11:22:05 +00:00
<img
src={image || roboImage}
alt="User Image"
className="profile-image"
style={{
borderWidth: '3px',
borderStyle: 'solid',
borderColor: `#${pubkey.substring(0, 6)}`
}}
/>
<Link to={getProfileRoute(pubkey)}>
<Typography
component="label"
sx={{
textAlign: 'center',
cursor: 'pointer',
color: theme.palette.text.primary
}}
>
{name}
</Typography>
</Link>
</Box>
)
}