import React, { useState, useEffect } from 'react'; import PropTypes from 'prop-types'; import './PeopleDrawer.scss'; import initMatrix from '../../../client/initMatrix'; import { getUsername } from '../../../util/matrixUtil'; import colorMXID from '../../../util/colorMXID'; import { openInviteUser } from '../../../client/action/navigation'; 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'; function getPowerLabel(powerLevel) { switch (powerLevel) { case 100: return 'Admin'; case 50: return 'Mod'; default: return null; } } function compare(m1, m2) { let aName = m1.name; let bName = m2.name; // remove "#" from the room name // To ignore it in sorting aName = aName.replaceAll('#', ''); bName = bName.replaceAll('#', ''); if (aName.toLowerCase() < bName.toLowerCase()) { return -1; } if (aName.toLowerCase() > bName.toLowerCase()) { return 1; } return 0; } function sortByPowerLevel(m1, m2) { let pl1 = String(m1.powerLevel); let pl2 = String(m2.powerLevel); if (pl1 === '100') pl1 = '90.9'; if (pl2 === '100') pl2 = '90.9'; if (pl1.toLowerCase() > pl2.toLowerCase()) { return -1; } if (pl1.toLowerCase() < pl2.toLowerCase()) { return 1; } return 0; } function PeopleDrawer({ roomId }) { const PER_PAGE_MEMBER = 50; const room = initMatrix.matrixClient.getRoom(roomId); const totalMemberList = room.getJoinedMembers().sort(compare).sort(sortByPowerLevel); const [memberList, updateMemberList] = useState([]); let isRoomChanged = false; function loadMorePeople() { updateMemberList(totalMemberList.slice(0, memberList.length + PER_PAGE_MEMBER)); } useEffect(() => { updateMemberList(totalMemberList.slice(0, PER_PAGE_MEMBER)); room.loadMembersIfNeeded().then(() => { if (isRoomChanged) return; const newTotalMemberList = room.getJoinedMembers().sort(compare).sort(sortByPowerLevel); updateMemberList(newTotalMemberList.slice(0, PER_PAGE_MEMBER)); }); return () => { isRoomChanged = true; }; }, [roomId]); return (