mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-11-06 15:30:27 +03:00
Add storing widget for comparing with (since we already store room id and the clientWidgetApi anyway)
This commit is contained in:
parent
3818671446
commit
f2f98a6973
1 changed files with 54 additions and 22 deletions
|
|
@ -10,6 +10,7 @@ import React, {
|
||||||
import { logger } from 'matrix-js-sdk/lib/logger';
|
import { logger } from 'matrix-js-sdk/lib/logger';
|
||||||
import { WidgetApiToWidgetAction, WidgetApiAction, ClientWidgetApi } from 'matrix-widget-api';
|
import { WidgetApiToWidgetAction, WidgetApiAction, ClientWidgetApi } from 'matrix-widget-api';
|
||||||
import { useParams } from 'react-router-dom';
|
import { useParams } from 'react-router-dom';
|
||||||
|
import { SmallWidget } from '../../../features/call/SmallWidget';
|
||||||
|
|
||||||
interface MediaStatePayload {
|
interface MediaStatePayload {
|
||||||
data?: {
|
data?: {
|
||||||
|
|
@ -30,13 +31,18 @@ interface CallContextState {
|
||||||
setViewedCallRoomId: (roomId: string | null) => void;
|
setViewedCallRoomId: (roomId: string | null) => void;
|
||||||
hangUp: () => void;
|
hangUp: () => void;
|
||||||
activeClientWidgetApi: ClientWidgetApi | null;
|
activeClientWidgetApi: ClientWidgetApi | null;
|
||||||
|
activeClientWidget: SmallWidget | null;
|
||||||
registerActiveClientWidgetApi: (
|
registerActiveClientWidgetApi: (
|
||||||
roomId: string | null,
|
roomId: string | null,
|
||||||
clientWidgetApi: ClientWidgetApi | null
|
clientWidgetApi: ClientWidgetApi | null,
|
||||||
|
clientWidget: SmallWidget
|
||||||
) => void;
|
) => void;
|
||||||
|
viewedClientWidgetApi: ClientWidgetApi | null;
|
||||||
|
viewedClientWidget: SmallWidget | null;
|
||||||
registerViewedClientWidgetApi: (
|
registerViewedClientWidgetApi: (
|
||||||
roomId: string | null,
|
roomId: string | null,
|
||||||
clientWidgetApi: ClientWidgetApi | null
|
clientWidgetApi: ClientWidgetApi | null,
|
||||||
|
clientWidget: SmallWidget
|
||||||
) => void;
|
) => void;
|
||||||
sendWidgetAction: <T = unknown>(
|
sendWidgetAction: <T = unknown>(
|
||||||
action: WidgetApiToWidgetAction | string,
|
action: WidgetApiToWidgetAction | string,
|
||||||
|
|
@ -71,12 +77,14 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
const [activeClientWidgetApi, setActiveClientWidgetApiState] = useState<ClientWidgetApi | null>(
|
const [activeClientWidgetApi, setActiveClientWidgetApiState] = useState<ClientWidgetApi | null>(
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
|
const [activeClientWidget, setActiveClientWidget] = useState<SmallWidget | null>(null);
|
||||||
const [activeClientWidgetApiRoomId, setActiveClientWidgetApiRoomId] = useState<string | null>(
|
const [activeClientWidgetApiRoomId, setActiveClientWidgetApiRoomId] = useState<string | null>(
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
const [viewedClientWidgetApi, setViewedClientWidgetApiState] = useState<ClientWidgetApi | null>(
|
const [viewedClientWidgetApi, setViewedClientWidgetApiState] = useState<ClientWidgetApi | null>(
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
|
const [viewedClientWidget, setViewedClientWidget] = useState<SmallWidget | null>(null);
|
||||||
const [viewedClientWidgetApiRoomId, setViewedClientWidgetApiRoomId] = useState<string | null>(
|
const [viewedClientWidgetApiRoomId, setViewedClientWidgetApiRoomId] = useState<string | null>(
|
||||||
null
|
null
|
||||||
);
|
);
|
||||||
|
|
@ -124,25 +132,34 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
);
|
);
|
||||||
|
|
||||||
const setActiveClientWidgetApi = useCallback(
|
const setActiveClientWidgetApi = useCallback(
|
||||||
(clientWidgetApi: ClientWidgetApi | null, roomId: string | null) => {
|
(
|
||||||
|
clientWidgetApi: ClientWidgetApi | null,
|
||||||
|
clientWidget: SmallWidget | null,
|
||||||
|
roomId: string | null
|
||||||
|
) => {
|
||||||
setActiveClientWidgetApiState(clientWidgetApi);
|
setActiveClientWidgetApiState(clientWidgetApi);
|
||||||
|
setActiveClientWidget(clientWidget);
|
||||||
setActiveClientWidgetApiRoomId(roomId);
|
setActiveClientWidgetApiRoomId(roomId);
|
||||||
},
|
},
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
|
||||||
const registerActiveClientWidgetApi = useCallback(
|
const registerActiveClientWidgetApi = useCallback(
|
||||||
(roomId: string | null, clientWidgetApi: ClientWidgetApi | null) => {
|
(
|
||||||
|
roomId: string | null,
|
||||||
|
clientWidgetApi: ClientWidgetApi | null,
|
||||||
|
clientWidget: SmallWidget | null
|
||||||
|
) => {
|
||||||
if (activeClientWidgetApi && activeClientWidgetApi !== clientWidgetApi) {
|
if (activeClientWidgetApi && activeClientWidgetApi !== clientWidgetApi) {
|
||||||
logger.debug(`CallContext: Cleaning up listeners for previous clientWidgetApi instance.`);
|
logger.debug(`CallContext: Cleaning up listeners for previous clientWidgetApi instance.`);
|
||||||
activeClientWidgetApi.removeAllListeners();
|
//activeClientWidgetApi.removeAllListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (roomId && clientWidgetApi) {
|
if (roomId && clientWidgetApi) {
|
||||||
logger.debug(`CallContext: Registering active clientWidgetApi for room ${roomId}.`);
|
logger.debug(`CallContext: Registering active clientWidgetApi for room ${roomId}.`);
|
||||||
setActiveClientWidgetApi(clientWidgetApi, roomId);
|
setActiveClientWidgetApi(clientWidgetApi, clientWidget, roomId);
|
||||||
} else if (roomId === activeClientWidgetApiRoomId || roomId === null) {
|
} else if (roomId === activeClientWidgetApiRoomId || roomId === null) {
|
||||||
setActiveClientWidgetApi(null, null);
|
setActiveClientWidgetApi(null, null, null);
|
||||||
resetMediaState();
|
resetMediaState();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -150,29 +167,36 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
);
|
);
|
||||||
|
|
||||||
const setViewedClientWidgetApi = useCallback(
|
const setViewedClientWidgetApi = useCallback(
|
||||||
(clientWidgetApi: ClientWidgetApi | null, roomId: string | null) => {
|
(
|
||||||
|
clientWidgetApi: ClientWidgetApi | null,
|
||||||
|
clientWidget: SmallWidget | null,
|
||||||
|
roomId: string | null
|
||||||
|
) => {
|
||||||
setViewedClientWidgetApiState(clientWidgetApi);
|
setViewedClientWidgetApiState(clientWidgetApi);
|
||||||
|
setViewedClientWidget(clientWidget);
|
||||||
setViewedClientWidgetApiRoomId(roomId);
|
setViewedClientWidgetApiRoomId(roomId);
|
||||||
},
|
},
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
|
||||||
const registerViewedClientWidgetApi = useCallback(
|
const registerViewedClientWidgetApi = useCallback(
|
||||||
(roomId: string | null, clientWidgetApi: ClientWidgetApi | null) => {
|
(
|
||||||
|
roomId: string | null,
|
||||||
|
clientWidgetApi: ClientWidgetApi | null,
|
||||||
|
clientWidget: SmallWidget | null
|
||||||
|
) => {
|
||||||
if (viewedClientWidgetApi && viewedClientWidgetApi !== clientWidgetApi) {
|
if (viewedClientWidgetApi && viewedClientWidgetApi !== clientWidgetApi) {
|
||||||
logger.debug(`CallContext: Cleaning up listeners for previous clientWidgetApi instance.`);
|
logger.debug(`CallContext: Cleaning up listeners for previous clientWidgetApi instance.`);
|
||||||
viewedClientWidgetApi.removeAllListeners();
|
|
||||||
viewedClientWidgetApi.stop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (roomId && clientWidgetApi) {
|
if (roomId && clientWidgetApi) {
|
||||||
logger.debug(`CallContext: Registering viewed clientWidgetApi for room ${roomId}.`);
|
logger.debug(`CallContext: Registering viewed clientWidgetApi for room ${roomId}.`);
|
||||||
setViewedClientWidgetApi(clientWidgetApi, roomId);
|
setViewedClientWidgetApi(clientWidgetApi, clientWidget, roomId);
|
||||||
} else if (roomId === viewedClientWidgetApiRoomId || roomId === null) {
|
} else if (roomId === viewedClientWidgetApiRoomId || roomId === null) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`CallContext: Clearing viewed clientWidgetApi for room ${viewedClientWidgetApiRoomId}.`
|
`CallContext: Clearing viewed clientWidgetApi for room ${viewedClientWidgetApiRoomId}.`
|
||||||
);
|
);
|
||||||
setViewedClientWidgetApi(null, null);
|
setViewedClientWidgetApi(null, null, null);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[viewedClientWidgetApi, viewedClientWidgetApiRoomId, setViewedClientWidgetApi]
|
[viewedClientWidgetApi, viewedClientWidgetApiRoomId, setViewedClientWidgetApi]
|
||||||
|
|
@ -181,10 +205,16 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
const hangUp = useCallback(() => {
|
const hangUp = useCallback(() => {
|
||||||
logger.debug(`CallContext: Hang up called.`);
|
logger.debug(`CallContext: Hang up called.`);
|
||||||
activeClientWidgetApi?.transport.send(`${WIDGET_HANGUP_ACTION}`, {});
|
activeClientWidgetApi?.transport.send(`${WIDGET_HANGUP_ACTION}`, {});
|
||||||
setActiveClientWidgetApi(null, null);
|
setActiveClientWidgetApi(null, null, null);
|
||||||
setViewedCallRoomId(activeCallRoomId);
|
setViewedCallRoomId(activeCallRoomId);
|
||||||
|
|
||||||
setIsCallActive(false);
|
setIsCallActive(false);
|
||||||
}, [activeCallRoomId, activeClientWidgetApi?.transport, setActiveClientWidgetApi, setViewedCallRoomId]);
|
}, [
|
||||||
|
activeCallRoomId,
|
||||||
|
activeClientWidgetApi?.transport,
|
||||||
|
setActiveClientWidgetApi,
|
||||||
|
setViewedCallRoomId,
|
||||||
|
]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!activeCallRoomId && !viewedCallRoomId) {
|
if (!activeCallRoomId && !viewedCallRoomId) {
|
||||||
|
|
@ -195,11 +225,8 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
if (ev.detail.widgetId === activeClientWidgetApi?.widget.id) {
|
if (ev.detail.widgetId === activeClientWidgetApi?.widget.id) {
|
||||||
activeClientWidgetApi?.transport.reply(ev.detail, {});
|
activeClientWidgetApi?.transport.reply(ev.detail, {});
|
||||||
setIsCallActive(false);
|
setIsCallActive(false);
|
||||||
} else if (ev.detail.widgetId === viewedClientWidgetApi?.widget.id) {
|
|
||||||
viewedClientWidgetApi?.transport.reply(ev.detail, {});
|
|
||||||
setIsCallActive(false);
|
|
||||||
}
|
}
|
||||||
logger.warn(
|
logger.debug(
|
||||||
`CallContext: Received hangup action from widget in room ${activeCallRoomId}.`,
|
`CallContext: Received hangup action from widget in room ${activeCallRoomId}.`,
|
||||||
ev
|
ev
|
||||||
);
|
);
|
||||||
|
|
@ -238,13 +265,11 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
const handleJoin = (ev: CustomEvent) => {
|
const handleJoin = (ev: CustomEvent) => {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
const setViewedAsActive = () => {
|
const setViewedAsActive = () => {
|
||||||
setActiveClientWidgetApi(viewedClientWidgetApi, viewedCallRoomId);
|
setActiveClientWidgetApi(viewedClientWidgetApi, viewedClientWidget, viewedCallRoomId);
|
||||||
setActiveCallRoomIdState(viewedCallRoomId);
|
setActiveCallRoomIdState(viewedCallRoomId);
|
||||||
setViewedClientWidgetApi(null, null);
|
|
||||||
setIsPrimaryIframe(!isPrimaryIframe);
|
setIsPrimaryIframe(!isPrimaryIframe);
|
||||||
setIsCallActive(true);
|
setIsCallActive(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
activeClientWidgetApi?.transport.reply(ev.detail, {});
|
activeClientWidgetApi?.transport.reply(ev.detail, {});
|
||||||
if (ev.detail.widgetId === activeClientWidgetApi?.widget.id) {
|
if (ev.detail.widgetId === activeClientWidgetApi?.widget.id) {
|
||||||
setIsCallActive(true);
|
setIsCallActive(true);
|
||||||
|
|
@ -291,6 +316,7 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
viewedCallRoomId,
|
viewedCallRoomId,
|
||||||
setViewedClientWidgetApi,
|
setViewedClientWidgetApi,
|
||||||
setActiveClientWidgetApi,
|
setActiveClientWidgetApi,
|
||||||
|
viewedClientWidget,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const sendWidgetAction = useCallback(
|
const sendWidgetAction = useCallback(
|
||||||
|
|
@ -367,7 +393,10 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
hangUp,
|
hangUp,
|
||||||
activeClientWidgetApi,
|
activeClientWidgetApi,
|
||||||
registerActiveClientWidgetApi,
|
registerActiveClientWidgetApi,
|
||||||
|
activeClientWidget,
|
||||||
|
viewedClientWidgetApi,
|
||||||
registerViewedClientWidgetApi,
|
registerViewedClientWidgetApi,
|
||||||
|
viewedClientWidget,
|
||||||
sendWidgetAction,
|
sendWidgetAction,
|
||||||
isChatOpen,
|
isChatOpen,
|
||||||
isAudioEnabled,
|
isAudioEnabled,
|
||||||
|
|
@ -387,7 +416,10 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
hangUp,
|
hangUp,
|
||||||
activeClientWidgetApi,
|
activeClientWidgetApi,
|
||||||
registerActiveClientWidgetApi,
|
registerActiveClientWidgetApi,
|
||||||
|
activeClientWidget,
|
||||||
|
viewedClientWidgetApi,
|
||||||
registerViewedClientWidgetApi,
|
registerViewedClientWidgetApi,
|
||||||
|
viewedClientWidget,
|
||||||
sendWidgetAction,
|
sendWidgetAction,
|
||||||
isChatOpen,
|
isChatOpen,
|
||||||
isAudioEnabled,
|
isAudioEnabled,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue