From c1133ebfc6f43e2fe5e4d0bd872e38383edac0b6 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 17 Dec 2019 14:05:51 +0000 Subject: [PATCH] null-guard useIsEncrypted --- src/components/views/right_panel/UserInfo.js | 4 ++-- src/hooks/useEventEmitter.js | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/components/views/right_panel/UserInfo.js b/src/components/views/right_panel/UserInfo.js index 90bb3f3dcb..d1d7aa0371 100644 --- a/src/components/views/right_panel/UserInfo.js +++ b/src/components/views/right_panel/UserInfo.js @@ -109,14 +109,14 @@ function openDMForUser(matrixClient, userId) { } function useIsEncrypted(cli, room) { - const [isEncrypted, setIsEncrypted] = useState(cli.isRoomEncrypted(room.roomId)); + const [isEncrypted, setIsEncrypted] = useState(room ? cli.isRoomEncrypted(room.roomId) : undefined); const update = useCallback((event) => { if (event.getType() === "m.room.encryption") { setIsEncrypted(cli.isRoomEncrypted(room.roomId)); } }, [cli, room]); - useEventEmitter(room.currentState, "RoomState.events", update); + useEventEmitter(room ? room.currentState : undefined, "RoomState.events", update); return isEncrypted; } diff --git a/src/hooks/useEventEmitter.js b/src/hooks/useEventEmitter.js index 56676bf871..7adc6ef2e3 100644 --- a/src/hooks/useEventEmitter.js +++ b/src/hooks/useEventEmitter.js @@ -28,6 +28,9 @@ export const useEventEmitter = (emitter, eventName, handler) => { useEffect( () => { + // allow disabling this hook by passing a falsy emitter + if (!emitter) return; + // Create event listener that calls handler function stored in ref const eventListener = event => savedHandler.current(event);