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:
78
apps/web/src/hooks/use-dictate-items.ts
Normal file
78
apps/web/src/hooks/use-dictate-items.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { useRef, useState } from 'react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
import { useAddItems } from '@/hooks/use-add-items'
|
||||
import { DICTATION_MESSAGES, useMicrophoneDictation } from '@/hooks/use-microphone-dictation'
|
||||
|
||||
type UseDictateItemsOptions = {
|
||||
/**
|
||||
* The dictation finished: `added` says whether anything made it to the list, so a
|
||||
* sheet can close on success and stay open to show why nothing was added.
|
||||
*/
|
||||
onFinish?: (added: boolean) => void
|
||||
/**
|
||||
* A message was set (an error, or a dictation the API limits rejected). A surface that
|
||||
* may already be closed uses this to show it somewhere else, e.g. as a toast.
|
||||
*/
|
||||
onMessage?: (message: string) => void
|
||||
}
|
||||
|
||||
/**
|
||||
* Dictation of items into a list, shared by the empty list's card and the floating
|
||||
* button: the microphone listens until the user stops it and the transcript goes
|
||||
* through `addDictation` (split by the API, undoable). Errors and limits stay in
|
||||
* `message` so each surface can show them where it fits.
|
||||
*/
|
||||
export function useDictateItems(
|
||||
listId: string,
|
||||
{ onFinish, onMessage }: UseDictateItemsOptions = {},
|
||||
) {
|
||||
const [message, setMessage] = useState<string | null>(null)
|
||||
// A rejected dictation is reported from a callback, so the outcome of `addDictation`
|
||||
// is read from here once it resolves.
|
||||
const rejected = useRef(false)
|
||||
|
||||
const showMessage = (detail: string) => {
|
||||
setMessage(detail)
|
||||
onMessage?.(detail)
|
||||
}
|
||||
|
||||
const { addDictation, processing } = useAddItems(listId, {
|
||||
onItemFailed: (source, detail) => toast.error(`No pudimos añadir «${source}»: ${detail}`),
|
||||
onDictationRejected: (_, detail) => {
|
||||
rejected.current = true
|
||||
showMessage(detail)
|
||||
},
|
||||
})
|
||||
|
||||
const dictation = useMicrophoneDictation({
|
||||
onPhrases: async (phrases) => {
|
||||
rejected.current = false
|
||||
const understood = await addDictation(phrases.join('\n'))
|
||||
if (!rejected.current && understood === 0) showMessage(DICTATION_MESSAGES.empty)
|
||||
onFinish?.(understood > 0 && !rejected.current)
|
||||
},
|
||||
onError: (detail) => {
|
||||
showMessage(detail)
|
||||
onFinish?.(false)
|
||||
},
|
||||
})
|
||||
|
||||
const listening = dictation.status === 'listening'
|
||||
|
||||
return {
|
||||
listening,
|
||||
/** Asking for the microphone or splitting the transcript: no new dictation yet. */
|
||||
busy: dictation.status === 'requesting' || processing,
|
||||
processing,
|
||||
supported: dictation.supported,
|
||||
/** What has been heard so far, shown while listening. */
|
||||
heard: dictation.heard,
|
||||
message,
|
||||
start: () => {
|
||||
setMessage(null)
|
||||
dictation.start()
|
||||
},
|
||||
stop: dictation.stop,
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
|
||||
import {
|
||||
canDictate,
|
||||
getSpeechRecognition as getBrowserSpeechRecognition,
|
||||
isIosStandalone,
|
||||
type SpeechRecognitionConstructor,
|
||||
} from '@/lib/speech-recognition'
|
||||
|
||||
/** Errors that only mean "nothing was said" or "we stopped it": not worth telling. */
|
||||
@@ -21,10 +20,8 @@ const ERROR_MESSAGES: Record<string, string> = {
|
||||
* 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' || isIosStandalone()) return null
|
||||
|
||||
return getBrowserSpeechRecognition()
|
||||
function getSpeechRecognition() {
|
||||
return canDictate() ? getBrowserSpeechRecognition() : null
|
||||
}
|
||||
|
||||
type UseDictationOptions = {
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
|
||||
import { splitDictation } from '@/lib/natural-input/dictation'
|
||||
import { getSpeechRecognition, type SpeechRecognitionLike } from '@/lib/speech-recognition'
|
||||
import {
|
||||
canDictate,
|
||||
getSpeechRecognition,
|
||||
type SpeechRecognitionLike,
|
||||
} from '@/lib/speech-recognition'
|
||||
|
||||
export const DICTATION_MESSAGES = {
|
||||
denied: 'No tenemos permiso para usar el micrófono. Habilítalo en los ajustes del navegador.',
|
||||
@@ -51,6 +55,7 @@ type UseMicrophoneDictationOptions = {
|
||||
* capture. Unmounting aborts the recognition.
|
||||
*/
|
||||
export function useMicrophoneDictation({ onPhrases, onError }: UseMicrophoneDictationOptions) {
|
||||
const [supported] = useState(() => canDictate())
|
||||
const [status, setStatus] = useState<DictationStatus>('idle')
|
||||
// What has been heard so far, shown while listening.
|
||||
const [heard, setHeard] = useState('')
|
||||
@@ -67,7 +72,7 @@ export function useMicrophoneDictation({ onPhrases, onError }: UseMicrophoneDict
|
||||
const start = useCallback(() => {
|
||||
if (endRef.current) return
|
||||
|
||||
const SpeechRecognition = getSpeechRecognition()
|
||||
const SpeechRecognition = canDictate() ? getSpeechRecognition() : null
|
||||
if (!SpeechRecognition) {
|
||||
callbacksRef.current.onError(DICTATION_MESSAGES.unsupported)
|
||||
return
|
||||
@@ -150,5 +155,5 @@ export function useMicrophoneDictation({ onPhrases, onError }: UseMicrophoneDict
|
||||
/** Stops listening; what was heard so far is delivered. */
|
||||
const stop = useCallback(() => endRef.current?.(true), [])
|
||||
|
||||
return { status, heard, start, stop }
|
||||
return { status, heard, supported, start, stop }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user