Skip to content
Snippets Groups Projects
Commit 80cd092a authored by Timo K's avatar Timo K
Browse files

don't use deprecated `room.currentState` in useRoomState.


Signed-off-by: default avatarTimo K <toger5@hotmail.de>
parent 1149b135
Branches master
No related tags found
No related merge requests found
...@@ -15,7 +15,7 @@ limitations under the License. ...@@ -15,7 +15,7 @@ limitations under the License.
*/ */
import { useCallback, useEffect, useRef, useState } from "react"; import { useCallback, useEffect, useRef, useState } from "react";
import { Room, RoomState, RoomStateEvent } from "matrix-js-sdk/src/matrix"; import { EventTimeline, Room, RoomState, RoomStateEvent } from "matrix-js-sdk/src/matrix";
import { useTypedEventEmitter } from "./useEventEmitter"; import { useTypedEventEmitter } from "./useEventEmitter";
...@@ -28,6 +28,9 @@ export const useRoomState = <T extends any = RoomState>( ...@@ -28,6 +28,9 @@ export const useRoomState = <T extends any = RoomState>(
room?: Room, room?: Room,
mapper: Mapper<T> = defaultMapper as Mapper<T>, mapper: Mapper<T> = defaultMapper as Mapper<T>,
): T => { ): T => {
function getCurrentState(room: Room | undefined): RoomState | undefined {
return room?.getLiveTimeline().getState(EventTimeline.FORWARDS);
}
// Create a ref that stores mapper // Create a ref that stores mapper
const savedMapper = useRef(mapper); const savedMapper = useRef(mapper);
...@@ -36,18 +39,23 @@ export const useRoomState = <T extends any = RoomState>( ...@@ -36,18 +39,23 @@ export const useRoomState = <T extends any = RoomState>(
savedMapper.current = mapper; savedMapper.current = mapper;
}, [mapper]); }, [mapper]);
const [value, setValue] = useState<T>(room ? mapper(room.currentState) : (undefined as T)); const [value, setValue] = useState<T>(() => {
const roomState = getCurrentState(room);
return roomState ? mapper(roomState) : (undefined as T);
});
const update = useCallback(() => { const update = useCallback(() => {
if (!room) return; const roomState = getCurrentState(room);
setValue(savedMapper.current(room.currentState)); if (!roomState) return;
setValue(savedMapper.current(roomState));
}, [room]); }, [room]);
useTypedEventEmitter(room?.currentState, RoomStateEvent.Update, update); useTypedEventEmitter(room, RoomStateEvent.Update, update);
useEffect(() => { useEffect(() => {
update(); update();
return () => { return () => {
setValue(room ? savedMapper.current(room.currentState) : (undefined as T)); const roomState = getCurrentState(room);
setValue(roomState ? savedMapper.current(roomState) : (undefined as T));
}; };
}, [room, update]); }, [room, update]);
return value; return value;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment