diff --git a/apps/web/src/components/items/create-item-form.tsx b/apps/web/src/components/items/create-item-form.tsx index 0d83d49..78d9890 100644 --- a/apps/web/src/components/items/create-item-form.tsx +++ b/apps/web/src/components/items/create-item-form.tsx @@ -276,7 +276,14 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp : 'Dictar productos' return ( -
+ { + // Enter while dictating ends the dictation: what was heard goes through its flow. + if (!dictation.listening) return onSubmit(event) + event.preventDefault() + dictation.stop() + }} + > diff --git a/apps/web/src/hooks/use-dictation.ts b/apps/web/src/hooks/use-dictation.ts index ce36aba..e5cb597 100644 --- a/apps/web/src/hooks/use-dictation.ts +++ b/apps/web/src/hooks/use-dictation.ts @@ -52,17 +52,29 @@ type UseDictationOptions = { onTranscript: (transcript: string) => void } +/** + * Quiet time after the last words heard that ends the dictation. Safari on iOS keeps + * listening long after the user stops talking even with `continuous = false`, so the + * phrase is delivered once nothing new has been heard for this long. + */ +export const DICTATION_SILENCE_MS = 3000 + +/** A recognition in progress: `finish` delivers what was heard, `cancel` drops it. */ +type DictationSession = { finish: () => void; cancel: () => void } + /** * Dictation with the Web Speech API in Argentine Spanish: one phrase per `start()` * (`continuous = false`), with the partial transcript in `interim` while the user - * talks. `no-speech` and `aborted` are ignored; any other error is in `error`. + * talks. The phrase is delivered when the browser ends the recognition, when the user + * stops it, or after `DICTATION_SILENCE_MS` without new words, whichever comes first. + * `no-speech` and `aborted` are ignored; any other error is in `error`. */ export function useDictation({ onTranscript }: UseDictationOptions) { const [supported] = useState(() => getSpeechRecognition() !== null) const [listening, setListening] = useState(false) const [interim, setInterim] = useState('') const [error, setError] = useState(null) - const recognitionRef = useRef(null) + const sessionRef = useRef(null) const onTranscriptRef = useRef(onTranscript) useEffect(() => { @@ -70,11 +82,11 @@ export function useDictation({ onTranscript }: UseDictationOptions) { }) // Leaving the page stops listening, without delivering a transcript. - useEffect(() => () => recognitionRef.current?.abort(), []) + useEffect(() => () => sessionRef.current?.cancel(), []) const start = useCallback(() => { const SpeechRecognition = getSpeechRecognition() - if (!SpeechRecognition || recognitionRef.current) return + if (!SpeechRecognition || sessionRef.current) return const recognition = new SpeechRecognition() recognition.lang = 'es-AR' @@ -84,6 +96,21 @@ export function useDictation({ onTranscript }: UseDictationOptions) { let transcript = '' let failed = false + let done = false + let silenceTimer: ReturnType | undefined + + // Runs once, whatever ends the dictation first; aborting afterwards only fires an + // ignored `aborted` error and an `onend` that finds the session already done. + const end = (deliver: boolean) => { + if (done) return + done = true + clearTimeout(silenceTimer) + sessionRef.current = null + setListening(false) + setInterim('') + recognition.abort() + if (deliver && transcript && !failed) onTranscriptRef.current(transcript) + } recognition.onresult = (event) => { // The results hold the whole phrase so far: final parts plus the guess in progress. @@ -91,6 +118,9 @@ export function useDictation({ onTranscript }: UseDictationOptions) { .join('') .trim() setInterim(transcript) + + clearTimeout(silenceTimer) + silenceTimer = setTimeout(() => end(true), DICTATION_SILENCE_MS) } recognition.onerror = (event) => { @@ -101,12 +131,7 @@ export function useDictation({ onTranscript }: UseDictationOptions) { // Some browsers (iOS Safari) end without marking the last result as final, so // whatever was heard by then is the transcript. - recognition.onend = () => { - recognitionRef.current = null - setListening(false) - setInterim('') - if (transcript && !failed) onTranscriptRef.current(transcript) - } + recognition.onend = () => end(true) setError(null) setInterim('') @@ -118,12 +143,12 @@ export function useDictation({ onTranscript }: UseDictationOptions) { return } - recognitionRef.current = recognition + sessionRef.current = { finish: () => end(true), cancel: () => end(false) } setListening(true) }, []) - /** Stops listening; what was heard so far is still delivered. */ - const stop = useCallback(() => recognitionRef.current?.stop(), []) + /** Stops listening right away; what was heard so far is still delivered. */ + const stop = useCallback(() => sessionRef.current?.finish(), []) const clearError = useCallback(() => setError(null), [])