Skip to content

Commit f0cd646

Browse files
committed
Refactor react-native example for improved ergonomics.
1 parent 431ad3c commit f0cd646

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

evi-react-native-example/App.tsx

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ interface ChatEntry {
3030
content: string;
3131
}
3232

33+
type ConnectionState = "disconnected" | "connecting" | "connected";
3334

3435
// 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)
3536
const humeClientWithApiKey = () => {
@@ -52,7 +53,8 @@ const humeClientWithAccessToken = async () => {
5253
}
5354

5455
const App = () => {
55-
const [isConnected, setIsConnected] = useState(false);
56+
const [connectionState, setConnectionState] =
57+
useState<ConnectionState>("disconnected");
5658
const [isMuted, setIsMuted] = useState(false);
5759
const [chatEntries, setChatEntries] = useState<ChatEntry[]>([]);
5860
const humeRef = useRef<HumeClient | null>(null);
@@ -118,7 +120,7 @@ const App = () => {
118120
});
119121

120122
chatSocket.on("close", () => {
121-
setIsConnected(false);
123+
setConnectionState("disconnected");
122124
});
123125

124126
chatSocketRef.current = chatSocket;
@@ -147,42 +149,55 @@ const App = () => {
147149
chatSocketRef.current.close();
148150
}
149151
};
150-
152+
151153
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 () => {
162155
NativeAudio.stopRecording().catch((error: any) => {
163156
console.error("Error while stopping recording", error);
164157
});
158+
165159
if (
166160
chatSocketRef.current &&
167161
chatSocketRef.current.readyState === WebSocket.OPEN
168162
) {
169163
chatSocketRef.current?.close();
170164
}
171165
};
172-
return onUnmount;
173-
}, [isConnected]);
166+
}, []);
174167

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");
184176
}
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+
};
186201

187202
const handleInterruption = () => {
188203
NativeAudio.stopPlayback();
@@ -250,6 +265,9 @@ const App = () => {
250265
}
251266
};
252267

268+
const isConnecting = connectionState === "connecting";
269+
const isConnected = connectionState === "connected";
270+
253271
return (
254272
<View style={styles.appBackground}>
255273
<SafeAreaView style={styles.container}>
@@ -275,12 +293,19 @@ const App = () => {
275293
</ScrollView>
276294
<View style={styles.buttonContainer}>
277295
<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())}
280305
/>
281306
<Button
282307
title={isMuted ? "Unmute" : "Mute"}
283-
onPress={() => setIsMuted(!isMuted)}
308+
onPress={() => (isMuted ? unmute() : mute())}
284309
/>
285310
</View>
286311
</SafeAreaView>

0 commit comments

Comments
 (0)