From 20bb05ddc61e5126e46a1e4943619d67d7f4cc27 Mon Sep 17 00:00:00 2001 From: enes Date: Wed, 31 Jul 2024 13:53:36 +0200 Subject: [PATCH] feat: add UserAvatar, UserIconButton Closes #68 - Only use getRoboHashPicture function (set 1) for Avatars --- src/components/UserAvatar/index.tsx | 42 +++++++++ src/components/UserAvatar/styles.module.scss | 13 +++ src/components/UserAvatarIconButton/index.tsx | 32 +++++++ .../UserAvatarIconButton/style.module.scss | 7 ++ src/components/username.module.scss | 7 ++ src/components/username.tsx | 93 +++---------------- src/pages/create/index.tsx | 6 +- src/pages/home/index.tsx | 6 +- src/pages/sign/internal/displayMeta.tsx | 6 +- src/pages/verify/index.tsx | 4 +- 10 files changed, 125 insertions(+), 91 deletions(-) create mode 100644 src/components/UserAvatar/index.tsx create mode 100644 src/components/UserAvatar/styles.module.scss create mode 100644 src/components/UserAvatarIconButton/index.tsx create mode 100644 src/components/UserAvatarIconButton/style.module.scss create mode 100644 src/components/username.module.scss diff --git a/src/components/UserAvatar/index.tsx b/src/components/UserAvatar/index.tsx new file mode 100644 index 0000000..65e993d --- /dev/null +++ b/src/components/UserAvatar/index.tsx @@ -0,0 +1,42 @@ +import { useNavigate } from 'react-router-dom' +import { getProfileRoute } from '../../routes' + +import styles from './styles.module.scss' +import React from 'react' +import { AvatarIconButton } from '../UserAvatarIconButton' + +interface UserAvatarProps { + name: string + pubkey: string + image?: string +} + +/** + * This component will be used for the displaying username and profile picture. + * Clicking will navigate to the user's profile. + */ +export const UserAvatar = ({ pubkey, name, image }: UserAvatarProps) => { + const navigate = useNavigate() + + const handleClick = (e: React.MouseEvent) => { + e.stopPropagation() + navigate(getProfileRoute(pubkey)) + } + + return ( +
+ + {name ? ( + + ) : null} +
+ ) +} diff --git a/src/components/UserAvatar/styles.module.scss b/src/components/UserAvatar/styles.module.scss new file mode 100644 index 0000000..6147819 --- /dev/null +++ b/src/components/UserAvatar/styles.module.scss @@ -0,0 +1,13 @@ +.container { + display: flex; + align-items: center; + gap: 10px; + flex-grow: 1; +} + +.username { + cursor: pointer; + font-weight: 500; + font-size: 14px; + color: var(--text-color); +} diff --git a/src/components/UserAvatarIconButton/index.tsx b/src/components/UserAvatarIconButton/index.tsx new file mode 100644 index 0000000..a12564a --- /dev/null +++ b/src/components/UserAvatarIconButton/index.tsx @@ -0,0 +1,32 @@ +import { IconButton, IconButtonProps } from '@mui/material' +import styles from './style.module.scss' +import { getRoboHashPicture } from '../../utils' + +interface AvatarIconButtonProps extends IconButtonProps { + src: string | undefined + hexKey: string | undefined +} + +/** + * This component displays profile image inside IconButton + * @param {string | undefined} props.src - image source or robohash picture + * @param {string | undefined} props.hexKey - robohash and affects border + * @param {IconButtonProps} props - component extends mui's IconButton + */ +export const AvatarIconButton = (props: AvatarIconButtonProps) => { + const { src, hexKey, ...rest } = props + + return ( + + user image + + ) +} diff --git a/src/components/UserAvatarIconButton/style.module.scss b/src/components/UserAvatarIconButton/style.module.scss new file mode 100644 index 0000000..f8213f0 --- /dev/null +++ b/src/components/UserAvatarIconButton/style.module.scss @@ -0,0 +1,7 @@ +.icon { + width: 40px; + height: 40px; + border-radius: 50%; + border-width: 3px; + overflow: hidden; +} diff --git a/src/components/username.module.scss b/src/components/username.module.scss new file mode 100644 index 0000000..835f863 --- /dev/null +++ b/src/components/username.module.scss @@ -0,0 +1,7 @@ +.container { + display: flex; + flex-direction: row; + justify-content: end; + align-items: center; + gap: 12px; +} diff --git a/src/components/username.tsx b/src/components/username.tsx index 859f8e7..7ee6dc3 100644 --- a/src/components/username.tsx +++ b/src/components/username.tsx @@ -1,9 +1,9 @@ -import { Box, IconButton, Typography, useTheme } from '@mui/material' +import { Typography } from '@mui/material' import { useSelector } from 'react-redux' -import { useNavigate } from 'react-router-dom' -import { getProfileRoute } from '../routes' import { State } from '../store/rootReducer' -import { hexToNpub } from '../utils' + +import styles from './username.module.scss' +import { AvatarIconButton } from './UserAvatarIconButton' type Props = { username: string @@ -11,19 +11,15 @@ type Props = { handleClick: (event: React.MouseEvent) => void } +/** + * This component will be used for the displaying logged in user in AppBar. + * Clicking will open the menu. + */ const Username = ({ username, avatarContent, handleClick }: Props) => { const hexKey = useSelector((state: State) => state.auth.usersPubkey) return ( -
+
{ > {username} - - user-avatar - + />
) } export default Username - -type UserProps = { - pubkey: string - name: string - image?: string -} - -/** - * This component will be used for the displaying username and profile picture. - * If image is not available, robohash image will be displayed - */ -export const UserComponent = ({ pubkey, name, image }: UserProps) => { - const theme = useTheme() - const navigate = useNavigate() - - const npub = hexToNpub(pubkey) - const roboImage = `https://robohash.org/${npub}.png?set=set3` - - const handleClick = (e: React.MouseEvent) => { - e.stopPropagation() - // navigate to user's profile - navigate(getProfileRoute(pubkey)) - } - - return ( - - User Image - - {name} - - - ) -} diff --git a/src/pages/create/index.tsx b/src/pages/create/index.tsx index f90ee77..b16c6d3 100644 --- a/src/pages/create/index.tsx +++ b/src/pages/create/index.tsx @@ -30,7 +30,7 @@ import { useSelector } from 'react-redux' import { useLocation, useNavigate } from 'react-router-dom' import { toast } from 'react-toastify' import { LoadingSpinner } from '../../components/LoadingSpinner' -import { UserComponent } from '../../components/username' +import { UserAvatar } from '../../components/UserAvatar' import { MetadataController, NostrController } from '../../controllers' import { appPrivateRoutes } from '../../routes' import { State } from '../../store/rootReducer' @@ -799,7 +799,7 @@ const DisplayUser = ({ return ( - - { (function () { const profile = profiles[submittedBy] return ( - { {signStatus} - - { return ( <> -