Skip to content

Refactor react-native example for improved ergonomics #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 53 additions & 28 deletions evi-react-native-example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface ChatEntry {
content: string;
}

type ConnectionState = "disconnected" | "connecting" | "connected";

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

const App = () => {
const [isConnected, setIsConnected] = useState(false);
const [connectionState, setConnectionState] =
useState<ConnectionState>("disconnected");
const [isMuted, setIsMuted] = useState(false);
const [chatEntries, setChatEntries] = useState<ChatEntry[]>([]);
const humeRef = useRef<HumeClient | null>(null);
Expand Down Expand Up @@ -118,7 +120,7 @@ const App = () => {
});

chatSocket.on("close", () => {
setIsConnected(false);
setConnectionState("disconnected");
});

chatSocketRef.current = chatSocket;
Expand Down Expand Up @@ -147,42 +149,55 @@ const App = () => {
chatSocketRef.current.close();
}
};

useEffect(() => {
if (isConnected) {
handleConnect().catch((error) => {
console.error("Error while connecting:", error);
});
} else {
handleDisconnect().catch((error) => {
console.error("Error while disconnecting:", error);
});
}
const onUnmount = () => {
return () => {
NativeAudio.stopRecording().catch((error: any) => {
console.error("Error while stopping recording", error);
});

if (
chatSocketRef.current &&
chatSocketRef.current.readyState === WebSocket.OPEN
) {
chatSocketRef.current?.close();
}
};
return onUnmount;
}, [isConnected]);
}, []);

useEffect(() => {
if (isMuted) {
NativeAudio.mute().catch((error) => {
console.error("Error while muting", error);
});
} else {
NativeAudio.unmute().catch((error) => {
console.error("Error while unmuting", error);
});
const connect = async () => {
setConnectionState("connecting");
try {
await handleConnect();
setConnectionState("connected");
} catch (error) {
console.error("Error while connecting:", error);
setConnectionState("disconnected");
}
}, [isMuted]);
};

const disconnect = async () => {
try {
await handleDisconnect();
setConnectionState("disconnected");
} catch (error) {
console.error("Error while disconnecting:", error);
}
};

const mute = () => {
setIsMuted(true);
NativeAudio.mute().catch((error) => {
console.error("Error while muting", error);
});
};

const unmute = () => {
setIsMuted(false);
NativeAudio.unmute().catch((error) => {
console.error("Error while unmuting", error);
});
};

const handleInterruption = () => {
NativeAudio.stopPlayback();
Expand Down Expand Up @@ -250,6 +265,9 @@ const App = () => {
}
};

const isConnecting = connectionState === "connecting";
const isConnected = connectionState === "connected";

return (
<View style={styles.appBackground}>
<SafeAreaView style={styles.container}>
Expand All @@ -275,12 +293,19 @@ const App = () => {
</ScrollView>
<View style={styles.buttonContainer}>
<Button
title={isConnected ? "Disconnect" : "Connect"}
onPress={() => setIsConnected(!isConnected)}
disabled={isConnecting}
title={
isConnecting
? "Connecting..."
: isConnected
? "Disconnect"
: "Connect"
}
onPress={() => (isConnected ? disconnect() : connect())}
/>
<Button
title={isMuted ? "Unmute" : "Mute"}
onPress={() => setIsMuted(!isMuted)}
onPress={() => (isMuted ? unmute() : mute())}
/>
</View>
</SafeAreaView>
Expand Down