'use client' import { XIcon } from 'lucide-react' import { useEffect, useId, useRef, useState, type KeyboardEvent, type ReactNode } from 'react' import { Button } from '@/components/ui/button' import { Popover, PopoverAnchor, PopoverContent } from '@/components/ui/Popover' import type { CampaignListOmniboxChip, CampaignListOmniboxSuggestion, } from '@/lib/campaignListOmnibox' import { cn } from '@/lib/utils' export type CampaignListOmniboxProps = { id?: string label: string placeholder: string chips: readonly CampaignListOmniboxChip[] suggestions: readonly CampaignListOmniboxSuggestion[] query: string onQueryChange: (query: string) => void onSelectSuggestion: (suggestionId: string) => void onRemoveChip: (chipId: string) => void /** Free-text commit (Enter with no arrow selection / default). Domain owns id scheme. */ onCommitQuery?: (query: string) => void onClearAll?: () => void isPending?: boolean /** Domain controls beside the bar (e.g. save bookmark). */ trailing?: ReactNode } /** * Shared list omnibox chassis (B127): chips inside the field, caret to the * right, grouped suggestions from the domain adapter. Navigation / URL policy * stay in the caller. * * Keyboard contract mirrors RelationChipCell (ARIA combobox by hand): arrows * move the active option, Enter picks it, Escape closes. cmdk is not used — * its Root expects an inner CommandInput and would leave this field mouse-only. */ export const CampaignListOmnibox = ({ id: idProp, label, placeholder, chips, suggestions, query, onQueryChange, onSelectSuggestion, onRemoveChip, onCommitQuery, onClearAll, isPending = false, trailing, }: CampaignListOmniboxProps) => { const reactId = useId() const id = idProp ?? `campaign-list-omnibox-${reactId}` const listboxId = `${id}-suggestions` const optionId = (index: number) => `${id}-option-${index}` const inputRef = useRef(null) const [open, setOpen] = useState(false) const [activeChipId, setActiveChipId] = useState(null) const [activeIndex, setActiveIndex] = useState(0) const hasChips = chips.length > 0 const showSuggestions = open && suggestions.length > 0 const safeActiveIndex = suggestions.length === 0 ? 0 : Math.min(activeIndex, suggestions.length - 1) const activeOptionId = showSuggestions ? optionId(safeActiveIndex) : undefined useEffect(() => { if (!open) setActiveChipId(null) }, [open]) useEffect(() => { setActiveIndex(0) }, [query]) const selectSuggestion = (suggestionId: string) => { onSelectSuggestion(suggestionId) onQueryChange('') setOpen(false) setActiveIndex(0) inputRef.current?.focus() } const onKeyDown = (event: KeyboardEvent) => { if (event.key === 'ArrowDown' || event.key === 'ArrowUp') { if (suggestions.length === 0) return event.preventDefault() setOpen(true) const delta = event.key === 'ArrowDown' ? 1 : -1 setActiveIndex((current) => (current + delta + suggestions.length) % suggestions.length) return } if (event.key === 'Home' || event.key === 'End') { if (suggestions.length === 0) return event.preventDefault() setOpen(true) setActiveIndex(event.key === 'Home' ? 0 : suggestions.length - 1) return } if (event.key === 'Escape') { if (open) { event.preventDefault() setOpen(false) } return } if (event.key === 'Backspace' && query.length === 0 && chips.length > 0) { event.preventDefault() if (activeChipId) { onRemoveChip(activeChipId) setActiveChipId(null) return } const last = chips[chips.length - 1] if (last) setActiveChipId(last.id) return } if (event.key === 'Enter') { const trimmed = query.trim() if (showSuggestions && suggestions[safeActiveIndex]) { event.preventDefault() selectSuggestion(suggestions[safeActiveIndex]!.id) return } if (trimmed && onCommitQuery) { event.preventDefault() onCommitQuery(trimmed) onQueryChange('') setOpen(false) } } } const groups = (() => { const map = new Map() suggestions.forEach((suggestion, index) => { const bucket = map.get(suggestion.group) ?? [] bucket.push({ suggestion, index }) map.set(suggestion.group, bucket) }) return [...map.entries()] })() const activeChipLabel = activeChipId ? chips.find((chip) => chip.id === activeChipId)?.label : null return (
{ if (!next) setOpen(false) }} >
{ if (event.target === event.currentTarget) inputRef.current?.focus() }} > {chips.map((chip) => ( {chip.label} ))} { setActiveChipId(null) onQueryChange(event.target.value) setOpen(true) }} onFocus={() => setOpen(true)} onKeyDown={onKeyDown} className="min-h-8 min-w-[8rem] flex-1 bg-transparent text-sm outline-none placeholder:text-muted-foreground" />
event.preventDefault()} onCloseAutoFocus={(event) => event.preventDefault()} >
{groups.map(([group, items]) => (

{group}

{items.map(({ suggestion, index }) => { const active = index === safeActiveIndex return ( ) })}
))}
{activeChipLabel ? (

{activeChipLabel} selecionado. Backspace remove.

) : null} {isPending ? (

Atualizando resultados…

) : null}
{trailing} {onClearAll && hasChips ? ( ) : null}
) }