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

94 lines
2.9 KiB
React
Raw Normal View History

/* eslint-disable react/prop-types */
2022-03-03 18:36:53 +05:30
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
import initMatrix from '../../../client/initMatrix';
2022-03-15 17:21:36 +05:30
import cons from '../../../client/state/cons';
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';
2022-02-16 19:52:51 +05:30
import { joinRuleToIconSrc } from '../../../util/matrixUtil';
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 VerticalMenuIC from '../../../../public/res/ic/outlined/vertical-menu.svg';
2022-02-27 21:10:54 +05:30
import { useForceUpdate } from '../../hooks/useForceUpdate';
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);
2022-03-15 17:21:36 +05:30
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;
2022-03-15 17:21:36 +05:30
const isMuted = noti.getNotiType(roomId) === cons.notifs.MUTE;
2022-02-27 21:10:54 +05:30
const [, forceUpdate] = useForceUpdate();
useEffect(() => {
2022-03-03 18:36:53 +05:30
const unSub1 = drawerPostie.subscribe('selector-change', roomId, forceUpdate);
const unSub2 = drawerPostie.subscribe('unread-change', roomId, forceUpdate);
return () => {
2022-03-03 18:36:53 +05:30
unSub1();
unSub2();
};
}, []);
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
);
};
return (
2021-08-31 18:43:31 +05:30
<RoomSelector
key={roomId}
name={room.name}
roomId={roomId}
imageSrc={isDM ? imageSrc : null}
2022-02-16 19:52:51 +05:30
iconSrc={isDM ? null : joinRuleToIconSrc(room.getJoinRule(), room.isSpaceRoom())}
2022-02-27 21:10:54 +05:30
isSelected={navigation.selectedRoomId === roomId}
2022-03-15 17:21:36 +05:30
isMuted={isMuted}
isUnread={!isMuted && 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;