'use client' import { ChevronsUpDownIcon, XIcon } from 'lucide-react' import { useState } from 'react' import { Badge } from '@/components/ui/Badge' import { Button } from '@/components/ui/button' import { Command, CommandDialog, CommandGroup, CommandInput, CommandItem, CommandList, } from '@/components/ui/Command' import { Field, FieldDescription, FieldError, FieldLabel } from '@/components/ui/field' import { Spinner } from '@/components/ui/Spinner' import { useAsyncSearchOptions } from '@/hooks/useAsyncSearchOptions' import { isContactSearchQueryReady } from '@/lib/contactSearchQuery' import { MAX_ACTIVITY_RESPONSIBLES, type ActivityResponsibleCollection, } from '@/lib/schemas/activity' import { cn } from '@/lib/utils' export type ResponsibleOption = { relationTo: ActivityResponsibleCollection id: number name: string typeLabel: string } type ResponsibleMultiSelectProps = { /** FormData field name — a hidden JSON field with the typed entries. */ name: string label: string value?: ResponsibleOption[] error?: string description?: string /** Extra classes for the field label (e.g. `sr-only` on label-less sheets). */ labelClassName?: string /** Trigger text while empty (replaces the "Nenhum" fallback). */ emptyText?: string /** Extra classes for the trigger button (e.g. borderless list rows). */ triggerClassName?: string search: (query: string) => Promise } /** * C90 — polymorphic multi-value responsible picker for the activity form * (later reused by the agenda inline creation, C91). One search catalog across * staff/leadership/dobradinha, results grouped by type, removable typed chips. */ export const ResponsibleMultiSelect = ({ name, label, value = [], error, description, labelClassName, emptyText, triggerClassName, search, }: ResponsibleMultiSelectProps) => { // Initial-only (like RelationMultiSelect's initialSelectedIDs): on the create // page `value` is a fresh `[]` every render and a sync effect would wipe the // user's picks on any failed submit. const [selected, setSelected] = useState(value) const [open, setOpen] = useState(false) const [query, setQuery] = useState('') const { options, loading, failed } = useAsyncSearchOptions({ open, query, search, isQueryReady: isContactSearchQueryReady, }) const selectedKey = (option: ResponsibleOption) => `${option.relationTo}:${option.id}` const selectedKeys = new Set(selected.map(selectedKey)) const atCapacity = selected.length >= MAX_ACTIVITY_RESPONSIBLES const add = (option: ResponsibleOption) => { if (atCapacity) return setSelected((current) => current.some((entry) => selectedKey(entry) === selectedKey(option)) ? current : [...current, option], ) } const remove = (option: ResponsibleOption) => { const key = selectedKey(option) setSelected((current) => current.filter((entry) => selectedKey(entry) !== key)) } const visibleOptions = options.filter((option) => !selectedKeys.has(selectedKey(option))) const groups = visibleOptions.reduce>((acc, option) => { const group = acc[option.typeLabel] ?? [] group.push(option) acc[option.typeLabel] = group return acc }, {}) const selectedLabel = selected.length === 0 ? (emptyText ?? 'Nenhum') : `${selected.length} ${selected.length === 1 ? 'responsável' : 'responsáveis'}` return ( {label} ({ relationTo, value: id })))} /> {selected.length ? (
    {selected.map((option) => (
  • {option.name} {option.typeLabel}
  • ))}
) : null} {loading ? (
) : failed ? (

Não foi possível concluir a busca.

) : Object.keys(groups).length > 0 ? ( Object.entries(groups).map(([typeLabel, groupOptions]) => ( {groupOptions.map((option) => ( add(option)} disabled={atCapacity} > {option.name} ))} )) ) : (

{!isContactSearchQueryReady(query) ? 'Digite ao menos dois caracteres para buscar.' : 'Nenhum resultado encontrado.'}

)}
{description ? {description} : null} {error ? {error} : null}
) }