feat(items): voice dictation with Gemma 4 and a reworked list UI

Dictation: a mic button in the add field uses the Web Speech API (es-AR) and
shows the partial transcript in the input. POST /api/voice/split asks Gemma 4
(Gemini API, plain fetch) to split the sentence into one string per item,
written the way the existing parser reads typed text; each string then goes
through the usual add flow. The endpoint never fails because of the model:
missing key, timeout, 429 or invalid JSON fall back to splitting on commas,
semicolons, " y " and " e ". A toast offers "Deshacer" for the whole dictation;
if the request fails, the transcript is added as a single item.

UI: "+" instead of the search icon, quantity tags ("2 L", "×12", "1 kg"), the
list name with a list switcher and the pending/bought count in the header,
"Comprados (N)" folded by default, and a bottom sheet to edit name, quantity
(− / + and unit), note and delete. PATCH items now accepts a name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-10-28 19:25:10 -03:00
parent eb1f6cb2a9
commit a814b34036
39 changed files with 1673 additions and 288 deletions

View File

@@ -0,0 +1,131 @@
import { useCallback, useEffect, useRef, useState } from 'react'
/** The slice of the Web Speech API this hook uses (TypeScript's DOM lib does not ship it). */
type SpeechRecognitionResultLike = { isFinal: boolean; 0: { transcript: string } }
type SpeechRecognitionLike = {
lang: string
interimResults: boolean
continuous: boolean
maxAlternatives: number
onresult: ((event: { results: ArrayLike<SpeechRecognitionResultLike> }) => void) | null
onerror: ((event: { error: string }) => void) | null
onend: (() => void) | null
start: () => void
stop: () => void
abort: () => void
}
type SpeechRecognitionConstructor = new () => SpeechRecognitionLike
/** Errors that only mean "nothing was said" or "we stopped it": not worth telling. */
const IGNORED_ERRORS = new Set(['no-speech', 'aborted'])
const ERROR_MESSAGES: Record<string, string> = {
'not-allowed': 'Permite el acceso al micrófono para dictar.',
'service-not-allowed': 'El dictado no está disponible en este navegador.',
'audio-capture': 'No encontramos un micrófono.',
network: 'Sin conexión para reconocer la voz.',
}
/**
* The browser's speech recognition, or `null` when there is none. An iOS home-screen
* app exposes `webkitSpeechRecognition` but it does not work there, so it counts as
* unsupported (the keyboard's own dictation still works).
*/
function getSpeechRecognition(): SpeechRecognitionConstructor | null {
if (typeof window === 'undefined') return null
const iosStandalone = (navigator as Navigator & { standalone?: boolean }).standalone === true
if (iosStandalone) return null
const speechWindow = window as Window & {
SpeechRecognition?: SpeechRecognitionConstructor
webkitSpeechRecognition?: SpeechRecognitionConstructor
}
return speechWindow.SpeechRecognition ?? speechWindow.webkitSpeechRecognition ?? null
}
type UseDictationOptions = {
/** Called once with the whole transcript when the user stops talking. */
onTranscript: (transcript: string) => 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`.
*/
export function useDictation({ onTranscript }: UseDictationOptions) {
const [supported] = useState(() => getSpeechRecognition() !== null)
const [listening, setListening] = useState(false)
const [interim, setInterim] = useState('')
const [error, setError] = useState<string | null>(null)
const recognitionRef = useRef<SpeechRecognitionLike | null>(null)
const onTranscriptRef = useRef(onTranscript)
useEffect(() => {
onTranscriptRef.current = onTranscript
})
// Leaving the page stops listening, without delivering a transcript.
useEffect(() => () => recognitionRef.current?.abort(), [])
const start = useCallback(() => {
const SpeechRecognition = getSpeechRecognition()
if (!SpeechRecognition || recognitionRef.current) return
const recognition = new SpeechRecognition()
recognition.lang = 'es-AR'
recognition.interimResults = true
recognition.continuous = false
recognition.maxAlternatives = 1
let transcript = ''
let failed = false
recognition.onresult = (event) => {
// The results hold the whole phrase so far: final parts plus the guess in progress.
transcript = Array.from(event.results, (result) => result[0].transcript)
.join('')
.trim()
setInterim(transcript)
}
recognition.onerror = (event) => {
if (IGNORED_ERRORS.has(event.error)) return
failed = true
setError(ERROR_MESSAGES[event.error] ?? 'No pudimos reconocer la voz.')
}
// 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)
}
setError(null)
setInterim('')
try {
recognition.start()
} catch {
setError('No pudimos empezar a escuchar.')
return
}
recognitionRef.current = recognition
setListening(true)
}, [])
/** Stops listening; what was heard so far is still delivered. */
const stop = useCallback(() => recognitionRef.current?.stop(), [])
const clearError = useCallback(() => setError(null), [])
return { supported, listening, interim, error, start, stop, clearError }
}