From 9c5fde32585cd25a92c612450faefa87fd5df5eb Mon Sep 17 00:00:00 2001 From: Gigiaj Date: Wed, 16 Apr 2025 19:49:11 -0500 Subject: [PATCH] Clean up file and remove broken import --- src/app/pages/client/CallProvider.tsx | 65 ++++++++------------------- 1 file changed, 18 insertions(+), 47 deletions(-) diff --git a/src/app/pages/client/CallProvider.tsx b/src/app/pages/client/CallProvider.tsx index 2f308394..227ea1b7 100644 --- a/src/app/pages/client/CallProvider.tsx +++ b/src/app/pages/client/CallProvider.tsx @@ -1,35 +1,28 @@ import React, { createContext, useState, useContext, useMemo, useCallback, ReactNode } from 'react'; import { logger } from 'matrix-js-sdk/lib/logger'; -// Import the transport type and action types from matrix-widget-api -import { WidgetApiToWidgetAction, ITransport, AnyWidgetAction } from 'matrix-widget-api'; // Ensure ITransport and AnyWidgetAction are imported +import { WidgetApiToWidgetAction, ITransport, WidgetApiAction } from 'matrix-widget-api'; -// Define the shape of the context value interface CallContextState { activeCallRoomId: string | null; setActiveCallRoomId: (roomId: string | null) => void; hangUp: () => void; - // --- New additions for API interaction --- - activeApiTransport: ITransport | null; // Hold the active transport - registerActiveTransport: (roomId: string | null, transport: ITransport | null) => void; // Function for PersistentCallContainer to register/unregister + activeApiTransport: ITransport | null; + registerActiveTransport: (roomId: string | null, transport: ITransport | null) => void; sendWidgetAction: ( - action: WidgetApiToWidgetAction | string, // Allow standard actions or custom string actions + action: WidgetApiToWidgetAction | string, data: T - ) => Promise; // Function for any component to send actions + ) => Promise; } -// Create the context const CallContext = createContext(undefined); interface CallProviderProps { children: ReactNode; } -// Create the Provider component export function CallProvider({ children }: CallProviderProps) { const [activeCallRoomId, setActiveCallRoomIdState] = useState(null); - // --- New State: Hold the transport of the currently active widget API --- const [activeApiTransport, setActiveApiTransport] = useState(null); - // Store the room ID associated with the current active transport const [transportRoomId, setTransportRoomId] = useState(null); // --- Actions --- @@ -37,7 +30,6 @@ export function CallProvider({ children }: CallProviderProps) { (roomId: string | null) => { logger.debug(`CallContext: Setting activeCallRoomId to ${roomId}`); setActiveCallRoomIdState(roomId); - // If the room being cleared is the one associated with the transport, clear the transport if (roomId === null || roomId !== transportRoomId) { logger.debug( `CallContext: Clearing active transport because active room changed or was cleared.` @@ -47,50 +39,37 @@ export function CallProvider({ children }: CallProviderProps) { } }, [transportRoomId] - ); // Depends on transportRoomId to avoid clearing unnecessarily + ); const hangUp = useCallback(() => { logger.debug(`CallContext: Hang up called.`); setActiveCallRoomIdState(null); - // Also clear the transport on hangup logger.debug(`CallContext: Clearing active transport due to hangup.`); setActiveApiTransport(null); setTransportRoomId(null); }, []); - // --- New Action: Register/Unregister Transport --- - // Called by PersistentCallContainer when its widget API is ready or cleaned up - // Now accepts the roomId associated with the transport const registerActiveTransport = useCallback( (roomId: string | null, transport: ITransport | null) => { if (roomId && transport) { logger.debug(`CallContext: Registering active transport for room ${roomId}.`); setActiveApiTransport(transport); setTransportRoomId(roomId); + } else if (roomId === transportRoomId || roomId === null) { + logger.debug(`CallContext: Clearing active transport for room ${transportRoomId}.`); + setActiveApiTransport(null); + setTransportRoomId(null); } else { - // Only clear if the transport being cleared belongs to the currently stored roomId - // or if roomId is explicitly null (global cleanup) - if (roomId === transportRoomId || roomId === null) { - logger.debug(`CallContext: Clearing active transport for room ${transportRoomId}.`); - setActiveApiTransport(null); - setTransportRoomId(null); - } else { - logger.debug( - `CallContext: Ignoring transport clear request for room ${roomId}, as current transport belongs to ${transportRoomId}.` - ); - } + logger.debug( + `CallContext: Ignoring transport clear request for room ${roomId}, as current transport belongs to ${transportRoomId}.` + ); } }, - [transportRoomId] // Depends on the currently stored transportRoomId + [transportRoomId] ); - // --- New Action: Send Action to Widget --- - // Can be called by any component within the provider const sendWidgetAction = useCallback( - async ( - action: WidgetApiToWidgetAction | string, // Use the imported type or allow custom strings - data: T - ): Promise => { + async (action: WidgetApiToWidgetAction | string, data: T): Promise => { if (!activeApiTransport) { logger.warn( `CallContext: Cannot send action '${action}', no active API transport registered.` @@ -108,17 +87,13 @@ export function CallProvider({ children }: CallProviderProps) { `CallContext: Sending action '${action}' via active transport (room: ${transportRoomId}) with data:`, data ); - // Use the transport's send method. - // The 'action' parameter should match expected Widget API actions. - // The transport handles wrapping this in the correct message format. - await activeApiTransport.send(action as AnyWidgetAction, data); // Cast action type if needed by transport method signature + await activeApiTransport.send(action as WidgetApiAction, data); } catch (error) { logger.error(`CallContext: Error sending action '${action}':`, error); return Promise.reject(error); - // Handle error appropriately } }, - [activeApiTransport, activeCallRoomId, transportRoomId] // Depends on the current transport and active room IDs + [activeApiTransport, activeCallRoomId, transportRoomId] ); // --- Memoize Context Value --- @@ -127,8 +102,7 @@ export function CallProvider({ children }: CallProviderProps) { activeCallRoomId, setActiveCallRoomId, hangUp, - // Add new state and actions to the context value - activeApiTransport, // Keep exposing transport if direct access is ever needed, but prefer sendWidgetAction + activeApiTransport, registerActiveTransport, sendWidgetAction, }), @@ -142,12 +116,9 @@ export function CallProvider({ children }: CallProviderProps) { ] ); - // Provide the context value to children return {children}; } -// --- Custom Hook --- -// Remains the same, but now returns the extended context value export function useCallState(): CallContextState { const context = useContext(CallContext); if (context === undefined) {