@@ -30,6 +30,7 @@ interface ChatEntry {
30
30
content : string ;
31
31
}
32
32
33
+ type ConnectionState = "disconnected" | "connecting" | "connected" ;
33
34
34
35
// WARNING! For development only. In production, the app should hit your own backend server to get an access token, using "token authentication" (see https://dev.hume.ai/docs/introduction/api-key#token-authentication)
35
36
const humeClientWithApiKey = ( ) => {
@@ -52,7 +53,8 @@ const humeClientWithAccessToken = async () => {
52
53
}
53
54
54
55
const App = ( ) => {
55
- const [ isConnected , setIsConnected ] = useState ( false ) ;
56
+ const [ connectionState , setConnectionState ] =
57
+ useState < ConnectionState > ( "disconnected" ) ;
56
58
const [ isMuted , setIsMuted ] = useState ( false ) ;
57
59
const [ chatEntries , setChatEntries ] = useState < ChatEntry [ ] > ( [ ] ) ;
58
60
const humeRef = useRef < HumeClient | null > ( null ) ;
@@ -118,7 +120,7 @@ const App = () => {
118
120
} ) ;
119
121
120
122
chatSocket . on ( "close" , ( ) => {
121
- setIsConnected ( false ) ;
123
+ setConnectionState ( "disconnected" ) ;
122
124
} ) ;
123
125
124
126
chatSocketRef . current = chatSocket ;
@@ -147,42 +149,55 @@ const App = () => {
147
149
chatSocketRef . current . close ( ) ;
148
150
}
149
151
} ;
150
-
152
+
151
153
useEffect ( ( ) => {
152
- if ( isConnected ) {
153
- handleConnect ( ) . catch ( ( error ) => {
154
- console . error ( "Error while connecting:" , error ) ;
155
- } ) ;
156
- } else {
157
- handleDisconnect ( ) . catch ( ( error ) => {
158
- console . error ( "Error while disconnecting:" , error ) ;
159
- } ) ;
160
- }
161
- const onUnmount = ( ) => {
154
+ return ( ) => {
162
155
NativeAudio . stopRecording ( ) . catch ( ( error : any ) => {
163
156
console . error ( "Error while stopping recording" , error ) ;
164
157
} ) ;
158
+
165
159
if (
166
160
chatSocketRef . current &&
167
161
chatSocketRef . current . readyState === WebSocket . OPEN
168
162
) {
169
163
chatSocketRef . current ?. close ( ) ;
170
164
}
171
165
} ;
172
- return onUnmount ;
173
- } , [ isConnected ] ) ;
166
+ } , [ ] ) ;
174
167
175
- useEffect ( ( ) => {
176
- if ( isMuted ) {
177
- NativeAudio . mute ( ) . catch ( ( error ) => {
178
- console . error ( "Error while muting" , error ) ;
179
- } ) ;
180
- } else {
181
- NativeAudio . unmute ( ) . catch ( ( error ) => {
182
- console . error ( "Error while unmuting" , error ) ;
183
- } ) ;
168
+ const connect = async ( ) => {
169
+ setConnectionState ( "connecting" ) ;
170
+ try {
171
+ await handleConnect ( ) ;
172
+ setConnectionState ( "connected" ) ;
173
+ } catch ( error ) {
174
+ console . error ( "Error while connecting:" , error ) ;
175
+ setConnectionState ( "disconnected" ) ;
184
176
}
185
- } , [ isMuted ] ) ;
177
+ } ;
178
+
179
+ const disconnect = async ( ) => {
180
+ try {
181
+ await handleDisconnect ( ) ;
182
+ setConnectionState ( "disconnected" ) ;
183
+ } catch ( error ) {
184
+ console . error ( "Error while disconnecting:" , error ) ;
185
+ }
186
+ } ;
187
+
188
+ const mute = ( ) => {
189
+ setIsMuted ( true ) ;
190
+ NativeAudio . mute ( ) . catch ( ( error ) => {
191
+ console . error ( "Error while muting" , error ) ;
192
+ } ) ;
193
+ } ;
194
+
195
+ const unmute = ( ) => {
196
+ setIsMuted ( false ) ;
197
+ NativeAudio . unmute ( ) . catch ( ( error ) => {
198
+ console . error ( "Error while unmuting" , error ) ;
199
+ } ) ;
200
+ } ;
186
201
187
202
const handleInterruption = ( ) => {
188
203
NativeAudio . stopPlayback ( ) ;
@@ -250,6 +265,9 @@ const App = () => {
250
265
}
251
266
} ;
252
267
268
+ const isConnecting = connectionState === "connecting" ;
269
+ const isConnected = connectionState === "connected" ;
270
+
253
271
return (
254
272
< View style = { styles . appBackground } >
255
273
< SafeAreaView style = { styles . container } >
@@ -275,12 +293,19 @@ const App = () => {
275
293
</ ScrollView >
276
294
< View style = { styles . buttonContainer } >
277
295
< Button
278
- title = { isConnected ? "Disconnect" : "Connect" }
279
- onPress = { ( ) => setIsConnected ( ! isConnected ) }
296
+ disabled = { isConnecting }
297
+ title = {
298
+ isConnecting
299
+ ? "Connecting..."
300
+ : isConnected
301
+ ? "Disconnect"
302
+ : "Connect"
303
+ }
304
+ onPress = { ( ) => ( isConnected ? disconnect ( ) : connect ( ) ) }
280
305
/>
281
306
< Button
282
307
title = { isMuted ? "Unmute" : "Mute" }
283
- onPress = { ( ) => setIsMuted ( ! isMuted ) }
308
+ onPress = { ( ) => ( isMuted ? unmute ( ) : mute ( ) ) }
284
309
/>
285
310
</ View >
286
311
</ SafeAreaView >
0 commit comments