Files
cinny/src/app/organisms/room/RoomView.jsx
T

118 lines
3.8 KiB
React
Raw Normal View History

2021-12-22 20:18:32 +05:30
import React, { useEffect, useRef } from 'react';
2021-07-28 18:45:52 +05:30
import PropTypes from 'prop-types';
2021-08-31 18:43:31 +05:30
import './RoomView.scss';
2023-06-12 21:15:23 +10:00
import { Text, config } from 'folds';
2023-06-28 17:27:28 +05:30
import { EventType } from 'matrix-js-sdk';
2021-07-28 18:45:52 +05:30
2021-12-22 20:18:32 +05:30
import cons from '../../../client/state/cons';
import navigation from '../../../client/state/navigation';
2021-08-31 18:43:31 +05:30
import RoomViewHeader from './RoomViewHeader';
2023-06-12 21:15:23 +10:00
import { RoomInput } from './RoomInput';
import { useStateEvent } from '../../hooks/useStateEvent';
import { StateEvent } from '../../../types/matrix/room';
import { RoomTombstone } from './RoomTombstone';
2023-10-06 13:44:06 +11:00
import { usePowerLevelsAPI } from '../../hooks/usePowerLevels';
2023-06-12 21:15:23 +10:00
import { useMatrixClient } from '../../hooks/useMatrixClient';
import { RoomInputPlaceholder } from './RoomInputPlaceholder';
2023-10-06 13:44:06 +11:00
import { RoomTimeline } from './RoomTimeline';
import { RoomViewTyping } from './RoomViewTyping';
import { RoomViewFollowing } from './RoomViewFollowing';
import { useEditor } from '../../components/editor';
2021-07-28 18:45:52 +05:30
2023-10-06 13:44:06 +11:00
function RoomView({ room, eventId }) {
2023-06-12 21:15:23 +10:00
const roomInputRef = useRef(null);
2021-12-22 20:18:32 +05:30
const roomViewRef = useRef(null);
2023-10-06 13:44:06 +11:00
// eslint-disable-next-line react/prop-types
2023-10-06 13:44:06 +11:00
const { roomId } = room;
const editor = useEditor();
2021-07-28 18:45:52 +05:30
2023-06-12 21:15:23 +10:00
const mx = useMatrixClient();
const tombstoneEvent = useStateEvent(room, StateEvent.RoomTombstone);
2023-10-06 13:44:06 +11:00
const { getPowerLevel, canSendEvent } = usePowerLevelsAPI();
2023-06-12 21:15:23 +10:00
const myUserId = mx.getUserId();
2023-06-28 17:27:28 +05:30
const canMessage = myUserId
? canSendEvent(EventType.RoomMessage, getPowerLevel(myUserId))
: false;
2023-06-12 21:15:23 +10:00
2021-12-22 20:18:32 +05:30
useEffect(() => {
const settingsToggle = (isVisible) => {
const roomView = roomViewRef.current;
roomView.classList.toggle('room-view--dropped');
const roomViewContent = roomView.children[1];
2021-12-23 10:03:20 +05:30
if (isVisible) {
setTimeout(() => {
if (!navigation.isRoomSettings) return;
roomViewContent.style.visibility = 'hidden';
}, 200);
} else roomViewContent.style.visibility = 'visible';
2021-12-22 20:18:32 +05:30
};
navigation.on(cons.events.navigation.ROOM_SETTINGS_TOGGLED, settingsToggle);
return () => {
navigation.removeListener(cons.events.navigation.ROOM_SETTINGS_TOGGLED, settingsToggle);
};
}, []);
2021-07-28 18:45:52 +05:30
return (
2021-12-22 20:18:32 +05:30
<div className="room-view" ref={roomViewRef}>
2021-08-31 18:43:31 +05:30
<RoomViewHeader roomId={roomId} />
<div className="room-view__content-wrapper">
<div className="room-view__scrollable">
2023-10-06 13:44:06 +11:00
<RoomTimeline
key={roomId}
room={room}
eventId={eventId}
2023-06-12 21:15:23 +10:00
roomInputRef={roomInputRef}
2023-10-06 13:44:06 +11:00
editor={editor}
/>
2023-10-06 13:44:06 +11:00
<RoomViewTyping room={room} />
</div>
<div className="room-view__sticky">
2023-06-12 21:15:23 +10:00
<div className="room-view__editor">
{tombstoneEvent ? (
<RoomTombstone
roomId={roomId}
body={tombstoneEvent.getContent().body}
replacementRoomId={tombstoneEvent.getContent().replacement_room}
/>
) : (
<>
{canMessage && (
2023-10-06 13:44:06 +11:00
<RoomInput
editor={editor}
roomId={roomId}
roomViewRef={roomViewRef}
ref={roomInputRef}
/>
2023-06-12 21:15:23 +10:00
)}
{!canMessage && (
<RoomInputPlaceholder
style={{ padding: config.space.S200 }}
alignItems="Center"
justifyContent="Center"
>
<Text align="Center">You do not have permission to post in this room</Text>
</RoomInputPlaceholder>
)}
</>
)}
</div>
2023-10-06 13:44:06 +11:00
<RoomViewFollowing room={room} />
2021-07-28 18:45:52 +05:30
</div>
</div>
</div>
);
}
RoomView.defaultProps = {
eventId: null,
};
2021-08-31 18:43:31 +05:30
RoomView.propTypes = {
2023-06-12 21:15:23 +10:00
room: PropTypes.shape({}).isRequired,
eventId: PropTypes.string,
2021-07-28 18:45:52 +05:30
};
2021-08-31 18:43:31 +05:30
export default RoomView;