Refactor sidebar avatar component

Signed-off-by: Ajay Bura <ajbura@gmail.com>
This commit is contained in:
Ajay Bura 2022-03-07 21:05:47 +05:30
parent e1a67acde1
commit 8f41139076
2 changed files with 54 additions and 63 deletions

View file

@ -4,16 +4,13 @@ import './SidebarAvatar.scss';
import { twemojify } from '../../../util/twemojify'; import { twemojify } from '../../../util/twemojify';
import Avatar from '../../atoms/avatar/Avatar';
import Text from '../../atoms/text/Text'; import Text from '../../atoms/text/Text';
import Tooltip from '../../atoms/tooltip/Tooltip'; import Tooltip from '../../atoms/tooltip/Tooltip';
import NotificationBadge from '../../atoms/badge/NotificationBadge';
import { blurOnBubbling } from '../../atoms/button/script'; import { blurOnBubbling } from '../../atoms/button/script';
const SidebarAvatar = React.forwardRef(({ const SidebarAvatar = React.forwardRef(({
tooltip, text, bgColor, imageSrc, tooltip, active, onClick, onContextMenu,
iconSrc, active, onClick, onContextMenu, avatar, notificationBadge,
isUnread, notificationCount, isAlert,
}, ref) => { }, ref) => {
let activeClass = ''; let activeClass = '';
if (active) activeClass = ' sidebar-avatar--active'; if (active) activeClass = ' sidebar-avatar--active';
@ -30,51 +27,26 @@ const SidebarAvatar = React.forwardRef(({
onClick={onClick} onClick={onClick}
onContextMenu={onContextMenu} onContextMenu={onContextMenu}
> >
<Avatar {avatar}
text={text} {notificationBadge}
bgColor={bgColor}
imageSrc={imageSrc}
iconSrc={iconSrc}
size="normal"
/>
{ isUnread && (
<NotificationBadge
alert={isAlert}
content={notificationCount !== 0 ? notificationCount : null}
/>
)}
</button> </button>
</Tooltip> </Tooltip>
); );
}); });
SidebarAvatar.defaultProps = { SidebarAvatar.defaultProps = {
text: null,
bgColor: 'transparent',
iconSrc: null,
imageSrc: null,
active: false, active: false,
onClick: null, onClick: null,
onContextMenu: null, onContextMenu: null,
isUnread: false, notificationBadge: null,
notificationCount: 0,
isAlert: false,
}; };
SidebarAvatar.propTypes = { SidebarAvatar.propTypes = {
tooltip: PropTypes.string.isRequired, tooltip: PropTypes.string.isRequired,
text: PropTypes.string,
bgColor: PropTypes.string,
imageSrc: PropTypes.string,
iconSrc: PropTypes.string,
active: PropTypes.bool, active: PropTypes.bool,
onClick: PropTypes.func, onClick: PropTypes.func,
onContextMenu: PropTypes.func, onContextMenu: PropTypes.func,
isUnread: PropTypes.bool, avatar: PropTypes.node.isRequired,
notificationCount: PropTypes.oneOfType([ notificationBadge: PropTypes.node,
PropTypes.string,
PropTypes.number,
]),
isAlert: PropTypes.bool,
}; };
export default SidebarAvatar; export default SidebarAvatar;

View file

@ -10,6 +10,8 @@ import {
} from '../../../client/action/navigation'; } from '../../../client/action/navigation';
import { abbreviateNumber, getEventCords } from '../../../util/common'; import { abbreviateNumber, getEventCords } from '../../../util/common';
import Avatar from '../../atoms/avatar/Avatar';
import NotificationBadge from '../../atoms/badge/NotificationBadge';
import ScrollView from '../../atoms/scroll/ScrollView'; import ScrollView from '../../atoms/scroll/ScrollView';
import SidebarAvatar from '../../molecules/sidebar-avatar/SidebarAvatar'; import SidebarAvatar from '../../molecules/sidebar-avatar/SidebarAvatar';
import SpaceOptions from '../../molecules/space-options/SpaceOptions'; import SpaceOptions from '../../molecules/space-options/SpaceOptions';
@ -52,9 +54,14 @@ function ProfileAvatarMenu() {
<SidebarAvatar <SidebarAvatar
onClick={openSettings} onClick={openSettings}
tooltip={profile.displayName} tooltip={profile.displayName}
imageSrc={profile.avatarUrl !== null ? mx.mxcUrlToHttp(profile.avatarUrl, 42, 42, 'crop') : null} avatar={(
bgColor={colorMXID(mx.getUserId())} <Avatar
text={profile.displayName} text={profile.displayName}
bgColor={colorMXID(mx.getUserId())}
size="normal"
imageSrc={profile.avatarUrl !== null ? mx.mxcUrlToHttp(profile.avatarUrl, 42, 42, 'crop') : null}
/>
)}
/> />
); );
} }
@ -150,22 +157,28 @@ function SideBar() {
<div className="scrollable-content"> <div className="scrollable-content">
<div className="featured-container"> <div className="featured-container">
<SidebarAvatar <SidebarAvatar
tooltip="Home"
active={selectedTab === cons.tabs.HOME} active={selectedTab === cons.tabs.HOME}
onClick={() => selectTab(cons.tabs.HOME)} onClick={() => selectTab(cons.tabs.HOME)}
tooltip="Home" avatar={<Avatar iconSrc={HomeIC} size="normal" />}
iconSrc={HomeIC} notificationBadge={homeNoti ? (
isUnread={homeNoti !== null} <NotificationBadge
notificationCount={homeNoti !== null ? abbreviateNumber(homeNoti.total) : 0} alert={homeNoti?.highlight > 0}
isAlert={homeNoti?.highlight > 0} content={abbreviateNumber(homeNoti.total) || null}
/>
) : null}
/> />
<SidebarAvatar <SidebarAvatar
tooltip="People"
active={selectedTab === cons.tabs.DIRECTS} active={selectedTab === cons.tabs.DIRECTS}
onClick={() => selectTab(cons.tabs.DIRECTS)} onClick={() => selectTab(cons.tabs.DIRECTS)}
tooltip="People" avatar={<Avatar iconSrc={UserIC} size="normal" />}
iconSrc={UserIC} notificationBadge={dmsNoti ? (
isUnread={dmsNoti !== null} <NotificationBadge
notificationCount={dmsNoti !== null ? abbreviateNumber(dmsNoti.total) : 0} alert={dmsNoti?.highlight > 0}
isAlert={dmsNoti?.highlight > 0} content={abbreviateNumber(dmsNoti.total) || null}
/>
) : null}
/> />
</div> </div>
<div className="sidebar-divider" /> <div className="sidebar-divider" />
@ -179,22 +192,30 @@ function SideBar() {
active={selectedTab === sRoomId} active={selectedTab === sRoomId}
key={sRoomId} key={sRoomId}
tooltip={room.name} tooltip={room.name}
bgColor={colorMXID(room.roomId)}
imageSrc={room.getAvatarUrl(initMatrix.matrixClient.baseUrl, 42, 42, 'crop') || null}
text={room.name}
isUnread={notifications.hasNoti(sRoomId)}
notificationCount={abbreviateNumber(notifications.getTotalNoti(sRoomId))}
isAlert={notifications.getHighlightNoti(sRoomId) !== 0}
onClick={() => selectTab(shortcut)} onClick={() => selectTab(shortcut)}
onContextMenu={(e) => openSpaceOptions(e, sRoomId)} onContextMenu={(e) => openSpaceOptions(e, sRoomId)}
avatar={(
<Avatar
text={room.name}
bgColor={colorMXID(room.roomId)}
size="normal"
imageSrc={room.getAvatarUrl(initMatrix.matrixClient.baseUrl, 42, 42, 'crop') || null}
/>
)}
notificationBadge={notifications.hasNoti(sRoomId) ? (
<NotificationBadge
alert={notifications.getHighlightNoti(sRoomId) > 0}
content={abbreviateNumber(notifications.getTotalNoti(sRoomId)) || null}
/>
) : null}
/> />
); );
}) })
} }
<SidebarAvatar <SidebarAvatar
onClick={() => openShortcutSpaces()}
tooltip="Pin spaces" tooltip="Pin spaces"
iconSrc={AddPinIC} onClick={() => openShortcutSpaces()}
avatar={<Avatar iconSrc={AddPinIC} size="normal" />}
/> />
</div> </div>
</div> </div>
@ -204,18 +225,16 @@ function SideBar() {
<div className="sidebar-divider" /> <div className="sidebar-divider" />
<div className="sticky-container"> <div className="sticky-container">
<SidebarAvatar <SidebarAvatar
onClick={() => openSearch()}
tooltip="Search" tooltip="Search"
iconSrc={SearchIC} onClick={() => openSearch()}
avatar={<Avatar iconSrc={SearchIC} size="normal" />}
/> />
{ totalInvites !== 0 && ( { totalInvites !== 0 && (
<SidebarAvatar <SidebarAvatar
isUnread
notificationCount={totalInvites}
isAlert
onClick={() => openInviteList()}
tooltip="Invites" tooltip="Invites"
iconSrc={InviteIC} onClick={() => openInviteList()}
avatar={<Avatar iconSrc={InviteIC} size="normal" />}
notificationBadge={<NotificationBadge alert content={totalInvites} />}
/> />
)} )}
<ProfileAvatarMenu /> <ProfileAvatarMenu />