2023-08-16 18:41:27 +01:00
|
|
|
/*
|
2024-09-06 10:22:13 +02:00
|
|
|
Copyright 2023, 2024 New Vector Ltd.
|
2023-08-16 18:41:27 +01: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.
|
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 {
|
2024-12-11 09:27:55 +00:00
|
|
|
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";
|
2025-06-05 07:54:57 -04:00
|
|
|
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(
|
2025-01-17 10:30:28 -05:00
|
|
|
rtcSession: MatrixRTCSession | undefined,
|
2023-08-16 18:41:27 +01:00
|
|
|
): boolean {
|
2025-06-05 07:54:57 -04:00
|
|
|
// 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(() => {
|
2025-06-05 07:54:57 -04:00
|
|
|
logger.info(
|
|
|
|
|
`Session in room ${rtcSession?.room.roomId} changed to ${
|
|
|
|
|
isJoined ? "joined" : "left"
|
|
|
|
|
}`,
|
|
|
|
|
);
|
|
|
|
|
}, [rtcSession, isJoined]);
|
2023-08-16 18:41:27 +01:00
|
|
|
|
2025-06-05 07:54:57 -04:00
|
|
|
return isJoined;
|
2023-08-16 18:41:27 +01:00
|
|
|
}
|