Prefer RoomStateEvent.Update where possible as it fires far less (#7878)
This commit is contained in:
parent
36ae0ea49d
commit
c257bc3f7a
40 changed files with 223 additions and 190 deletions
|
@ -18,19 +18,21 @@ import { useCallback, useState } from "react";
|
|||
import { MatrixClient } from "matrix-js-sdk/src/client";
|
||||
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
||||
import { Room } from "matrix-js-sdk/src/models/room";
|
||||
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
|
||||
import { EventType } from "matrix-js-sdk/src/@types/event";
|
||||
|
||||
import { useEventEmitter } from "./useEventEmitter";
|
||||
import { useTypedEventEmitter } from "./useEventEmitter";
|
||||
|
||||
// Hook to simplify watching whether a Matrix room is encrypted, returns undefined if room is undefined
|
||||
export function useIsEncrypted(cli: MatrixClient, room?: Room): boolean | undefined {
|
||||
const [isEncrypted, setIsEncrypted] = useState(room ? cli.isRoomEncrypted(room.roomId) : undefined);
|
||||
|
||||
const update = useCallback((event: MatrixEvent) => {
|
||||
if (room && event.getType() === "m.room.encryption") {
|
||||
if (room && event.getType() === EventType.RoomEncryption) {
|
||||
setIsEncrypted(cli.isRoomEncrypted(room.roomId));
|
||||
}
|
||||
}, [cli, room]);
|
||||
useEventEmitter(room ? room.currentState : undefined, "RoomState.events", update);
|
||||
useTypedEventEmitter(room?.currentState, RoomStateEvent.Events, update);
|
||||
|
||||
return isEncrypted;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue