mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-11-06 23:30:28 +03:00
Clean up file and remove broken import
This commit is contained in:
parent
99e55b36c6
commit
9c5fde3258
1 changed files with 18 additions and 47 deletions
|
|
@ -1,35 +1,28 @@
|
||||||
import React, { createContext, useState, useContext, useMemo, useCallback, ReactNode } from 'react';
|
import React, { createContext, useState, useContext, useMemo, useCallback, ReactNode } from 'react';
|
||||||
import { logger } from 'matrix-js-sdk/lib/logger';
|
import { logger } from 'matrix-js-sdk/lib/logger';
|
||||||
// Import the transport type and action types from matrix-widget-api
|
import { WidgetApiToWidgetAction, ITransport, WidgetApiAction } from 'matrix-widget-api';
|
||||||
import { WidgetApiToWidgetAction, ITransport, AnyWidgetAction } from 'matrix-widget-api'; // Ensure ITransport and AnyWidgetAction are imported
|
|
||||||
|
|
||||||
// Define the shape of the context value
|
|
||||||
interface CallContextState {
|
interface CallContextState {
|
||||||
activeCallRoomId: string | null;
|
activeCallRoomId: string | null;
|
||||||
setActiveCallRoomId: (roomId: string | null) => void;
|
setActiveCallRoomId: (roomId: string | null) => void;
|
||||||
hangUp: () => void;
|
hangUp: () => void;
|
||||||
// --- New additions for API interaction ---
|
activeApiTransport: ITransport | null;
|
||||||
activeApiTransport: ITransport | null; // Hold the active transport
|
registerActiveTransport: (roomId: string | null, transport: ITransport | null) => void;
|
||||||
registerActiveTransport: (roomId: string | null, transport: ITransport | null) => void; // Function for PersistentCallContainer to register/unregister
|
|
||||||
sendWidgetAction: <T = unknown>(
|
sendWidgetAction: <T = unknown>(
|
||||||
action: WidgetApiToWidgetAction | string, // Allow standard actions or custom string actions
|
action: WidgetApiToWidgetAction | string,
|
||||||
data: T
|
data: T
|
||||||
) => Promise<void>; // Function for any component to send actions
|
) => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the context
|
|
||||||
const CallContext = createContext<CallContextState | undefined>(undefined);
|
const CallContext = createContext<CallContextState | undefined>(undefined);
|
||||||
|
|
||||||
interface CallProviderProps {
|
interface CallProviderProps {
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the Provider component
|
|
||||||
export function CallProvider({ children }: CallProviderProps) {
|
export function CallProvider({ children }: CallProviderProps) {
|
||||||
const [activeCallRoomId, setActiveCallRoomIdState] = useState<string | null>(null);
|
const [activeCallRoomId, setActiveCallRoomIdState] = useState<string | null>(null);
|
||||||
// --- New State: Hold the transport of the currently active widget API ---
|
|
||||||
const [activeApiTransport, setActiveApiTransport] = useState<ITransport | null>(null);
|
const [activeApiTransport, setActiveApiTransport] = useState<ITransport | null>(null);
|
||||||
// Store the room ID associated with the current active transport
|
|
||||||
const [transportRoomId, setTransportRoomId] = useState<string | null>(null);
|
const [transportRoomId, setTransportRoomId] = useState<string | null>(null);
|
||||||
|
|
||||||
// --- Actions ---
|
// --- Actions ---
|
||||||
|
|
@ -37,7 +30,6 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
(roomId: string | null) => {
|
(roomId: string | null) => {
|
||||||
logger.debug(`CallContext: Setting activeCallRoomId to ${roomId}`);
|
logger.debug(`CallContext: Setting activeCallRoomId to ${roomId}`);
|
||||||
setActiveCallRoomIdState(roomId);
|
setActiveCallRoomIdState(roomId);
|
||||||
// If the room being cleared is the one associated with the transport, clear the transport
|
|
||||||
if (roomId === null || roomId !== transportRoomId) {
|
if (roomId === null || roomId !== transportRoomId) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`CallContext: Clearing active transport because active room changed or was cleared.`
|
`CallContext: Clearing active transport because active room changed or was cleared.`
|
||||||
|
|
@ -47,50 +39,37 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[transportRoomId]
|
[transportRoomId]
|
||||||
); // Depends on transportRoomId to avoid clearing unnecessarily
|
);
|
||||||
|
|
||||||
const hangUp = useCallback(() => {
|
const hangUp = useCallback(() => {
|
||||||
logger.debug(`CallContext: Hang up called.`);
|
logger.debug(`CallContext: Hang up called.`);
|
||||||
setActiveCallRoomIdState(null);
|
setActiveCallRoomIdState(null);
|
||||||
// Also clear the transport on hangup
|
|
||||||
logger.debug(`CallContext: Clearing active transport due to hangup.`);
|
logger.debug(`CallContext: Clearing active transport due to hangup.`);
|
||||||
setActiveApiTransport(null);
|
setActiveApiTransport(null);
|
||||||
setTransportRoomId(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(
|
const registerActiveTransport = useCallback(
|
||||||
(roomId: string | null, transport: ITransport | null) => {
|
(roomId: string | null, transport: ITransport | null) => {
|
||||||
if (roomId && transport) {
|
if (roomId && transport) {
|
||||||
logger.debug(`CallContext: Registering active transport for room ${roomId}.`);
|
logger.debug(`CallContext: Registering active transport for room ${roomId}.`);
|
||||||
setActiveApiTransport(transport);
|
setActiveApiTransport(transport);
|
||||||
setTransportRoomId(roomId);
|
setTransportRoomId(roomId);
|
||||||
|
} else if (roomId === transportRoomId || roomId === null) {
|
||||||
|
logger.debug(`CallContext: Clearing active transport for room ${transportRoomId}.`);
|
||||||
|
setActiveApiTransport(null);
|
||||||
|
setTransportRoomId(null);
|
||||||
} else {
|
} else {
|
||||||
// Only clear if the transport being cleared belongs to the currently stored roomId
|
logger.debug(
|
||||||
// or if roomId is explicitly null (global cleanup)
|
`CallContext: Ignoring transport clear request for room ${roomId}, as current transport belongs to ${transportRoomId}.`
|
||||||
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}.`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[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(
|
const sendWidgetAction = useCallback(
|
||||||
async <T = unknown,>(
|
async <T = unknown,>(action: WidgetApiToWidgetAction | string, data: T): Promise<void> => {
|
||||||
action: WidgetApiToWidgetAction | string, // Use the imported type or allow custom strings
|
|
||||||
data: T
|
|
||||||
): Promise<void> => {
|
|
||||||
if (!activeApiTransport) {
|
if (!activeApiTransport) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`CallContext: Cannot send action '${action}', no active API transport registered.`
|
`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:`,
|
`CallContext: Sending action '${action}' via active transport (room: ${transportRoomId}) with data:`,
|
||||||
data
|
data
|
||||||
);
|
);
|
||||||
// Use the transport's send method.
|
await activeApiTransport.send<T>(action as WidgetApiAction, data);
|
||||||
// The 'action' parameter should match expected Widget API actions.
|
|
||||||
// The transport handles wrapping this in the correct message format.
|
|
||||||
await activeApiTransport.send<T>(action as AnyWidgetAction, data); // Cast action type if needed by transport method signature
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`CallContext: Error sending action '${action}':`, error);
|
logger.error(`CallContext: Error sending action '${action}':`, error);
|
||||||
return Promise.reject(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 ---
|
// --- Memoize Context Value ---
|
||||||
|
|
@ -127,8 +102,7 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
activeCallRoomId,
|
activeCallRoomId,
|
||||||
setActiveCallRoomId,
|
setActiveCallRoomId,
|
||||||
hangUp,
|
hangUp,
|
||||||
// Add new state and actions to the context value
|
activeApiTransport,
|
||||||
activeApiTransport, // Keep exposing transport if direct access is ever needed, but prefer sendWidgetAction
|
|
||||||
registerActiveTransport,
|
registerActiveTransport,
|
||||||
sendWidgetAction,
|
sendWidgetAction,
|
||||||
}),
|
}),
|
||||||
|
|
@ -142,12 +116,9 @@ export function CallProvider({ children }: CallProviderProps) {
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
// Provide the context value to children
|
|
||||||
return <CallContext.Provider value={contextValue}>{children}</CallContext.Provider>;
|
return <CallContext.Provider value={contextValue}>{children}</CallContext.Provider>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Custom Hook ---
|
|
||||||
// Remains the same, but now returns the extended context value
|
|
||||||
export function useCallState(): CallContextState {
|
export function useCallState(): CallContextState {
|
||||||
const context = useContext(CallContext);
|
const context = useContext(CallContext);
|
||||||
if (context === undefined) {
|
if (context === undefined) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue