2022-08-30 15:13:39 -04:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-08-30 15:13:39 -04:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
2024-09-09 14:57:16 +01:00
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-08-30 15:13:39 -04:00
|
|
|
*/
|
|
|
|
|
|
2022-10-14 15:17:49 +02:00
|
|
|
import { useState, useCallback, useMemo } from "react";
|
2022-08-30 15:13:39 -04:00
|
|
|
|
2023-08-04 12:22:08 +01:00
|
|
|
import type { RoomMember } from "matrix-js-sdk/src/matrix";
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type Call, ConnectionState, CallEvent } from "../models/Call";
|
2023-08-03 13:56:30 +01:00
|
|
|
import { useTypedEventEmitterState, useEventEmitter } from "./useEventEmitter";
|
2022-08-30 15:13:39 -04:00
|
|
|
import { CallStore, CallStoreEvent } from "../stores/CallStore";
|
2022-10-07 22:16:35 +02:00
|
|
|
import SdkConfig, { DEFAULTS } from "../SdkConfig";
|
|
|
|
|
import { _t } from "../languageHandler";
|
2022-08-30 15:13:39 -04:00
|
|
|
|
|
|
|
|
export const useCall = (roomId: string): Call | null => {
|
2022-10-06 22:27:28 -04:00
|
|
|
const [call, setCall] = useState(() => CallStore.instance.getCall(roomId));
|
2022-08-30 15:13:39 -04:00
|
|
|
useEventEmitter(CallStore.instance, CallStoreEvent.Call, (call: Call | null, forRoomId: string) => {
|
|
|
|
|
if (forRoomId === roomId) setCall(call);
|
|
|
|
|
});
|
|
|
|
|
return call;
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-03 23:44:38 -05:00
|
|
|
export const useCallForWidget = (widgetId: string, roomId: string): Call | null => {
|
|
|
|
|
const call = useCall(roomId);
|
|
|
|
|
return call?.widget.id === widgetId ? call : null;
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-31 16:18:52 +01:00
|
|
|
export const useConnectionState = (call: Call | null): ConnectionState =>
|
2022-08-30 15:13:39 -04:00
|
|
|
useTypedEventEmitterState(
|
2024-01-31 16:18:52 +01:00
|
|
|
call ?? undefined,
|
2022-08-30 15:13:39 -04:00
|
|
|
CallEvent.ConnectionState,
|
2024-01-31 16:18:52 +01:00
|
|
|
useCallback((state) => state ?? call?.connectionState ?? ConnectionState.Disconnected, [call]),
|
2022-08-30 15:13:39 -04:00
|
|
|
);
|
|
|
|
|
|
2024-01-31 16:18:52 +01:00
|
|
|
export const useParticipants = (call: Call | null): Map<RoomMember, Set<string>> => {
|
|
|
|
|
return useTypedEventEmitterState(
|
|
|
|
|
call ?? undefined,
|
2022-08-30 15:13:39 -04:00
|
|
|
CallEvent.Participants,
|
2024-01-31 16:18:52 +01:00
|
|
|
useCallback((state) => state ?? call?.participants ?? [], [call]),
|
2022-08-30 15:13:39 -04:00
|
|
|
);
|
2024-01-31 16:18:52 +01:00
|
|
|
};
|
2022-10-06 22:27:28 -04:00
|
|
|
|
2024-01-31 16:18:52 +01:00
|
|
|
export const useParticipantCount = (call: Call | null): number => {
|
2022-10-14 15:17:49 +02:00
|
|
|
const participants = useParticipants(call);
|
|
|
|
|
|
|
|
|
|
return useMemo(() => {
|
2022-11-28 16:37:32 -05:00
|
|
|
let count = 0;
|
|
|
|
|
for (const devices of participants.values()) count += devices.size;
|
|
|
|
|
return count;
|
|
|
|
|
}, [participants]);
|
2022-10-14 15:17:49 +02:00
|
|
|
};
|
|
|
|
|
|
2022-11-28 16:37:32 -05:00
|
|
|
export const useParticipatingMembers = (call: Call): RoomMember[] => {
|
|
|
|
|
const participants = useParticipants(call);
|
|
|
|
|
|
|
|
|
|
return useMemo(() => {
|
|
|
|
|
const members: RoomMember[] = [];
|
|
|
|
|
for (const [member, devices] of participants) {
|
|
|
|
|
// Repeat the member for as many devices as they're using
|
|
|
|
|
for (let i = 0; i < devices.size; i++) members.push(member);
|
|
|
|
|
}
|
|
|
|
|
return members;
|
|
|
|
|
}, [participants]);
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-08 17:48:50 +01:00
|
|
|
export const useFull = (call: Call | null): boolean => {
|
2022-11-28 16:37:32 -05:00
|
|
|
return (
|
|
|
|
|
useParticipantCount(call) >=
|
|
|
|
|
(SdkConfig.get("element_call").participant_limit ?? DEFAULTS.element_call.participant_limit!)
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-08 17:48:50 +01:00
|
|
|
export const useJoinCallButtonDisabledTooltip = (call: Call | null): string | null => {
|
2022-10-07 22:16:35 +02:00
|
|
|
const isFull = useFull(call);
|
2025-03-05 15:41:26 -05:00
|
|
|
return isFull ? _t("voip|join_button_tooltip_call_full") : null;
|
2022-10-07 22:16:35 +02:00
|
|
|
};
|