bingo / src /components /voice.tsx
nier111's picture
Duplicate from hf4all/bingo
fbad01d
raw
history blame contribute delete
No virus
1.4 kB
import React, { useEffect } from 'react'
import { useSetAtom } from 'jotai'
import { useBing } from '@/lib/hooks/use-bing'
import Image from 'next/image'
import VoiceIcon from '@/assets/images/voice.svg'
import VoiceButton from './ui/voice'
import { SR } from '@/lib/bots/bing/sr'
import { voiceListenAtom } from '@/state'
const sr = new SR(['ๅ‘้€', 'ๆธ…็ฉบ', '้€€ๅ‡บ'])
const Voice = ({ setInput, input, sendMessage, isSpeaking }: Pick<ReturnType<typeof useBing>, 'setInput' | 'sendMessage' | 'input' | 'isSpeaking'>) => {
const setListen = useSetAtom(voiceListenAtom)
useEffect(() => {
if (sr.listening) return
sr.transcript = !isSpeaking
}, [isSpeaking])
useEffect(() => {
sr.onchange = (msg: string, command?: string) => {
switch (command) {
case '้€€ๅ‡บ':
sr.stop()
break;
case 'ๅ‘้€':
sendMessage(input)
case 'ๆธ…็ฉบ':
setInput('')
break;
default:
setInput(input + msg)
}
}
}, [input])
const switchSR = (enable: boolean = false) => {
setListen(enable)
if (enable) {
sr.start()
} else {
sr.stop()
}
}
return sr.listening ? (
<VoiceButton onClick={() => switchSR(false)} />
) : (
<Image alt="start voice" src={VoiceIcon} width={24} className="-mt-0.5" onClick={() => switchSR(true)} />
)
};
export default Voice;