Files
cinny/src/app/organisms/navigation/Selector.jsx
T

106 lines
3.6 KiB
React
Raw Normal View History

/* 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';
import { getEventCords, abbreviateNumber } from '../../../util/common';
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';
import HashIC from '../../../../public/res/ic/outlined/hash.svg';
import HashGlobeIC from '../../../../public/res/ic/outlined/hash-globe.svg';
import HashLockIC from '../../../../public/res/ic/outlined/hash-lock.svg';
import SpaceIC from '../../../../public/res/ic/outlined/space.svg';
import SpaceGlobeIC from '../../../../public/res/ic/outlined/space-globe.svg';
import SpaceLockIC from '../../../../public/res/ic/outlined/space-lock.svg';
import VerticalMenuIC from '../../../../public/res/ic/outlined/vertical-menu.svg';
2021-09-03 17:58:01 +05:30
function Selector({
roomId, isDM, drawerPostie, onClick,
}) {
const mx = initMatrix.matrixClient;
const noti = initMatrix.notifications;
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-09-03 17:58:01 +05:30
const [isSelected, setIsSelected] = useState(navigation.selectedRoomId === roomId);
const [, forceUpdate] = useState({});
2021-09-03 17:58:01 +05:30
function selectorChanged(selectedRoomId) {
setIsSelected(selectedRoomId === roomId);
}
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) => ({
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);
return (
2021-08-31 18:43:31 +05:30
<RoomSelector
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())}
isSelected={isSelected}
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={(
<IconButton
size="extra-small"
tooltip="Options"
tooltipPlacement="right"
src={VerticalMenuIC}
2022-01-29 14:31:14 +05:30
onClick={openOptions}
/>
2021-09-05 18:56:34 +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,
};
export default Selector;