feat(web): dictate items from a floating button on lists with items

An empty list dictates from its central card; once it has items that card is
gone, so the same flow now has a floating microphone in the bottom-right corner
that opens a bottom sheet and starts listening in the same tap.

- use-dictate-items extracts the dictation wiring shared by the card and the
  sheet (split by the API, merged with pending items, undoable toast).
- DictationFeedback keeps the live state (listening, phrases heard, errors) the
  same in both surfaces.
- Closing the sheet ends the dictation like "Detener": what was heard is added.
- canDictate() centralises the "an iOS home-screen app cannot dictate" rule; the
  sheet offers "Escribir a mano" there and focuses the composer input.
This commit is contained in:
Jose Selesan
2026-10-29 15:16:47 -03:00
parent aa170de8d1
commit eace15b140
8 changed files with 319 additions and 56 deletions

View File

@@ -1,10 +1,8 @@
import { Loader2Icon, MicIcon, SquareIcon } from 'lucide-react'
import { useState } from 'react'
import { toast } from 'sonner'
import { DictationFeedback } from '@/components/items/dictation-feedback'
import { Button } from '@/components/ui/button'
import { useAddItems } from '@/hooks/use-add-items'
import { DICTATION_MESSAGES, useMicrophoneDictation } from '@/hooks/use-microphone-dictation'
import { useDictateItems } from '@/hooks/use-dictate-items'
type DictateItemsButtonProps = {
listId: string
@@ -15,30 +13,13 @@ type DictateItemsButtonProps = {
* said through the same flow as the input's microphone (split by the API, undoable).
*/
export function DictateItemsButton({ listId }: DictateItemsButtonProps) {
const [message, setMessage] = useState<string | null>(null)
const { addDictation, processing } = useAddItems(listId, {
onItemFailed: (source, detail) => toast.error(`No pudimos añadir «${source}»: ${detail}`),
onDictationRejected: (_, detail) => setMessage(detail),
})
const dictation = useMicrophoneDictation({
onPhrases: async (phrases) => {
const understood = await addDictation(phrases.join('\n'))
if (understood === 0) setMessage(DICTATION_MESSAGES.empty)
},
onError: setMessage,
})
const listening = dictation.status === 'listening'
const busy = dictation.status === 'requesting' || processing
const { listening, busy, processing, heard, message, start, stop } = useDictateItems(listId)
const label = processing
? 'Procesando el dictado'
: dictation.status === 'requesting'
? 'Pidiendo acceso al micrófono'
: listening
? 'Detener dictado'
: 'Dictar ítems'
: listening
? 'Detener dictado'
: 'Dictar ítems'
return (
<div className="flex flex-col items-center gap-2">
@@ -50,9 +31,8 @@ export function DictateItemsButton({ listId }: DictateItemsButtonProps) {
disabled={busy}
className="relative rounded-full"
onClick={() => {
setMessage(null)
if (listening) dictation.stop()
else dictation.start()
if (listening) stop()
else start()
}}
>
{busy ? (
@@ -65,24 +45,7 @@ export function DictateItemsButton({ listId }: DictateItemsButtonProps) {
{listening ? 'Detener dictado' : 'Dictar ítems'}
</Button>
<div aria-live="polite" className="flex flex-col items-center gap-1 text-sm">
{listening && (
<p className="text-primary flex items-center gap-2 font-medium">
<span className="relative flex size-2.5" aria-hidden="true">
<span className="bg-primary absolute inline-flex size-full rounded-full opacity-75 motion-safe:animate-ping" />
<span className="bg-primary relative inline-flex size-2.5 rounded-full" />
</span>
Micrófono activo, te escuchamos
</p>
)}
{listening && dictation.heard && (
<p className="text-muted-foreground max-w-xs">{dictation.heard}</p>
)}
{listening && !dictation.heard && (
<p className="text-muted-foreground">Di «nuevo ítem» o haz una pausa entre artículos.</p>
)}
{message && !listening && <p className="text-destructive max-w-xs">{message}</p>}
</div>
<DictationFeedback listening={listening} heard={heard} message={message} />
</div>
)
}