add groundwork for call roomtimeline toggle

This commit is contained in:
Gigiaj 2025-04-18 03:01:44 -05:00
parent 8f87690b00
commit 1ac5b3d8fd

View file

@ -8,12 +8,7 @@ import React, {
useEffect, useEffect,
} from 'react'; } from 'react';
import { logger } from 'matrix-js-sdk/lib/logger'; import { logger } from 'matrix-js-sdk/lib/logger';
import { import { WidgetApiToWidgetAction, ITransport, WidgetApiAction } from 'matrix-widget-api';
WidgetApiToWidgetAction,
ITransport,
WidgetApiAction,
WidgetApiFromWidgetAction,
} from 'matrix-widget-api';
interface MediaStatePayload { interface MediaStatePayload {
audioEnabled?: boolean; audioEnabled?: boolean;
@ -21,6 +16,7 @@ interface MediaStatePayload {
} }
const WIDGET_MEDIA_STATE_UPDATE_ACTION = 'io.element.device_mute'; const WIDGET_MEDIA_STATE_UPDATE_ACTION = 'io.element.device_mute';
const WIDGET_HANGUP_ACTION = 'io.element.hangup';
const SET_MEDIA_STATE_ACTION = 'io.element.device_mute'; const SET_MEDIA_STATE_ACTION = 'io.element.device_mute';
@ -36,8 +32,10 @@ interface CallContextState {
) => Promise<void>; ) => Promise<void>;
isAudioEnabled: boolean; isAudioEnabled: boolean;
isVideoEnabled: boolean; isVideoEnabled: boolean;
isChatOpen: boolean;
toggleAudio: () => Promise<void>; toggleAudio: () => Promise<void>;
toggleVideo: () => Promise<void>; toggleVideo: () => Promise<void>;
toggleChat: () => Promise<void>;
} }
const CallContext = createContext<CallContextState | undefined>(undefined); const CallContext = createContext<CallContextState | undefined>(undefined);
@ -48,6 +46,7 @@ interface CallProviderProps {
const DEFAULT_AUDIO_ENABLED = false; const DEFAULT_AUDIO_ENABLED = false;
const DEFAULT_VIDEO_ENABLED = false; const DEFAULT_VIDEO_ENABLED = false;
const DEFAULT_CHAT_OPENED = false;
export function CallProvider({ children }: CallProviderProps) { export function CallProvider({ children }: CallProviderProps) {
const [activeCallRoomId, setActiveCallRoomIdState] = useState<string | null>(null); const [activeCallRoomId, setActiveCallRoomIdState] = useState<string | null>(null);
@ -56,11 +55,13 @@ export function CallProvider({ children }: CallProviderProps) {
const [isAudioEnabled, setIsAudioEnabledState] = useState<boolean>(DEFAULT_AUDIO_ENABLED); const [isAudioEnabled, setIsAudioEnabledState] = useState<boolean>(DEFAULT_AUDIO_ENABLED);
const [isVideoEnabled, setIsVideoEnabledState] = useState<boolean>(DEFAULT_VIDEO_ENABLED); const [isVideoEnabled, setIsVideoEnabledState] = useState<boolean>(DEFAULT_VIDEO_ENABLED);
const [isChatOpen, setIsChatOpenState] = useState<boolean>(DEFAULT_CHAT_OPENED);
const resetMediaState = useCallback(() => { const resetMediaState = useCallback(() => {
logger.debug('CallContext: Resetting media state to defaults.'); logger.debug('CallContext: Resetting media state to defaults.');
setIsAudioEnabledState(DEFAULT_AUDIO_ENABLED); setIsAudioEnabledState(DEFAULT_AUDIO_ENABLED);
setIsVideoEnabledState(DEFAULT_VIDEO_ENABLED); setIsVideoEnabledState(DEFAULT_VIDEO_ENABLED);
setIsChatOpenState(DEFAULT_CHAT_OPENED);
}, []); }, []);
const setActiveCallRoomId = useCallback( const setActiveCallRoomId = useCallback(
@ -154,13 +155,13 @@ export function CallProvider({ children }: CallProviderProps) {
}; };
logger.debug(`CallContext: Setting up listeners for transport in room ${activeCallRoomId}`); logger.debug(`CallContext: Setting up listeners for transport in room ${activeCallRoomId}`);
transport.on(`action:${WidgetApiFromWidgetAction.HangupCall}`, handleHangup); // Use standard hangup action name transport.on(`action:${WIDGET_HANGUP_ACTION}`, handleHangup); // Use standard hangup action name
transport.on(`action:${WIDGET_MEDIA_STATE_UPDATE_ACTION}`, handleMediaStateUpdate); transport.on(`action:${WIDGET_MEDIA_STATE_UPDATE_ACTION}`, handleMediaStateUpdate);
return () => { return () => {
logger.debug(`CallContext: Cleaning up listeners for transport in room ${activeCallRoomId}`); logger.debug(`CallContext: Cleaning up listeners for transport in room ${activeCallRoomId}`);
if (transport) { if (transport) {
transport.off(`action:${WidgetApiFromWidgetAction.HangupCall}`, handleHangup); transport.off(`action:${WIDGET_HANGUP_ACTION}`, handleHangup);
transport.off(`action:${WIDGET_MEDIA_STATE_UPDATE_ACTION}`, handleMediaStateUpdate); transport.off(`action:${WIDGET_MEDIA_STATE_UPDATE_ACTION}`, handleMediaStateUpdate);
} }
}; };
@ -169,6 +170,7 @@ export function CallProvider({ children }: CallProviderProps) {
activeCallRoomId, activeCallRoomId,
transportRoomId, transportRoomId,
hangUp, hangUp,
isChatOpen,
isAudioEnabled, isAudioEnabled,
isVideoEnabled, isVideoEnabled,
]); ]);
@ -235,6 +237,11 @@ export function CallProvider({ children }: CallProviderProps) {
} }
}, [isVideoEnabled, isAudioEnabled, sendWidgetAction]); }, [isVideoEnabled, isAudioEnabled, sendWidgetAction]);
const toggleChat = useCallback(async () => {
const newState = !isChatOpen;
setIsChatOpenState(!newState);
}, [isChatOpen]);
const contextValue = useMemo<CallContextState>( const contextValue = useMemo<CallContextState>(
() => ({ () => ({
activeCallRoomId, activeCallRoomId,
@ -243,10 +250,12 @@ export function CallProvider({ children }: CallProviderProps) {
activeApiTransport, activeApiTransport,
registerActiveTransport, registerActiveTransport,
sendWidgetAction, sendWidgetAction,
isChatOpen,
isAudioEnabled, isAudioEnabled,
isVideoEnabled, isVideoEnabled,
toggleAudio, toggleAudio,
toggleVideo, toggleVideo,
toggleChat,
}), }),
[ [
activeCallRoomId, activeCallRoomId,