2021-10-21 15:20:49 +03:00
|
|
|
import React, { useState, useEffect, useCallback } from 'react';
|
2021-07-28 16:15:52 +03:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import './PeopleDrawer.scss';
|
|
|
|
|
|
|
|
import initMatrix from '../../../client/initMatrix';
|
2021-10-18 18:25:52 +03:00
|
|
|
import { getPowerLabel, getUsernameOfRoomMember } from '../../../util/matrixUtil';
|
2021-07-28 16:15:52 +03:00
|
|
|
import colorMXID from '../../../util/colorMXID';
|
2021-10-18 18:25:52 +03:00
|
|
|
import { openInviteUser, openProfileViewer } from '../../../client/action/navigation';
|
2021-10-21 15:20:49 +03:00
|
|
|
import AsyncSearch from '../../../util/AsyncSearch';
|
2021-07-28 16:15:52 +03:00
|
|
|
|
|
|
|
import Text from '../../atoms/text/Text';
|
|
|
|
import Header, { TitleWrapper } from '../../atoms/header/Header';
|
|
|
|
import IconButton from '../../atoms/button/IconButton';
|
|
|
|
import Button from '../../atoms/button/Button';
|
|
|
|
import ScrollView from '../../atoms/scroll/ScrollView';
|
|
|
|
import Input from '../../atoms/input/Input';
|
|
|
|
import PeopleSelector from '../../molecules/people-selector/PeopleSelector';
|
|
|
|
|
|
|
|
import AddUserIC from '../../../../public/res/ic/outlined/add-user.svg';
|
|
|
|
|
2021-09-05 16:26:34 +03:00
|
|
|
function AtoZ(m1, m2) {
|
|
|
|
const aName = m1.name;
|
|
|
|
const bName = m2.name;
|
2021-07-28 16:15:52 +03:00
|
|
|
|
|
|
|
if (aName.toLowerCase() < bName.toLowerCase()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (aName.toLowerCase() > bName.toLowerCase()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
function sortByPowerLevel(m1, m2) {
|
2021-09-05 16:26:34 +03:00
|
|
|
const pl1 = m1.powerLevel;
|
|
|
|
const pl2 = m2.powerLevel;
|
2021-07-28 16:15:52 +03:00
|
|
|
|
2021-09-05 16:26:34 +03:00
|
|
|
if (pl1 > pl2) return -1;
|
|
|
|
if (pl1 < pl2) return 1;
|
2021-07-28 16:15:52 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2021-10-21 15:20:49 +03:00
|
|
|
function simplyfiMembers(members) {
|
|
|
|
const mx = initMatrix.matrixClient;
|
|
|
|
return members.map((member) => ({
|
|
|
|
userId: member.userId,
|
|
|
|
name: getUsernameOfRoomMember(member),
|
|
|
|
username: member.userId.slice(1, member.userId.indexOf(':')),
|
|
|
|
avatarSrc: member.getAvatarUrl(mx.baseUrl, 24, 24, 'crop'),
|
|
|
|
peopleRole: getPowerLabel(member.powerLevel),
|
|
|
|
powerLevel: members.powerLevel,
|
|
|
|
}));
|
|
|
|
}
|
2021-07-28 16:15:52 +03:00
|
|
|
|
2021-10-21 15:20:49 +03:00
|
|
|
const asyncSearch = new AsyncSearch();
|
2021-07-28 16:15:52 +03:00
|
|
|
function PeopleDrawer({ roomId }) {
|
|
|
|
const PER_PAGE_MEMBER = 50;
|
2021-10-21 15:20:49 +03:00
|
|
|
const mx = initMatrix.matrixClient;
|
|
|
|
const room = mx.getRoom(roomId);
|
2021-07-28 16:15:52 +03:00
|
|
|
let isRoomChanged = false;
|
|
|
|
|
2021-10-21 15:20:49 +03:00
|
|
|
const [itemCount, setItemCount] = useState(PER_PAGE_MEMBER);
|
|
|
|
const [membership, setMembership] = useState('join');
|
|
|
|
const [memberList, setMemberList] = useState([]);
|
|
|
|
const [searchedMembers, setSearchedMembers] = useState(null);
|
|
|
|
|
|
|
|
const getMembersWithMembership = useCallback(
|
|
|
|
(mship) => room.getMembersWithMembership(mship),
|
|
|
|
[roomId, membership],
|
|
|
|
);
|
|
|
|
|
2021-07-28 16:15:52 +03:00
|
|
|
function loadMorePeople() {
|
2021-10-21 15:20:49 +03:00
|
|
|
setItemCount(itemCount + PER_PAGE_MEMBER);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleSearchData(data) {
|
|
|
|
// NOTICE: data is passed as object property
|
|
|
|
// because react sucks at handling state update with array.
|
|
|
|
setSearchedMembers({ data });
|
|
|
|
setItemCount(PER_PAGE_MEMBER);
|
2021-07-28 16:15:52 +03:00
|
|
|
}
|
|
|
|
|
2021-10-21 15:20:49 +03:00
|
|
|
function handleSearch(e) {
|
|
|
|
if (e.target.value === '') {
|
|
|
|
setSearchedMembers(null);
|
|
|
|
setItemCount(PER_PAGE_MEMBER);
|
|
|
|
} else asyncSearch.search(e.target.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
asyncSearch.setup(memberList, {
|
|
|
|
keys: ['name', 'username', 'userId'],
|
|
|
|
limit: PER_PAGE_MEMBER,
|
|
|
|
});
|
|
|
|
}, [memberList]);
|
|
|
|
|
2021-07-28 16:15:52 +03:00
|
|
|
useEffect(() => {
|
2021-10-21 15:20:49 +03:00
|
|
|
setMemberList(
|
|
|
|
simplyfiMembers(
|
|
|
|
getMembersWithMembership(membership)
|
|
|
|
.sort(AtoZ).sort(sortByPowerLevel),
|
|
|
|
),
|
|
|
|
);
|
2021-07-28 16:15:52 +03:00
|
|
|
room.loadMembersIfNeeded().then(() => {
|
|
|
|
if (isRoomChanged) return;
|
2021-10-21 15:20:49 +03:00
|
|
|
setMemberList(
|
|
|
|
simplyfiMembers(
|
|
|
|
getMembersWithMembership(membership)
|
|
|
|
.sort(AtoZ).sort(sortByPowerLevel),
|
|
|
|
),
|
|
|
|
);
|
2021-07-28 16:15:52 +03:00
|
|
|
});
|
|
|
|
|
2021-10-21 15:20:49 +03:00
|
|
|
asyncSearch.on(asyncSearch.RESULT_SENT, handleSearchData);
|
2021-07-28 16:15:52 +03:00
|
|
|
return () => {
|
|
|
|
isRoomChanged = true;
|
2021-10-21 15:20:49 +03:00
|
|
|
setMemberList([]);
|
|
|
|
setSearchedMembers(null);
|
|
|
|
setItemCount(PER_PAGE_MEMBER);
|
|
|
|
asyncSearch.removeListener(asyncSearch.RESULT_SENT, handleSearchData);
|
2021-07-28 16:15:52 +03:00
|
|
|
};
|
|
|
|
}, [roomId]);
|
|
|
|
|
2021-10-21 15:20:49 +03:00
|
|
|
const mList = searchedMembers !== null ? searchedMembers.data : memberList.slice(0, itemCount);
|
2021-07-28 16:15:52 +03:00
|
|
|
return (
|
|
|
|
<div className="people-drawer">
|
|
|
|
<Header>
|
|
|
|
<TitleWrapper>
|
|
|
|
<Text variant="s1">
|
|
|
|
People
|
|
|
|
<Text className="people-drawer__member-count" variant="b3">{`${room.getJoinedMemberCount()} members`}</Text>
|
|
|
|
</Text>
|
|
|
|
</TitleWrapper>
|
|
|
|
<IconButton onClick={() => openInviteUser(roomId)} tooltip="Invite" src={AddUserIC} />
|
|
|
|
</Header>
|
|
|
|
<div className="people-drawer__content-wrapper">
|
|
|
|
<div className="people-drawer__scrollable">
|
|
|
|
<ScrollView autoHide>
|
|
|
|
<div className="people-drawer__content">
|
|
|
|
{
|
2021-10-21 15:20:49 +03:00
|
|
|
mList.map((member) => (
|
2021-07-28 16:15:52 +03:00
|
|
|
<PeopleSelector
|
|
|
|
key={member.userId}
|
2021-10-18 18:25:52 +03:00
|
|
|
onClick={() => openProfileViewer(member.userId, roomId)}
|
2021-10-21 15:20:49 +03:00
|
|
|
avatarSrc={member.avatarSrc}
|
|
|
|
name={member.name}
|
2021-07-28 16:15:52 +03:00
|
|
|
color={colorMXID(member.userId)}
|
2021-10-21 15:20:49 +03:00
|
|
|
peopleRole={member.peopleRole}
|
2021-07-28 16:15:52 +03:00
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
<div className="people-drawer__load-more">
|
|
|
|
{
|
2021-10-22 14:32:42 +03:00
|
|
|
mList.length !== 0 && memberList.length > itemCount && (
|
2021-07-28 16:15:52 +03:00
|
|
|
<Button onClick={loadMorePeople}>View more</Button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ScrollView>
|
|
|
|
</div>
|
|
|
|
<div className="people-drawer__sticky">
|
|
|
|
<form onSubmit={(e) => e.preventDefault()} className="people-search">
|
2021-10-21 15:20:49 +03:00
|
|
|
<Input type="text" onChange={handleSearch} placeholder="Search" required />
|
2021-07-28 16:15:52 +03:00
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
PeopleDrawer.propTypes = {
|
|
|
|
roomId: PropTypes.string.isRequired,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PeopleDrawer;
|