2022-10-14 09:40:21 -04:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2022-2024 New Vector Ltd.
|
2022-10-14 09:40:21 -04:00
|
|
|
|
2025-02-18 17:59:58 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-06 10:22:13 +02:00
|
|
|
Please see LICENSE in the repository root for full details.
|
2022-10-14 09:40:21 -04:00
|
|
|
*/
|
|
|
|
|
|
2025-06-05 07:54:57 -04:00
|
|
|
import { useCallback } from "react";
|
|
|
|
|
import {
|
|
|
|
|
type RoomState,
|
|
|
|
|
RoomStateEvent,
|
|
|
|
|
type Room,
|
|
|
|
|
RoomEvent,
|
|
|
|
|
} from "matrix-js-sdk";
|
2022-10-14 09:40:21 -04:00
|
|
|
|
2025-06-05 07:54:57 -04:00
|
|
|
import { useTypedEventEmitterState } from "../useEvents";
|
2022-10-14 09:40:21 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A React hook for values computed from room state.
|
|
|
|
|
* @param room The room.
|
|
|
|
|
* @param f A mapping from the current room state to the computed value.
|
|
|
|
|
* @returns The computed value.
|
|
|
|
|
*/
|
2025-06-05 07:54:57 -04:00
|
|
|
export function useRoomState<T>(room: Room, f: (state: RoomState) => T): T {
|
|
|
|
|
// TODO: matrix-js-sdk says that Room.currentState is deprecated, but it's not
|
|
|
|
|
// clear how to reactively track the current state of the room without it
|
|
|
|
|
const currentState = useTypedEventEmitterState(
|
2022-10-14 09:40:21 -04:00
|
|
|
room,
|
2025-06-05 07:54:57 -04:00
|
|
|
RoomEvent.CurrentStateUpdated,
|
|
|
|
|
useCallback(() => room.currentState, [room]),
|
|
|
|
|
);
|
|
|
|
|
return useTypedEventEmitterState(
|
|
|
|
|
currentState,
|
2022-10-14 09:40:21 -04:00
|
|
|
RoomStateEvent.Update,
|
2025-06-05 07:54:57 -04:00
|
|
|
useCallback(() => f(currentState), [f, currentState]),
|
2022-10-14 09:40:21 -04:00
|
|
|
);
|
2025-06-05 07:54:57 -04:00
|
|
|
}
|