cinny/src/app/molecules/room-selector/RoomSelector.jsx

108 lines
2.8 KiB
React
Raw Normal View History

2021-07-28 15:15:52 +02:00
import React from 'react';
import PropTypes from 'prop-types';
2021-08-31 15:13:31 +02:00
import './RoomSelector.scss';
2021-07-28 15:15:52 +02:00
import { twemojify } from '../../../util/twemojify';
2021-07-28 15:15:52 +02:00
import colorMXID from '../../../util/colorMXID';
import Text from '../../atoms/text/Text';
import Avatar from '../../atoms/avatar/Avatar';
import NotificationBadge from '../../atoms/badge/NotificationBadge';
import { blurOnBubbling } from '../../atoms/button/script';
2021-08-31 15:13:31 +02:00
function RoomSelectorWrapper({
2021-09-09 14:05:39 +02:00
isSelected, isUnread, onClick, content, options,
2021-08-29 10:27:55 +02:00
}) {
2021-09-09 14:05:39 +02:00
let myClass = isUnread ? ' room-selector--unread' : '';
myClass += isSelected ? ' room-selector--selected' : '';
2021-08-29 10:27:55 +02:00
return (
2021-09-09 14:05:39 +02:00
<div className={`room-selector${myClass}`}>
2021-08-29 10:27:55 +02:00
<button
2021-08-31 15:13:31 +02:00
className="room-selector__content"
2021-08-29 10:27:55 +02:00
type="button"
onClick={onClick}
2021-08-31 15:13:31 +02:00
onMouseUp={(e) => blurOnBubbling(e, '.room-selector')}
2021-08-29 10:27:55 +02:00
>
{content}
</button>
2021-08-31 15:13:31 +02:00
<div className="room-selector__options">{options}</div>
2021-08-29 10:27:55 +02:00
</div>
);
}
2021-08-31 15:13:31 +02:00
RoomSelectorWrapper.defaultProps = {
2021-08-29 10:27:55 +02:00
options: null,
};
2021-08-31 15:13:31 +02:00
RoomSelectorWrapper.propTypes = {
2021-08-29 10:27:55 +02:00
isSelected: PropTypes.bool.isRequired,
2021-09-09 14:05:39 +02:00
isUnread: PropTypes.bool.isRequired,
2021-08-29 10:27:55 +02:00
onClick: PropTypes.func.isRequired,
content: PropTypes.node.isRequired,
options: PropTypes.node,
};
2021-08-31 15:13:31 +02:00
function RoomSelector({
name, parentName, roomId, imageSrc, iconSrc,
2021-08-29 10:27:55 +02:00
isSelected, isUnread, notificationCount, isAlert,
options, onClick,
2021-07-28 15:15:52 +02:00
}) {
return (
2021-08-31 15:13:31 +02:00
<RoomSelectorWrapper
2021-08-29 10:27:55 +02:00
isSelected={isSelected}
2021-09-09 14:05:39 +02:00
isUnread={isUnread}
2021-08-29 10:27:55 +02:00
content={(
<>
2021-07-28 15:15:52 +02:00
<Avatar
text={name}
2021-07-28 15:15:52 +02:00
bgColor={colorMXID(roomId)}
imageSrc={imageSrc}
iconSrc={iconSrc}
size="extra-small"
/>
<Text variant="b1">
{twemojify(name)}
{parentName && (
<span className="text text-b3">
{' — '}
{twemojify(parentName)}
</span>
)}
</Text>
2021-08-29 10:27:55 +02:00
{ isUnread && (
2021-08-28 14:46:20 +02:00
<NotificationBadge
2021-08-29 10:27:55 +02:00
alert={isAlert}
2021-08-28 14:46:20 +02:00
content={notificationCount !== 0 ? notificationCount : null}
/>
)}
2021-08-29 10:27:55 +02:00
</>
)}
options={options}
onClick={onClick}
/>
2021-07-28 15:15:52 +02:00
);
}
2021-08-31 15:13:31 +02:00
RoomSelector.defaultProps = {
parentName: null,
2021-09-09 14:05:39 +02:00
isSelected: false,
2021-07-28 15:15:52 +02:00
imageSrc: null,
2021-08-29 10:27:55 +02:00
iconSrc: null,
options: null,
2021-07-28 15:15:52 +02:00
};
2021-08-31 15:13:31 +02:00
RoomSelector.propTypes = {
2021-08-29 10:27:55 +02:00
name: PropTypes.string.isRequired,
parentName: PropTypes.string,
2021-07-28 15:15:52 +02:00
roomId: PropTypes.string.isRequired,
2021-08-29 10:27:55 +02:00
imageSrc: PropTypes.string,
iconSrc: PropTypes.string,
2021-09-09 14:05:39 +02:00
isSelected: PropTypes.bool,
2021-08-29 10:27:55 +02:00
isUnread: PropTypes.bool.isRequired,
notificationCount: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]).isRequired,
2021-08-29 10:27:55 +02:00
isAlert: PropTypes.bool.isRequired,
options: PropTypes.node,
2021-07-28 15:15:52 +02:00
onClick: PropTypes.func.isRequired,
};
2021-08-31 15:13:31 +02:00
export default RoomSelector;