mirror of
https://github.com/vector-im/element-call.git
synced 2026-09-22 22:29:30 +00:00
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
/*
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
Please see LICENSE in the repository root for full details.
|
|
*/
|
|
|
|
import {
|
|
type CallMembership,
|
|
type MatrixRTCSession,
|
|
MatrixRTCSessionEvent,
|
|
} from "matrix-js-sdk/lib/matrixrtc";
|
|
import { useCallback, useSyncExternalStore } from "react";
|
|
|
|
const NONE: CallMembership[] = [];
|
|
|
|
/**
|
|
* The memberships of a matrix-js-sdk session, kept current; none without a
|
|
* session (the Rust crate carries the call then).
|
|
*/
|
|
export function useMatrixRTCSessionMemberships(
|
|
rtcSession: MatrixRTCSession | undefined,
|
|
): CallMembership[] {
|
|
const subscribe = useCallback(
|
|
(onChange: () => void) => {
|
|
if (rtcSession === undefined) return (): void => {};
|
|
rtcSession.on(MatrixRTCSessionEvent.MembershipsChanged, onChange);
|
|
return (): void => {
|
|
rtcSession.off(MatrixRTCSessionEvent.MembershipsChanged, onChange);
|
|
};
|
|
},
|
|
[rtcSession],
|
|
);
|
|
return useSyncExternalStore(
|
|
subscribe,
|
|
useCallback(() => rtcSession?.memberships ?? NONE, [rtcSession]),
|
|
);
|
|
}
|