2021-08-30 21:12:24 +05:30
|
|
|
/* eslint-disable react/prop-types */
|
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
|
|
import initMatrix from '../../../client/initMatrix';
|
|
|
|
|
import navigation from '../../../client/state/navigation';
|
2022-01-13 18:30:43 +05:30
|
|
|
import { openReusableContextMenu } from '../../../client/action/navigation';
|
2021-09-12 20:44:13 +05:30
|
|
|
import { getEventCords, abbreviateNumber } from '../../../util/common';
|
2021-08-30 21:12:24 +05:30
|
|
|
|
2021-09-05 18:56:34 +05:30
|
|
|
import IconButton from '../../atoms/button/IconButton';
|
2021-08-31 18:43:31 +05:30
|
|
|
import RoomSelector from '../../molecules/room-selector/RoomSelector';
|
2022-01-14 09:43:35 +05:30
|
|
|
import RoomOptions from '../../molecules/room-options/RoomOptions';
|
2022-01-29 14:31:14 +05:30
|
|
|
import SpaceOptions from '../../molecules/space-options/SpaceOptions';
|
2021-08-30 21:12:24 +05:30
|
|
|
|
|
|
|
|
import HashIC from '../../../../public/res/ic/outlined/hash.svg';
|
2021-12-26 11:26:41 +05:30
|
|
|
import HashGlobeIC from '../../../../public/res/ic/outlined/hash-globe.svg';
|
2021-08-30 21:12:24 +05:30
|
|
|
import HashLockIC from '../../../../public/res/ic/outlined/hash-lock.svg';
|
|
|
|
|
import SpaceIC from '../../../../public/res/ic/outlined/space.svg';
|
2021-12-26 11:26:41 +05:30
|
|
|
import SpaceGlobeIC from '../../../../public/res/ic/outlined/space-globe.svg';
|
2021-08-30 21:12:24 +05:30
|
|
|
import SpaceLockIC from '../../../../public/res/ic/outlined/space-lock.svg';
|
2021-09-09 18:36:39 +05:30
|
|
|
import VerticalMenuIC from '../../../../public/res/ic/outlined/vertical-menu.svg';
|
2021-08-30 21:12:24 +05:30
|
|
|
|
2021-09-03 17:58:01 +05:30
|
|
|
function Selector({
|
|
|
|
|
roomId, isDM, drawerPostie, onClick,
|
|
|
|
|
}) {
|
2021-08-30 21:12:24 +05:30
|
|
|
const mx = initMatrix.matrixClient;
|
2021-09-12 20:44:13 +05:30
|
|
|
const noti = initMatrix.notifications;
|
2021-08-30 21:12:24 +05:30
|
|
|
const room = mx.getRoom(roomId);
|
2021-09-05 18:56:34 +05:30
|
|
|
let imageSrc = room.getAvatarFallbackMember()?.getAvatarUrl(mx.baseUrl, 24, 24, 'crop') || null;
|
|
|
|
|
if (imageSrc === null) imageSrc = room.getAvatarUrl(mx.baseUrl, 24, 24, 'crop') || null;
|
2021-08-30 21:12:24 +05:30
|
|
|
|
2021-09-03 17:58:01 +05:30
|
|
|
const [isSelected, setIsSelected] = useState(navigation.selectedRoomId === roomId);
|
2021-08-30 21:12:24 +05:30
|
|
|
const [, forceUpdate] = useState({});
|
|
|
|
|
|
2021-09-03 17:58:01 +05:30
|
|
|
function selectorChanged(selectedRoomId) {
|
|
|
|
|
setIsSelected(selectedRoomId === roomId);
|
2021-08-30 21:12:24 +05:30
|
|
|
}
|
|
|
|
|
function changeNotificationBadge() {
|
|
|
|
|
forceUpdate({});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
drawerPostie.subscribe('selector-change', roomId, selectorChanged);
|
|
|
|
|
drawerPostie.subscribe('unread-change', roomId, changeNotificationBadge);
|
|
|
|
|
return () => {
|
|
|
|
|
drawerPostie.unsubscribe('selector-change', roomId);
|
|
|
|
|
drawerPostie.unsubscribe('unread-change', roomId);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2022-01-29 14:31:14 +05:30
|
|
|
const openOptions = (e) => {
|
2022-01-13 18:30:43 +05:30
|
|
|
e.preventDefault();
|
|
|
|
|
openReusableContextMenu(
|
|
|
|
|
'right',
|
|
|
|
|
getEventCords(e, '.room-selector'),
|
2022-01-29 14:31:14 +05:30
|
|
|
room.isSpaceRoom()
|
|
|
|
|
? (closeMenu) => <SpaceOptions roomId={roomId} afterOptionSelect={closeMenu} />
|
|
|
|
|
: (closeMenu) => <RoomOptions roomId={roomId} afterOptionSelect={closeMenu} />,
|
2022-01-13 18:30:43 +05:30
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-05 17:47:41 +05:30
|
|
|
const joinRuleToIconSrc = (joinRule) => ({
|
2021-12-26 11:26:41 +05:30
|
|
|
restricted: () => (room.isSpaceRoom() ? SpaceIC : HashIC),
|
|
|
|
|
invite: () => (room.isSpaceRoom() ? SpaceLockIC : HashLockIC),
|
|
|
|
|
public: () => (room.isSpaceRoom() ? SpaceGlobeIC : HashGlobeIC),
|
2022-01-05 17:47:41 +05:30
|
|
|
}[joinRule]?.() || null);
|
2021-12-26 11:26:41 +05:30
|
|
|
|
2021-08-30 21:12:24 +05:30
|
|
|
return (
|
2021-08-31 18:43:31 +05:30
|
|
|
<RoomSelector
|
2021-08-30 21:12:24 +05:30
|
|
|
key={roomId}
|
|
|
|
|
name={room.name}
|
|
|
|
|
roomId={roomId}
|
|
|
|
|
imageSrc={isDM ? imageSrc : null}
|
2022-01-05 17:47:41 +05:30
|
|
|
iconSrc={isDM ? null : joinRuleToIconSrc(room.getJoinRule())}
|
2021-08-30 21:12:24 +05:30
|
|
|
isSelected={isSelected}
|
2021-09-12 20:44:13 +05:30
|
|
|
isUnread={noti.hasNoti(roomId)}
|
|
|
|
|
notificationCount={abbreviateNumber(noti.getTotalNoti(roomId))}
|
|
|
|
|
isAlert={noti.getHighlightNoti(roomId) !== 0}
|
2021-09-03 17:58:01 +05:30
|
|
|
onClick={onClick}
|
2022-01-29 14:31:14 +05:30
|
|
|
onContextMenu={openOptions}
|
2021-09-05 18:56:34 +05:30
|
|
|
options={(
|
2021-09-09 18:36:39 +05:30
|
|
|
<IconButton
|
|
|
|
|
size="extra-small"
|
|
|
|
|
tooltip="Options"
|
|
|
|
|
tooltipPlacement="right"
|
|
|
|
|
src={VerticalMenuIC}
|
2022-01-29 14:31:14 +05:30
|
|
|
onClick={openOptions}
|
2021-09-09 18:36:39 +05:30
|
|
|
/>
|
2021-09-05 18:56:34 +05:30
|
|
|
)}
|
2021-08-30 21:12:24 +05:30
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Selector.defaultProps = {
|
|
|
|
|
isDM: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Selector.propTypes = {
|
|
|
|
|
roomId: PropTypes.string.isRequired,
|
|
|
|
|
isDM: PropTypes.bool,
|
|
|
|
|
drawerPostie: PropTypes.shape({}).isRequired,
|
2021-09-03 17:58:01 +05:30
|
|
|
onClick: PropTypes.func.isRequired,
|
2021-08-30 21:12:24 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Selector;
|