import { useEffect, type ReactElement } from 'react' import { useDictation } from '@jaspers-ai/sdk' // The microphone under the message box. A press starts listening, the next stops it and hands the // words to the box to edit and send; Escape drops what was said. The app records and transcribes // with the user's voice provider, so this view gets the words and never the audio. export function Mic({ onText }: { onText: (text: string) => void }): ReactElement { const dictation = useDictation() const { state, error } = dictation useEffect(() => { if (state !== 'listening') return const onKey = (e: KeyboardEvent): void => { if (e.key === 'Escape') void dictation.cancel() } window.addEventListener('keydown', onKey) return () => window.removeEventListener('keydown', onKey) }, [state, dictation]) const press = (): void => { if (state === 'idle') void dictation.start() else if (state === 'listening') void dictation.stop().then((text) => text.trim() && onText(text.trim())) } const label = state === 'listening' ? 'Stop and put what you said in the box (Escape drops it)' : state === 'transcribing' ? 'Writing down what you said' : 'Talk to the agent' return ( {state === 'listening' && Listening…} {state === 'transcribing' && Writing it down…} {error && state === 'idle' && {error}} ) }