33 * No modifications to the service itself
44 */
55
6- import React , { createContext , useContext , ReactNode , useState , useEffect , useCallback } from 'react' ;
6+ import React , { createContext , useContext , ReactNode , useState , useCallback } from 'react' ;
77import { createChatInstance , utils } from '@chat-e2ee/service' ;
8- import { ChatContextType , ChatInstance , Message } from '../types/index' ;
8+ import type { IChatE2EE , IE2ECall } from '@chat-e2ee/service' ;
9+ import { ChatContextType , Message } from '../types/index' ;
910import { createMessage } from '../utils/messageHandling' ;
1011import { playBeep } from '../utils/audioNotification' ;
1112
1213const ChatContext = createContext < ChatContextType | undefined > ( undefined ) ;
1314
1415export const ChatProvider : React . FC < { children : ReactNode } > = ( { children } ) => {
15- const [ chat , setChat ] = useState < ChatInstance | null > ( null ) ;
16+ const [ chat , setChat ] = useState < IChatE2EE | null > ( null ) ;
1617 const [ userId , setUserId ] = useState < string > ( '' ) ;
1718 const [ channelHash , setChannelHash ] = useState < string > ( '' ) ;
1819 const [ privateKey , setPrivateKey ] = useState < string > ( '' ) ;
@@ -60,12 +61,13 @@ export const ChatProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
6061 const newUserId = ( utils as any ) . generateUUID ( ) ;
6162 setUserId ( newUserId ) ;
6263
63- await chat . setChannel ( hash , newUserId ) ;
64+ // setChannel returns void but has async operations inside
65+ chat . setChannel ( hash , newUserId ) ;
6466 setChannelHash ( hash ) ;
6567 setIsConnected ( true ) ;
6668
6769 // Setup listeners
68- setupChatListeners ( chat , newUserId ) ;
70+ setupChatListeners ( chat ) ;
6971
7072 // Check for existing users
7173 await checkExistingUsers ( chat ) ;
@@ -84,7 +86,7 @@ export const ChatProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
8486 try {
8587 const message = createMessage ( userId , text , 'sent' ) ;
8688 addMessage ( message ) ;
87- await chat . encrypt ( { text } ) . send ( ) ;
89+ await chat . encrypt ( { text, image : '' } ) . send ( ) ;
8890 } catch ( err ) {
8991 console . error ( 'Failed to send message:' , err ) ;
9092 throw err ;
@@ -108,17 +110,24 @@ export const ChatProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
108110
109111 // End call
110112 const endCall = useCallback ( async ( ) => {
111- setCallActive ( false ) ;
112- setCallDuration ( 0 ) ;
113- } , [ ] ) ;
113+ try {
114+ if ( chat ) {
115+ chat . endCall ( ) ;
116+ }
117+ setCallActive ( false ) ;
118+ setCallDuration ( 0 ) ;
119+ } catch ( err ) {
120+ console . error ( 'Failed to end call:' , err ) ;
121+ }
122+ } , [ chat ] ) ;
114123
115124 // Add message to state
116125 const addMessage = useCallback ( ( message : Message ) => {
117126 setMessages ( ( prev ) => [ ...prev , message ] ) ;
118127 } , [ ] ) ;
119128
120129 // Setup chat listeners
121- const setupChatListeners = ( chatInstance : ChatInstance , currentUserId : string ) => {
130+ const setupChatListeners = ( chatInstance : IChatE2EE ) => {
122131 chatInstance . on ( 'on-alice-join' , ( ) => {
123132 playBeep ( ) ;
124133 setIsConnected ( true ) ;
@@ -138,16 +147,17 @@ export const ChatProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
138147 }
139148 } ) ;
140149
141- chatInstance . on ( 'call-added' , ( call : any ) => {
150+ chatInstance . on ( 'call-added' , ( call : IE2ECall ) => {
142151 setCallActive ( true ) ;
143152 setCallStatus ( 'Incoming Call...' ) ;
144153 setupCallListeners ( call ) ;
145154 } ) ;
146155 } ;
147156
148157 // Setup call listeners
149- const setupCallListeners = ( call : any ) => {
150- call . on ( 'state-changed' , ( state : string ) => {
158+ const setupCallListeners = ( call : IE2ECall ) => {
159+ call . on ( 'state-changed' , async ( ) => {
160+ const state = call . state ;
151161 setCallStatus ( state . charAt ( 0 ) . toUpperCase ( ) + state . slice ( 1 ) ) ;
152162
153163 if ( state === 'closed' || state === 'failed' ) {
@@ -159,7 +169,7 @@ export const ChatProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
159169 } ;
160170
161171 // Check for existing users
162- const checkExistingUsers = async ( chatInstance : ChatInstance ) => {
172+ const checkExistingUsers = async ( chatInstance : IChatE2EE ) => {
163173 try {
164174 const users = await chatInstance . getUsersInChannel ( ) ;
165175 if ( users && users . length > 1 ) {
0 commit comments