Spaces:
Runtime error
Runtime error
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; | |