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
|
|
|
|
2024-09-06 10:22:13 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
Please see LICENSE in the repository root for full details.
|
2023-08-16 18:41:27 +01:00
|
|
|
*/
|
|
|
|
|
|
2023-08-18 09:03:21 +01:00
|
|
|
import { logger } from "matrix-js-sdk/src/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,
|
|
|
|
|
} from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession";
|
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
|
|
|
|
|
|
export function useMatrixRTCSessionJoinState(
|
2023-10-11 10:42:04 -04:00
|
|
|
rtcSession: MatrixRTCSession,
|
2023-08-16 18:41:27 +01:00
|
|
|
): boolean {
|
|
|
|
|
const [isJoined, setJoined] = useState(rtcSession.isJoined());
|
|
|
|
|
|
2024-06-19 16:41:52 +02:00
|
|
|
const onJoinStateChanged = useCallback(
|
|
|
|
|
(isJoined: boolean) => {
|
|
|
|
|
logger.info(
|
|
|
|
|
`Session in room ${rtcSession.room.roomId} changed to ${
|
|
|
|
|
isJoined ? "joined" : "left"
|
|
|
|
|
}`,
|
|
|
|
|
);
|
|
|
|
|
setJoined(isJoined);
|
|
|
|
|
},
|
|
|
|
|
[rtcSession],
|
|
|
|
|
);
|
2023-08-16 18:41:27 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
rtcSession.on(MatrixRTCSessionEvent.JoinStateChanged, onJoinStateChanged);
|
|
|
|
|
|
2024-06-04 11:20:25 -04:00
|
|
|
return (): void => {
|
2023-08-16 18:41:27 +01:00
|
|
|
rtcSession.off(
|
|
|
|
|
MatrixRTCSessionEvent.JoinStateChanged,
|
2023-10-11 10:42:04 -04:00
|
|
|
onJoinStateChanged,
|
2023-08-16 18:41:27 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
}, [rtcSession, onJoinStateChanged]);
|
|
|
|
|
|
|
|
|
|
return isJoined;
|
|
|
|
|
}
|