61 lines
2.1 KiB
TypeScript
61 lines
2.1 KiB
TypeScript
import { Room } from 'matrix-js-sdk';
|
|
import {
|
|
MatrixRTCSession,
|
|
MatrixRTCSessionEvent,
|
|
MatrixRTCSessionEventHandlerMap,
|
|
} from 'matrix-js-sdk/lib/matrixrtc/MatrixRTCSession';
|
|
import { CallMembership } from 'matrix-js-sdk/lib/matrixrtc/CallMembership';
|
|
import { useEffect, useState } from 'react';
|
|
import { MatrixRTCSessionManagerEvents } from 'matrix-js-sdk/lib/matrixrtc/MatrixRTCSessionManager';
|
|
import { useMatrixClient } from './useMatrixClient';
|
|
|
|
export const useCallSession = (room: Room): MatrixRTCSession => {
|
|
const mx = useMatrixClient();
|
|
|
|
const [session, setSession] = useState(mx.matrixRTC.getRoomSession(room));
|
|
|
|
useEffect(() => {
|
|
const start = (roomId: string) => {
|
|
if (roomId !== room.roomId) return;
|
|
setSession(mx.matrixRTC.getRoomSession(room));
|
|
};
|
|
const end = (roomId: string) => {
|
|
if (roomId !== room.roomId) return;
|
|
setSession(mx.matrixRTC.getRoomSession(room));
|
|
};
|
|
mx.matrixRTC.on(MatrixRTCSessionManagerEvents.SessionStarted, start);
|
|
mx.matrixRTC.on(MatrixRTCSessionManagerEvents.SessionEnded, end);
|
|
return () => {
|
|
mx.matrixRTC.off(MatrixRTCSessionManagerEvents.SessionStarted, start);
|
|
mx.matrixRTC.off(MatrixRTCSessionManagerEvents.SessionEnded, end);
|
|
};
|
|
}, [mx, room]);
|
|
|
|
return session;
|
|
};
|
|
|
|
export const useCallMembersChange = (
|
|
session: MatrixRTCSession,
|
|
callback: (members: CallMembership[]) => void,
|
|
): void => {
|
|
useEffect(() => {
|
|
const handleMembershipsChange: MatrixRTCSessionEventHandlerMap[MatrixRTCSessionEvent.MembershipsChanged] =
|
|
(oldestMembership, newMemberships) => {
|
|
callback(newMemberships);
|
|
};
|
|
|
|
session.on(MatrixRTCSessionEvent.MembershipsChanged, handleMembershipsChange);
|
|
return () => {
|
|
session.removeListener(MatrixRTCSessionEvent.MembershipsChanged, handleMembershipsChange);
|
|
};
|
|
}, [session, callback]);
|
|
};
|
|
|
|
export const useCallMembers = (session: MatrixRTCSession): CallMembership[] => {
|
|
const [memberships, setMemberships] = useState<CallMembership[]>(session.memberships);
|
|
|
|
useCallMembersChange(session, setMemberships);
|
|
|
|
return memberships;
|
|
};
|