2021-08-30 17:42:24 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2021-09-03 14:28:01 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2021-08-30 17:42:24 +02:00
|
|
|
|
|
|
|
import initMatrix from '../../../client/initMatrix';
|
|
|
|
import cons from '../../../client/state/cons';
|
|
|
|
import navigation from '../../../client/state/navigation';
|
2021-09-03 14:28:01 +02:00
|
|
|
import { selectSpace, selectRoom } from '../../../client/action/navigation';
|
2021-08-30 17:42:24 +02:00
|
|
|
import Postie from '../../../util/Postie';
|
|
|
|
|
|
|
|
import Text from '../../atoms/text/Text';
|
|
|
|
import Selector from './Selector';
|
|
|
|
|
|
|
|
import { AtoZ } from './common';
|
|
|
|
|
|
|
|
const drawerPostie = new Postie();
|
2021-09-03 14:28:01 +02:00
|
|
|
function Home({ spaceId }) {
|
|
|
|
const [, forceUpdate] = useState({});
|
2021-09-12 17:14:13 +02:00
|
|
|
const { roomList, notifications } = initMatrix;
|
2021-09-03 14:28:01 +02:00
|
|
|
let spaceIds = [];
|
|
|
|
let roomIds = [];
|
|
|
|
let directIds = [];
|
2021-08-30 17:42:24 +02:00
|
|
|
|
2021-09-03 14:28:01 +02:00
|
|
|
const spaceChildIds = roomList.getSpaceChildren(spaceId);
|
|
|
|
if (spaceChildIds) {
|
|
|
|
spaceIds = spaceChildIds.filter((roomId) => roomList.spaces.has(roomId)).sort(AtoZ);
|
|
|
|
roomIds = spaceChildIds.filter((roomId) => roomList.rooms.has(roomId)).sort(AtoZ);
|
|
|
|
directIds = spaceChildIds.filter((roomId) => roomList.directs.has(roomId)).sort(AtoZ);
|
|
|
|
} else {
|
|
|
|
spaceIds = [...roomList.spaces]
|
|
|
|
.filter((roomId) => !roomList.roomIdToParents.has(roomId)).sort(AtoZ);
|
|
|
|
roomIds = [...roomList.rooms]
|
|
|
|
.filter((roomId) => !roomList.roomIdToParents.has(roomId)).sort(AtoZ);
|
|
|
|
}
|
2021-08-30 17:42:24 +02:00
|
|
|
|
2021-09-03 14:28:01 +02:00
|
|
|
function selectorChanged(selectedRoomId, prevSelectedRoomId) {
|
2021-08-30 17:42:24 +02:00
|
|
|
if (!drawerPostie.hasTopic('selector-change')) return;
|
|
|
|
const addresses = [];
|
2021-09-03 14:28:01 +02:00
|
|
|
if (drawerPostie.hasSubscriber('selector-change', selectedRoomId)) addresses.push(selectedRoomId);
|
|
|
|
if (drawerPostie.hasSubscriber('selector-change', prevSelectedRoomId)) addresses.push(prevSelectedRoomId);
|
2021-08-30 17:42:24 +02:00
|
|
|
if (addresses.length === 0) return;
|
2021-09-03 14:28:01 +02:00
|
|
|
drawerPostie.post('selector-change', addresses, selectedRoomId);
|
2021-08-30 17:42:24 +02:00
|
|
|
}
|
2021-09-12 17:14:13 +02:00
|
|
|
function notiChanged(roomId, total, prevTotal) {
|
|
|
|
if (total === prevTotal) return;
|
|
|
|
if (drawerPostie.hasTopicAndSubscriber('unread-change', roomId)) {
|
|
|
|
drawerPostie.post('unread-change', roomId);
|
|
|
|
}
|
2021-08-30 17:42:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function roomListUpdated() {
|
|
|
|
const { spaces, rooms, directs } = initMatrix.roomList;
|
|
|
|
if (!(
|
2021-09-03 14:28:01 +02:00
|
|
|
spaces.has(navigation.selectedRoomId)
|
|
|
|
|| rooms.has(navigation.selectedRoomId)
|
|
|
|
|| directs.has(navigation.selectedRoomId))
|
2021-08-30 17:42:24 +02:00
|
|
|
) {
|
|
|
|
selectRoom(null);
|
|
|
|
}
|
|
|
|
forceUpdate({});
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
roomList.on(cons.events.roomList.ROOMLIST_UPDATED, roomListUpdated);
|
|
|
|
navigation.on(cons.events.navigation.ROOM_SELECTED, selectorChanged);
|
2021-09-12 17:14:13 +02:00
|
|
|
notifications.on(cons.events.notifications.NOTI_CHANGED, notiChanged);
|
2021-08-30 17:42:24 +02:00
|
|
|
return () => {
|
|
|
|
roomList.removeListener(cons.events.roomList.ROOMLIST_UPDATED, roomListUpdated);
|
|
|
|
navigation.removeListener(cons.events.navigation.ROOM_SELECTED, selectorChanged);
|
2021-09-12 17:14:13 +02:00
|
|
|
notifications.removeListener(cons.events.notifications.NOTI_CHANGED, notiChanged);
|
2021-08-30 17:42:24 +02:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ spaceIds.length !== 0 && <Text className="cat-header" variant="b3">Spaces</Text> }
|
|
|
|
{ spaceIds.map((id) => (
|
|
|
|
<Selector
|
|
|
|
key={id}
|
|
|
|
roomId={id}
|
|
|
|
isDM={false}
|
|
|
|
drawerPostie={drawerPostie}
|
2021-09-03 14:28:01 +02:00
|
|
|
onClick={() => selectSpace(id)}
|
2021-08-30 17:42:24 +02:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
|
2021-08-31 15:13:31 +02:00
|
|
|
{ roomIds.length !== 0 && <Text className="cat-header" variant="b3">Rooms</Text> }
|
2021-08-30 17:42:24 +02:00
|
|
|
{ roomIds.map((id) => (
|
|
|
|
<Selector
|
|
|
|
key={id}
|
|
|
|
roomId={id}
|
|
|
|
isDM={false}
|
|
|
|
drawerPostie={drawerPostie}
|
2021-09-03 14:28:01 +02:00
|
|
|
onClick={() => selectRoom(id)}
|
2021-08-30 17:42:24 +02:00
|
|
|
/>
|
|
|
|
)) }
|
2021-09-03 14:28:01 +02:00
|
|
|
|
|
|
|
{ directIds.length !== 0 && <Text className="cat-header" variant="b3">People</Text> }
|
|
|
|
{ directIds.map((id) => (
|
|
|
|
<Selector
|
|
|
|
key={id}
|
|
|
|
roomId={id}
|
|
|
|
drawerPostie={drawerPostie}
|
|
|
|
onClick={() => selectRoom(id)}
|
|
|
|
/>
|
|
|
|
))}
|
2021-08-30 17:42:24 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2021-09-03 14:28:01 +02:00
|
|
|
Home.defaultProps = {
|
|
|
|
spaceId: null,
|
|
|
|
};
|
|
|
|
Home.propTypes = {
|
|
|
|
spaceId: PropTypes.string,
|
|
|
|
};
|
2021-08-30 17:42:24 +02:00
|
|
|
|
|
|
|
export default Home;
|