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 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-28 19:34:20 -03:00
parent a814b34036
commit 48c0c83424
2 changed files with 46 additions and 14 deletions

View File

@@ -276,7 +276,14 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
: 'Dictar productos' : 'Dictar productos'
return ( return (
<form onSubmit={onSubmit}> <form
onSubmit={(event) => {
// Enter while dictating ends the dictation: what was heard goes through its flow.
if (!dictation.listening) return onSubmit(event)
event.preventDefault()
dictation.stop()
}}
>
<Field data-invalid={!!form.formState.errors.name}> <Field data-invalid={!!form.formState.errors.name}>
<InputGroup className="bg-card h-14 rounded-full shadow-xs dark:bg-card"> <InputGroup className="bg-card h-14 rounded-full shadow-xs dark:bg-card">
<InputGroupAddon className="pl-1.5"> <InputGroupAddon className="pl-1.5">

View File

@@ -52,17 +52,29 @@ type UseDictationOptions = {
onTranscript: (transcript: string) => void 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()` * Dictation with the Web Speech API in Argentine Spanish: one phrase per `start()`
* (`continuous = false`), with the partial transcript in `interim` while the user * (`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) { export function useDictation({ onTranscript }: UseDictationOptions) {
const [supported] = useState(() => getSpeechRecognition() !== null) const [supported] = useState(() => getSpeechRecognition() !== null)
const [listening, setListening] = useState(false) const [listening, setListening] = useState(false)
const [interim, setInterim] = useState('') const [interim, setInterim] = useState('')
const [error, setError] = useState<string | null>(null) const [error, setError] = useState<string | null>(null)
const recognitionRef = useRef<SpeechRecognitionLike | null>(null) const sessionRef = useRef<DictationSession | null>(null)
const onTranscriptRef = useRef(onTranscript) const onTranscriptRef = useRef(onTranscript)
useEffect(() => { useEffect(() => {
@@ -70,11 +82,11 @@ export function useDictation({ onTranscript }: UseDictationOptions) {
}) })
// Leaving the page stops listening, without delivering a transcript. // Leaving the page stops listening, without delivering a transcript.
useEffect(() => () => recognitionRef.current?.abort(), []) useEffect(() => () => sessionRef.current?.cancel(), [])
const start = useCallback(() => { const start = useCallback(() => {
const SpeechRecognition = getSpeechRecognition() const SpeechRecognition = getSpeechRecognition()
if (!SpeechRecognition || recognitionRef.current) return if (!SpeechRecognition || sessionRef.current) return
const recognition = new SpeechRecognition() const recognition = new SpeechRecognition()
recognition.lang = 'es-AR' recognition.lang = 'es-AR'
@@ -84,6 +96,21 @@ export function useDictation({ onTranscript }: UseDictationOptions) {
let transcript = '' let transcript = ''
let failed = false let failed = false
let done = false
let silenceTimer: ReturnType<typeof setTimeout> | 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) => { recognition.onresult = (event) => {
// The results hold the whole phrase so far: final parts plus the guess in progress. // 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('') .join('')
.trim() .trim()
setInterim(transcript) setInterim(transcript)
clearTimeout(silenceTimer)
silenceTimer = setTimeout(() => end(true), DICTATION_SILENCE_MS)
} }
recognition.onerror = (event) => { 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 // Some browsers (iOS Safari) end without marking the last result as final, so
// whatever was heard by then is the transcript. // whatever was heard by then is the transcript.
recognition.onend = () => { recognition.onend = () => end(true)
recognitionRef.current = null
setListening(false)
setInterim('')
if (transcript && !failed) onTranscriptRef.current(transcript)
}
setError(null) setError(null)
setInterim('') setInterim('')
@@ -118,12 +143,12 @@ export function useDictation({ onTranscript }: UseDictationOptions) {
return return
} }
recognitionRef.current = recognition sessionRef.current = { finish: () => end(true), cancel: () => end(false) }
setListening(true) setListening(true)
}, []) }, [])
/** Stops listening; what was heard so far is still delivered. */ /** Stops listening right away; what was heard so far is still delivered. */
const stop = useCallback(() => recognitionRef.current?.stop(), []) const stop = useCallback(() => sessionRef.current?.finish(), [])
const clearError = useCallback(() => setError(null), []) const clearError = useCallback(() => setError(null), [])