Last active
July 29, 2021 20:57
-
-
Save lmcarreiro/eb92dd4279f1f2eda5b54e33e63e569c to your computer and use it in GitHub Desktop.
STT+VAR article - index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import ReactDOM from "react-dom"; | |
import useSpeechToText from "./utils/hooks/useSpeechToText"; | |
ReactDOM.render( | |
<React.StrictMode> | |
<TestApp /> | |
</React.StrictMode>, | |
document.getElementById("root"), | |
); | |
function TestApp() { | |
const [muted, setMuted] = React.useState(true); | |
const [messages, setMessages] = React.useState<{ text: string; isFinal: boolean }[]>([]); | |
const showMessage = React.useCallback((newMessage: { text: string; isFinal: boolean }) => { | |
setMessages(messages => { | |
const previousMessages = messages.slice(0, -1); | |
const [lastMessage] = messages.slice(-1); | |
return lastMessage?.isFinal | |
? [...previousMessages, lastMessage, newMessage] | |
: [...previousMessages, newMessage]; | |
}); | |
}, []); | |
useSpeechToText(true, muted, showMessage); | |
return ( | |
<div> | |
<button onClick={() => setMuted(m => !m)}>{muted ? "Unmute" : "Mute"}</button> | |
<ul> | |
{messages.map((m, i) => ( | |
<li key={i} style={{ color: m.isFinal ? "black" : "red" }}> | |
{m.text} | |
</li> | |
))} | |
</ul> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment