Spaces:
Sleeping
Sleeping
File size: 1,426 Bytes
6337686 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
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, setInput, sendMessage])
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;
|