diff --git a/src/app/components/element-call/CallView.tsx b/src/app/components/element-call/CallView.tsx index 2f1ecc33..7aff6f49 100644 --- a/src/app/components/element-call/CallView.tsx +++ b/src/app/components/element-call/CallView.tsx @@ -69,4 +69,83 @@ export function CallView({ onJoin = undefined, onClose = undefined, onHangup = undefined, -}: IRoomCallViewProps): JSX.Element {} +}: IRoomCallViewProps): JSX.Element { + // Construct required variables + const room = useRoom(); + const client = useMatrixClient(); + const iframe = useRef(null); + + // Model state + const [elementCall, setElementCall] = useState(); + const [widgetApi, setWidgetApi] = useState(null); + const [state, setState] = useState(State.Preparing); + + // Initialization parameters + const isDirect = useIsDirectRoom(); + const callOngoing = useCallOngoing(room); + const initialCallOngoing = React.useRef(callOngoing); + const initialIsDirect = React.useRef(isDirect); + useEffect(() => { + if (client && room && !elementCall) { + const e = new ElementCall(client, room, initialIsDirect.current, initialCallOngoing.current); + setElementCall(e); + } + }, [client, room, setElementCall, elementCall]); + + // Start the messaging over the widget api. + useEffect(() => { + if (iframe.current && elementCall) { + elementCall.startMessaging(iframe.current); + } + return () => { + elementCall?.stopMessaging(); + }; + }, [iframe, elementCall]); + + // Widget api ready + useEventEmitter(elementCall, 'ready', () => { + setWidgetApi(elementCall?.widgetApi ?? null); + setState(State.Lobby); + }); + + // Use widget api to listen for hangup/join/close actions + useEventEmitter(widgetApi, action(CallWidgetActions.HangupCall), () => { + setState(State.HungUp); + onHangup?.(); + }); + useEventEmitter(widgetApi, action(CallWidgetActions.JoinCall), () => { + setState(State.Joined); + onJoin?.(); + }); + useEventEmitter(widgetApi, action(CallWidgetActions.Close), () => { + setState(State.CanClose); + onClose?.(); + }); + + // render component + return ( +
+ {/* Exit button for lobby state */} + {state === State.Lobby && ( + + )} +