2020-12-09 11:02:30 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
2021-10-22 22:23:32 +00:00
|
|
|
import { throttle } from "lodash";
|
2020-12-09 11:02:30 +00:00
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
import { useEventEmitter } from "./useEventEmitter";
|
2020-12-09 11:02:30 +00:00
|
|
|
|
2020-12-09 11:09:18 +00:00
|
|
|
// Hook to simplify watching Matrix Room joined members
|
2020-12-09 11:02:30 +00:00
|
|
|
export const useRoomMembers = (room: Room, throttleWait = 250) => {
|
|
|
|
const [members, setMembers] = useState<RoomMember[]>(room.getJoinedMembers());
|
|
|
|
useEventEmitter(room.currentState, "RoomState.members", throttle(() => {
|
|
|
|
setMembers(room.getJoinedMembers());
|
2021-06-29 12:11:58 +00:00
|
|
|
}, throttleWait, { leading: true, trailing: true }));
|
2020-12-09 11:02:30 +00:00
|
|
|
return members;
|
|
|
|
};
|
2020-12-09 11:07:40 +00:00
|
|
|
|
2020-12-09 11:09:18 +00:00
|
|
|
// Hook to simplify watching Matrix Room joined member count
|
2020-12-09 11:07:40 +00:00
|
|
|
export const useRoomMemberCount = (room: Room, throttleWait = 250) => {
|
|
|
|
const [count, setCount] = useState<number>(room.getJoinedMemberCount());
|
|
|
|
useEventEmitter(room.currentState, "RoomState.members", throttle(() => {
|
|
|
|
setCount(room.getJoinedMemberCount());
|
2021-06-29 12:11:58 +00:00
|
|
|
}, throttleWait, { leading: true, trailing: true }));
|
2020-12-09 11:07:40 +00:00
|
|
|
return count;
|
|
|
|
};
|