From 48c0c8342401c494d118fa8e0e2245028de8dc74 Mon Sep 17 00:00:00 2001 From: Jose Selesan Date: Wed, 28 Oct 2026 19:34:20 -0300 Subject: [PATCH] feat(web): finish a dictation after 3 seconds of silence Safari on iOS keeps listening long after the user stops talking, so the phrase only got added by pressing Enter (which added the raw transcript, skipping the AI split). Now the dictation ends 3 seconds after the last words heard and goes through the usual voice flow; Enter while listening ends it the same way instead of adding the text as typed. Co-Authored-By: Claude Opus 5.5 --- .../src/components/items/create-item-form.tsx | 9 +++- apps/web/src/hooks/use-dictation.ts | 51 ++++++++++++++----- 2 files changed, 46 insertions(+), 14 deletions(-) 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), [])