feat(web): add text dictated with the iOS keyboard after 3 seconds
The installed iOS app has no speech recognition, so people dictate with the keyboard's microphone, which only writes text into the input and never ended it. Text that arrives without key presses (words or phrases at once, or replacements) is now treated as a dictation: 3 seconds after the last change it goes through the voice flow (AI split, "Deshacer"). Typing a key cancels it; pastes and deletions never trigger it. The form field is no longer called `name`, which made iOS offer to autofill a contact into the add field. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -22,6 +22,7 @@ import {
|
||||
useRemoveItems,
|
||||
useUpdateItem,
|
||||
} from '@/hooks/use-items'
|
||||
import { useKeyboardDictation } from '@/hooks/use-keyboard-dictation'
|
||||
import { useVoiceSplit } from '@/hooks/use-voice-split'
|
||||
import { ApiError } from '@/lib/api-client'
|
||||
import { findDuplicate, mergeBatch, mergeInto } from '@/lib/natural-input/merge'
|
||||
@@ -31,7 +32,8 @@ import { cn } from '@/lib/utils'
|
||||
// The input may hold several items ("leche, pan y 2 pascualinas" or a pasted list);
|
||||
// each one is checked against the API limits once the text is read.
|
||||
const createItemSchema = z.object({
|
||||
name: z.string().trim().min(1, 'Escribe un artículo').max(2000, 'Máximo 2000 caracteres'),
|
||||
// Not called `name`: iOS offers to autofill a contact into an input named that way.
|
||||
entry: z.string().trim().min(1, 'Escribe un artículo').max(2000, 'Máximo 2000 caracteres'),
|
||||
})
|
||||
|
||||
type CreateItemValues = z.infer<typeof createItemSchema>
|
||||
@@ -104,10 +106,10 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
|
||||
const form = useForm<CreateItemValues>({
|
||||
resolver: zodResolver(createItemSchema),
|
||||
defaultValues: { name: '' },
|
||||
defaultValues: { entry: '' },
|
||||
})
|
||||
|
||||
const text = useWatch({ control: form.control, name: 'name' })
|
||||
const text = useWatch({ control: form.control, name: 'entry' })
|
||||
const literal = literalText !== null && literalText === text
|
||||
// Placeholders have no server id yet, so they cannot be added to.
|
||||
const existing = (items ?? []).filter((item) => !isOptimisticItem(item))
|
||||
@@ -116,12 +118,20 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
existingRef.current = existing
|
||||
|
||||
const dictation = useDictation({ onTranscript: (transcript) => void addDictation(transcript) })
|
||||
// The keyboard's own microphone (the only one in the installed iOS app) just writes
|
||||
// text: once it has been quiet for a few seconds, that text is added as a dictation.
|
||||
const keyboardDictation = useKeyboardDictation({
|
||||
onIdle: () => {
|
||||
const dictated = form.getValues('entry').trim()
|
||||
if (dictated && !dictation.listening && !voiceSplit.isPending) void addDictation(dictated)
|
||||
},
|
||||
})
|
||||
const processing = voiceSplit.isPending
|
||||
const dictating = dictation.listening || processing
|
||||
|
||||
// The partial transcript is shown in the input while the user talks.
|
||||
useEffect(() => {
|
||||
if (dictation.listening) form.setValue('name', dictation.interim)
|
||||
if (dictation.listening) form.setValue('entry', dictation.interim)
|
||||
}, [dictation.listening, dictation.interim, form])
|
||||
|
||||
const preview = readItems(text, literal, existing)
|
||||
@@ -131,9 +141,9 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
(literal || !isPlainItem(preview, text) || preview.some((item) => item.duplicate))
|
||||
|
||||
const restoreText = (source: string, message: string) => {
|
||||
const current = form.getValues('name').trim()
|
||||
form.setValue('name', current ? `${current}; ${source}` : source)
|
||||
form.setError('name', { message })
|
||||
const current = form.getValues('entry').trim()
|
||||
form.setValue('entry', current ? `${current}; ${source}` : source)
|
||||
form.setError('entry', { message })
|
||||
}
|
||||
|
||||
/** Adds the items, or adds to the pending ones they repeat. A failed one comes back to the input. */
|
||||
@@ -191,17 +201,18 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
// The input clears at once (items are added optimistically) so several items can be
|
||||
// typed in a row; if a request fails its text comes back with the error.
|
||||
const onSubmit = form.handleSubmit((values) => {
|
||||
const toAdd = readItems(values.name, literal, existing)
|
||||
keyboardDictation.cancel()
|
||||
const toAdd = readItems(values.entry, literal, existing)
|
||||
|
||||
const limitError = toAdd.map(itemLimitError).find(Boolean)
|
||||
if (limitError) {
|
||||
form.setError('name', { message: limitError })
|
||||
form.setError('entry', { message: limitError })
|
||||
return
|
||||
}
|
||||
|
||||
form.reset()
|
||||
setLiteralText(null)
|
||||
form.setFocus('name')
|
||||
form.setFocus('entry')
|
||||
|
||||
addItems(toAdd)
|
||||
})
|
||||
@@ -211,6 +222,7 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
* usual parser. If the request fails, the whole transcript is added as a single item.
|
||||
*/
|
||||
const addDictation = async (transcript: string) => {
|
||||
keyboardDictation.cancel()
|
||||
form.reset()
|
||||
setLiteralText(null)
|
||||
|
||||
@@ -226,8 +238,8 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
|
||||
const limitError = toAdd.map(itemLimitError).find(Boolean)
|
||||
if (limitError) {
|
||||
form.setValue('name', transcript)
|
||||
form.setError('name', { message: limitError })
|
||||
form.setValue('entry', transcript)
|
||||
form.setError('entry', { message: limitError })
|
||||
return
|
||||
}
|
||||
|
||||
@@ -264,7 +276,7 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
.filter(Boolean)
|
||||
.join('; ')
|
||||
|
||||
form.setValue('name', input.value.slice(0, start) + lines + input.value.slice(end), {
|
||||
form.setValue('entry', input.value.slice(0, start) + lines + input.value.slice(end), {
|
||||
shouldDirty: true,
|
||||
})
|
||||
}
|
||||
@@ -284,7 +296,7 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
dictation.stop()
|
||||
}}
|
||||
>
|
||||
<Field data-invalid={!!form.formState.errors.name}>
|
||||
<Field data-invalid={!!form.formState.errors.entry}>
|
||||
<InputGroup className="bg-card h-14 rounded-full shadow-xs dark:bg-card">
|
||||
<InputGroupAddon className="pl-1.5">
|
||||
<InputGroupButton
|
||||
@@ -304,7 +316,7 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
<InputGroupInput
|
||||
id={NEW_ITEM_INPUT_ID}
|
||||
aria-label="Nuevo artículo"
|
||||
aria-invalid={!!form.formState.errors.name}
|
||||
aria-invalid={!!form.formState.errors.entry}
|
||||
aria-describedby={dictation.error ? 'dictation-error' : undefined}
|
||||
autoComplete="off"
|
||||
placeholder={
|
||||
@@ -314,7 +326,9 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
readOnly={dictating}
|
||||
className="text-base"
|
||||
onPaste={onPaste}
|
||||
{...form.register('name', { onChange: dictation.clearError })}
|
||||
onKeyDown={keyboardDictation.onKeyDown}
|
||||
onInput={keyboardDictation.onInput}
|
||||
{...form.register('entry', { onChange: dictation.clearError })}
|
||||
/>
|
||||
{dictation.supported && (
|
||||
<InputGroupAddon align="inline-end" className="pr-1.5">
|
||||
@@ -330,7 +344,11 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
dictation.listening &&
|
||||
'after:bg-primary/30 after:absolute after:inset-0 after:-z-10 after:rounded-full motion-safe:after:animate-ping',
|
||||
)}
|
||||
onClick={dictation.listening ? dictation.stop : dictation.start}
|
||||
onClick={() => {
|
||||
keyboardDictation.cancel()
|
||||
if (dictation.listening) dictation.stop()
|
||||
else dictation.start()
|
||||
}}
|
||||
>
|
||||
{processing ? (
|
||||
<Loader2Icon className="size-5 animate-spin" />
|
||||
@@ -341,7 +359,7 @@ export function CreateItemForm({ listId, autoFocus = false }: CreateItemFormProp
|
||||
</InputGroupAddon>
|
||||
)}
|
||||
</InputGroup>
|
||||
<FieldError errors={[form.formState.errors.name]} className="px-5" />
|
||||
<FieldError errors={[form.formState.errors.entry]} className="px-5" />
|
||||
{dictation.error && (
|
||||
<p id="dictation-error" className="text-muted-foreground px-5 text-sm" aria-live="polite">
|
||||
{dictation.error}
|
||||
|
||||
Reference in New Issue
Block a user