Files
superlist/apps/web/src/components/layout/list-switcher.tsx
Jose Selesan a814b34036 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>
2026-10-28 19:25:10 -03:00

96 lines
3.4 KiB
TypeScript

import type { ShoppingListSummary } from '@superlist/shared'
import { useQuery } from '@tanstack/react-query'
import { Link } from '@tanstack/react-router'
import { CheckIcon, ChevronDownIcon, ListIcon, StarIcon } from 'lucide-react'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import { itemsQueryOptions } from '@/hooks/use-items'
import { useLists } from '@/hooks/use-lists'
import { useToday } from '@/hooks/use-today'
import { splitItems } from '@/lib/items'
type ListSwitcherProps = {
list: Pick<ShoppingListSummary, 'id' | 'name'>
}
/**
* The open list's name with a "▾" that opens the user's lists (the same ones the
* "Listas" tab shows), plus "X pendientes · Y comprados" below it. The primary list
* opens on the home page, the others on their own page.
*/
export function ListSwitcher({ list }: ListSwitcherProps) {
const { data: lists } = useLists()
return (
<div className="flex min-w-0 flex-col">
<DropdownMenu>
<h1 className="flex min-w-0">
<DropdownMenuTrigger className="focus-visible:ring-ring/50 -ml-1 flex min-h-11 min-w-0 items-center gap-1 rounded-lg px-1 text-left text-xl leading-tight font-bold outline-none focus-visible:ring-3">
<span className="truncate">{list.name}</span>
<ChevronDownIcon className="size-5 shrink-0" aria-hidden />
<span className="sr-only">: cambiar de lista</span>
</DropdownMenuTrigger>
</h1>
<DropdownMenuContent align="start" className="w-64">
<DropdownMenuLabel>Mis listas</DropdownMenuLabel>
{lists?.map((option) => (
<DropdownMenuItem key={option.id} asChild className="min-h-11">
{option.isPrimary ? (
<Link to="/">
<ListOption list={option} current={option.id === list.id} />
</Link>
) : (
<Link to="/lists/$listId" params={{ listId: option.id }}>
<ListOption list={option} current={option.id === list.id} />
</Link>
)}
</DropdownMenuItem>
))}
<DropdownMenuSeparator />
<DropdownMenuItem asChild className="min-h-11">
<Link to="/lists">
<ListIcon />
Ver todas las listas
</Link>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
<ListCounts listId={list.id} />
</div>
)
}
function ListOption({ list, current }: { list: ShoppingListSummary; current: boolean }) {
return (
<>
<span className="min-w-0 flex-1 truncate">{list.name}</span>
{list.isPrimary && <StarIcon aria-label="Principal" className="text-muted-foreground" />}
{current && <CheckIcon aria-label="Lista actual" className="text-primary" />}
</>
)
}
/** "6 pendientes · 2 comprados", from the items query the page already loads. */
function ListCounts({ listId }: { listId: string }) {
const { data: items } = useQuery(itemsQueryOptions(listId))
const today = useToday()
if (!items) return null
const { pending, boughtToday } = splitItems(items, today)
return (
<p className="text-muted-foreground truncate text-sm leading-tight">
{pending.length} {pending.length === 1 ? 'pendiente' : 'pendientes'} · {boughtToday.length}{' '}
{boughtToday.length === 1 ? 'comprado' : 'comprados'}
</p>
)
}