From 21c7bb979ee1ade3bfb18b1435ea1960dd45c282 Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 30 May 2022 15:30:57 +0100 Subject: [PATCH 1/2] Convert useGroupCall to TS --- src/room/{useGroupCall.js => useGroupCall.ts} | 59 ++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) rename src/room/{useGroupCall.js => useGroupCall.ts} (82%) diff --git a/src/room/useGroupCall.js b/src/room/useGroupCall.ts similarity index 82% rename from src/room/useGroupCall.js rename to src/room/useGroupCall.ts index 51900c5..25c2179 100644 --- a/src/room/useGroupCall.js +++ b/src/room/useGroupCall.ts @@ -18,10 +18,33 @@ import { useCallback, useEffect, useState } from "react"; import { GroupCallEvent, GroupCallState, + GroupCall, } from "matrix-js-sdk/src/webrtc/groupCall"; +import { MatrixCall } from "matrix-js-sdk/src/webrtc/call"; +import { CallFeed } from "matrix-js-sdk/src/webrtc/callFeed"; +import { RoomMember } from "matrix-js-sdk/src/models/room-member"; + import { usePageUnload } from "./usePageUnload"; -export function useGroupCall(groupCall) { +interface State { + state: GroupCallState; + calls: MatrixCall[]; + localCallFeed: CallFeed; + activeSpeaker: string; + userMediaFeeds: CallFeed[]; + error: Error; + microphoneMuted: boolean; + localVideoMuted: boolean; + screenshareFeeds: CallFeed[]; + localScreenshareFeed: CallFeed; + localDesktopCapturerSourceId: string; + isScreensharing: boolean; + requestingScreenshare: boolean; + participants: RoomMember[]; + hasLocalParticipant: boolean; +} + +export function useGroupCall(groupCall: GroupCall) { const [ { state, @@ -41,20 +64,25 @@ export function useGroupCall(groupCall) { requestingScreenshare, }, setState, - ] = useState({ + ] = useState({ state: GroupCallState.LocalCallFeedUninitialized, calls: [], + localCallFeed: null, + activeSpeaker: null, userMediaFeeds: [], + error: null, microphoneMuted: false, localVideoMuted: false, - screenshareFeeds: [], isScreensharing: false, + screenshareFeeds: [], + localScreenshareFeed: null, + localDesktopCapturerSourceId: null, requestingScreenshare: false, participants: [], hasLocalParticipant: false, }); - const updateState = (state) => + const updateState = (state: Partial) => setState((prevState) => ({ ...prevState, ...state })); useEffect(() => { @@ -75,25 +103,28 @@ export function useGroupCall(groupCall) { }); } - function onUserMediaFeedsChanged(userMediaFeeds) { + function onUserMediaFeedsChanged(userMediaFeeds: CallFeed[]): void { updateState({ userMediaFeeds: [...userMediaFeeds], }); } - function onScreenshareFeedsChanged(screenshareFeeds) { + function onScreenshareFeedsChanged(screenshareFeeds: CallFeed[]): void { updateState({ screenshareFeeds: [...screenshareFeeds], }); } - function onActiveSpeakerChanged(activeSpeaker) { + function onActiveSpeakerChanged(activeSpeaker: string): void { updateState({ activeSpeaker: activeSpeaker, }); } - function onLocalMuteStateChanged(microphoneMuted, localVideoMuted) { + function onLocalMuteStateChanged( + microphoneMuted: boolean, + localVideoMuted: boolean + ): void { updateState({ microphoneMuted, localVideoMuted, @@ -101,10 +132,10 @@ export function useGroupCall(groupCall) { } function onLocalScreenshareStateChanged( - isScreensharing, - localScreenshareFeed, - localDesktopCapturerSourceId - ) { + isScreensharing: boolean, + localScreenshareFeed: CallFeed, + localDesktopCapturerSourceId: string + ): void { updateState({ isScreensharing, localScreenshareFeed, @@ -112,13 +143,13 @@ export function useGroupCall(groupCall) { }); } - function onCallsChanged(calls) { + function onCallsChanged(calls: MatrixCall[]): void { updateState({ calls: [...calls], }); } - function onParticipantsChanged(participants) { + function onParticipantsChanged(participants: RoomMember[]): void { updateState({ participants: [...participants], hasLocalParticipant: groupCall.hasLocalParticipant(), From 1164e6f1e7282e394a2ed7409f795e73707cff2d Mon Sep 17 00:00:00 2001 From: David Baker Date: Mon, 30 May 2022 15:53:44 +0100 Subject: [PATCH 2/2] Add return type too --- src/room/useGroupCall.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/room/useGroupCall.ts b/src/room/useGroupCall.ts index 25c2179..6816bcf 100644 --- a/src/room/useGroupCall.ts +++ b/src/room/useGroupCall.ts @@ -26,6 +26,30 @@ import { RoomMember } from "matrix-js-sdk/src/models/room-member"; import { usePageUnload } from "./usePageUnload"; +export interface UseGroupCallType { + state: GroupCallState; + calls: MatrixCall[]; + localCallFeed: CallFeed; + activeSpeaker: string; + userMediaFeeds: CallFeed[]; + microphoneMuted: boolean; + localVideoMuted: boolean; + error: Error; + initLocalCallFeed: () => void; + enter: () => void; + leave: () => void; + toggleLocalVideoMuted: () => void; + toggleMicrophoneMuted: () => void; + toggleScreensharing: () => void; + requestingScreenshare: boolean; + isScreensharing: boolean; + screenshareFeeds: CallFeed[]; + localScreenshareFeed: CallFeed; + localDesktopCapturerSourceId: string; + participants: RoomMember[]; + hasLocalParticipant: boolean; +} + interface State { state: GroupCallState; calls: MatrixCall[]; @@ -44,7 +68,7 @@ interface State { hasLocalParticipant: boolean; } -export function useGroupCall(groupCall: GroupCall) { +export function useGroupCall(groupCall: GroupCall): UseGroupCallType { const [ { state,