cinny/src/app/organisms/navigation/Drawer.jsx

86 lines
2.9 KiB
React
Raw Normal View History

2021-07-28 16:15:52 +03:00
import React, { useState, useEffect } from 'react';
import './Drawer.scss';
2021-09-05 16:26:34 +03:00
import initMatrix from '../../../client/initMatrix';
2021-07-28 16:15:52 +03:00
import cons from '../../../client/state/cons';
import navigation from '../../../client/state/navigation';
2021-09-05 16:26:34 +03:00
import { selectTab, selectSpace } from '../../../client/action/navigation';
2021-07-28 16:15:52 +03:00
import Text from '../../atoms/text/Text';
2021-07-28 16:15:52 +03:00
import ScrollView from '../../atoms/scroll/ScrollView';
import DrawerHeader from './DrawerHeader';
2021-09-03 15:28:01 +03:00
import DrawerBreadcrumb from './DrawerBreadcrumb';
import Home from './Home';
import Directs from './Directs';
2021-07-28 16:15:52 +03:00
2021-08-30 06:01:13 +03:00
function Drawer() {
const [systemState, setSystemState] = useState(null);
2021-09-05 16:26:34 +03:00
const [selectedTab, setSelectedTab] = useState(navigation.selectedTab);
2021-09-03 15:28:01 +03:00
const [spaceId, setSpaceId] = useState(navigation.selectedSpaceId);
2021-08-30 06:01:13 +03:00
2021-09-05 16:26:34 +03:00
function onTabSelected(tabId) {
2021-09-03 15:28:01 +03:00
setSelectedTab(tabId);
}
function onSpaceSelected(roomId) {
setSpaceId(roomId);
2021-08-30 06:01:13 +03:00
}
2021-09-05 16:26:34 +03:00
function onRoomLeaved(roomId) {
const lRoomIndex = navigation.selectedSpacePath.indexOf(roomId);
if (lRoomIndex === -1) return;
if (lRoomIndex === 0) selectTab(cons.tabs.HOME);
else selectSpace(navigation.selectedSpacePath[lRoomIndex - 1]);
}
2021-08-30 06:01:13 +03:00
function handleSystemState(state) {
if (state === 'ERROR' || state === 'RECONNECTING' || state === 'STOPPED') {
setSystemState({ status: 'Connection lost!' });
}
if (systemState !== null) setSystemState(null);
}
2021-08-30 06:01:13 +03:00
useEffect(() => {
2021-09-05 16:26:34 +03:00
navigation.on(cons.events.navigation.TAB_SELECTED, onTabSelected);
2021-09-03 15:28:01 +03:00
navigation.on(cons.events.navigation.SPACE_SELECTED, onSpaceSelected);
2021-09-05 16:26:34 +03:00
initMatrix.roomList.on(cons.events.roomList.ROOM_LEAVED, onRoomLeaved);
2021-08-30 06:01:13 +03:00
return () => {
2021-09-05 16:26:34 +03:00
navigation.removeListener(cons.events.navigation.TAB_SELECTED, onTabSelected);
2021-09-03 15:28:01 +03:00
navigation.removeListener(cons.events.navigation.SPACE_SELECTED, onSpaceSelected);
2021-09-05 16:26:34 +03:00
initMatrix.roomList.removeListener(cons.events.roomList.ROOM_LEAVED, onRoomLeaved);
2021-08-30 06:01:13 +03:00
};
}, []);
useEffect(() => {
initMatrix.matrixClient.on('sync', handleSystemState);
return () => {
initMatrix.matrixClient.removeListener('sync', handleSystemState);
};
}, [systemState]);
2021-07-28 16:15:52 +03:00
return (
<div className="drawer">
2021-09-03 15:28:01 +03:00
<DrawerHeader selectedTab={selectedTab} spaceId={spaceId} />
2021-07-28 16:15:52 +03:00
<div className="drawer__content-wrapper">
2021-09-05 16:26:34 +03:00
{selectedTab !== cons.tabs.DIRECTS && <DrawerBreadcrumb spaceId={spaceId} />}
2021-08-31 16:13:31 +03:00
<div className="rooms__wrapper">
2021-07-28 16:15:52 +03:00
<ScrollView autoHide>
2021-08-31 16:13:31 +03:00
<div className="rooms-container">
{
2021-09-05 16:26:34 +03:00
selectedTab !== cons.tabs.DIRECTS
2021-09-03 15:28:01 +03:00
? <Home spaceId={spaceId} />
: <Directs />
}
</div>
2021-07-28 16:15:52 +03:00
</ScrollView>
</div>
</div>
{ systemState !== null && (
<div className="drawer__state">
<Text>{systemState.status}</Text>
</div>
)}
2021-07-28 16:15:52 +03:00
</div>
);
}
export default Drawer;