Files
element-call/src/useMatrixRTCSessionJoinState.ts
T

41 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-08-16 18:41:27 +01:00
/*
Copyright 2023, 2024 New Vector Ltd.
2023-08-16 18:41:27 +01:00
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
2023-08-16 18:41:27 +01:00
*/
2025-03-13 13:58:43 +01:00
import { logger } from "matrix-js-sdk/lib/logger";
2023-08-16 18:41:27 +01:00
import {
type MatrixRTCSession,
2023-08-16 18:41:27 +01:00
MatrixRTCSessionEvent,
2025-03-13 13:58:43 +01:00
} from "matrix-js-sdk/lib/matrixrtc";
import { TypedEventEmitter } from "matrix-js-sdk";
import { useCallback, useEffect } from "react";
import { useTypedEventEmitterState } from "./useEvents";
const dummySession = new TypedEventEmitter();
2023-08-16 18:41:27 +01:00
export function useMatrixRTCSessionJoinState(
rtcSession: MatrixRTCSession | undefined,
2023-08-16 18:41:27 +01:00
): boolean {
// React doesn't allow you to run a hook conditionally, so we have to plug in
// a dummy event emitter in case there is no rtcSession yet
const isJoined = useTypedEventEmitterState(
rtcSession ?? dummySession,
MatrixRTCSessionEvent.JoinStateChanged,
useCallback(() => rtcSession?.isJoined() ?? false, [rtcSession]),
);
2023-08-16 18:41:27 +01:00
useEffect(() => {
logger.info(
`Session in room ${rtcSession?.room.roomId} changed to ${
isJoined ? "joined" : "left"
}`,
);
}, [rtcSession, isJoined]);
2023-08-16 18:41:27 +01:00
return isJoined;
2023-08-16 18:41:27 +01:00
}