2022-02-27 16:40:54 +01:00
|
|
|
import React, { 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';
|
|
|
|
import Postie from '../../../util/Postie';
|
2022-03-17 12:25:16 +01:00
|
|
|
import { roomIdByActivity, roomIdByAtoZ } from '../../../util/sort';
|
2021-08-30 17:42:24 +02:00
|
|
|
|
2022-03-03 14:06:53 +01:00
|
|
|
import RoomsCategory from './RoomsCategory';
|
2021-08-30 17:42:24 +02:00
|
|
|
|
2022-03-03 14:06:53 +01:00
|
|
|
import { useCategorizedSpaces } from '../../hooks/useCategorizedSpaces';
|
2021-08-30 17:42:24 +02:00
|
|
|
|
|
|
|
const drawerPostie = new Postie();
|
2021-09-03 14:28:01 +02:00
|
|
|
function Home({ spaceId }) {
|
2022-03-03 14:06:53 +01:00
|
|
|
const mx = initMatrix.matrixClient;
|
|
|
|
const { roomList, notifications, accountData } = initMatrix;
|
2022-03-14 13:04:34 +01:00
|
|
|
const { spaces, rooms, directs } = roomList;
|
2022-03-13 14:22:51 +01:00
|
|
|
useCategorizedSpaces();
|
2022-03-03 14:06:53 +01:00
|
|
|
const isCategorized = accountData.categorizedSpaces.has(spaceId);
|
|
|
|
|
|
|
|
let categories = null;
|
2021-09-03 14:28:01 +02:00
|
|
|
let spaceIds = [];
|
|
|
|
let roomIds = [];
|
|
|
|
let directIds = [];
|
2021-08-30 17:42:24 +02:00
|
|
|
|
2022-03-14 13:04:34 +01:00
|
|
|
if (spaceId) {
|
|
|
|
const spaceChildIds = roomList.getSpaceChildren(spaceId);
|
2022-03-03 14:06:53 +01:00
|
|
|
spaceIds = spaceChildIds.filter((roomId) => spaces.has(roomId));
|
|
|
|
roomIds = spaceChildIds.filter((roomId) => rooms.has(roomId));
|
|
|
|
directIds = spaceChildIds.filter((roomId) => directs.has(roomId));
|
2021-09-03 14:28:01 +02:00
|
|
|
} else {
|
2022-03-14 13:04:34 +01:00
|
|
|
spaceIds = roomList.getOrphanSpaces();
|
|
|
|
roomIds = roomList.getOrphanRooms();
|
2022-03-03 14:06:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isCategorized) {
|
|
|
|
categories = roomList.getCategorizedSpaces(spaceIds);
|
2022-03-13 14:22:51 +01:00
|
|
|
categories.delete(spaceId);
|
2021-09-03 14:28:01 +02:00
|
|
|
}
|
2021-08-30 17:42:24 +02:00
|
|
|
|
2022-02-27 16:40:54 +01:00
|
|
|
useEffect(() => {
|
|
|
|
const selectorChanged = (selectedRoomId, prevSelectedRoomId) => {
|
|
|
|
if (!drawerPostie.hasTopic('selector-change')) return;
|
|
|
|
const addresses = [];
|
|
|
|
if (drawerPostie.hasSubscriber('selector-change', selectedRoomId)) addresses.push(selectedRoomId);
|
|
|
|
if (drawerPostie.hasSubscriber('selector-change', prevSelectedRoomId)) addresses.push(prevSelectedRoomId);
|
|
|
|
if (addresses.length === 0) return;
|
|
|
|
drawerPostie.post('selector-change', addresses, selectedRoomId);
|
|
|
|
};
|
2021-08-30 17:42:24 +02:00
|
|
|
|
2022-02-27 16:40:54 +01:00
|
|
|
const 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
|
|
|
|
|
|
|
navigation.on(cons.events.navigation.ROOM_SELECTED, selectorChanged);
|
2021-09-12 17:14:13 +02:00
|
|
|
notifications.on(cons.events.notifications.NOTI_CHANGED, notiChanged);
|
2022-03-15 12:51:36 +01:00
|
|
|
notifications.on(cons.events.notifications.MUTE_TOGGLED, notiChanged);
|
2021-08-30 17:42:24 +02:00
|
|
|
return () => {
|
|
|
|
navigation.removeListener(cons.events.navigation.ROOM_SELECTED, selectorChanged);
|
2021-09-12 17:14:13 +02:00
|
|
|
notifications.removeListener(cons.events.notifications.NOTI_CHANGED, notiChanged);
|
2022-03-15 12:51:36 +01:00
|
|
|
notifications.removeListener(cons.events.notifications.MUTE_TOGGLED, notiChanged);
|
2021-08-30 17:42:24 +02:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-03-03 14:06:53 +01:00
|
|
|
{ !isCategorized && spaceIds.length !== 0 && (
|
2022-03-17 12:25:16 +01:00
|
|
|
<RoomsCategory name="Spaces" roomIds={spaceIds.sort(roomIdByAtoZ)} drawerPostie={drawerPostie} />
|
2022-03-03 14:06:53 +01:00
|
|
|
)}
|
2021-08-30 17:42:24 +02:00
|
|
|
|
2022-03-03 14:06:53 +01:00
|
|
|
{ roomIds.length !== 0 && (
|
2022-03-17 12:25:16 +01:00
|
|
|
<RoomsCategory name="Rooms" roomIds={roomIds.sort(roomIdByAtoZ)} drawerPostie={drawerPostie} />
|
2022-03-03 14:06:53 +01:00
|
|
|
)}
|
2021-09-03 14:28:01 +02:00
|
|
|
|
2022-03-03 14:06:53 +01:00
|
|
|
{ directIds.length !== 0 && (
|
2022-03-17 12:25:16 +01:00
|
|
|
<RoomsCategory name="People" roomIds={directIds.sort(roomIdByActivity)} drawerPostie={drawerPostie} />
|
2022-03-03 14:06:53 +01:00
|
|
|
)}
|
2022-02-27 16:40:54 +01:00
|
|
|
|
2022-03-17 12:25:16 +01:00
|
|
|
{ isCategorized && [...categories].map(([catId, childIds]) => {
|
|
|
|
const rms = [];
|
|
|
|
const dms = [];
|
|
|
|
childIds.forEach((id) => {
|
|
|
|
if (directs.has(id)) dms.push(id);
|
|
|
|
else rms.push(id);
|
|
|
|
});
|
|
|
|
rms.sort(roomIdByAtoZ);
|
|
|
|
dms.sort(roomIdByActivity);
|
|
|
|
return (
|
|
|
|
<RoomsCategory
|
|
|
|
key={catId}
|
|
|
|
spaceId={catId}
|
|
|
|
name={mx.getRoom(catId).name}
|
|
|
|
roomIds={rms.concat(dms)}
|
|
|
|
drawerPostie={drawerPostie}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
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;
|