'use client' import { XIcon } from 'lucide-react' import { useState } from 'react' import { Badge } from '@/components/ui/Badge' import { Command, CommandInput, CommandItem, CommandList } from '@/components/ui/Command' import { Spinner } from '@/components/ui/Spinner' import { useAsyncSearchOptions } from '@/hooks/useAsyncSearchOptions' import { isContactSearchQueryReady } from '@/lib/contactSearchQuery' import { CONTENT_PIECE_LEADERS_MAX, CONTENT_PIECE_PUBLIC_FIGURES_MAX } from '@/lib/contentPiece' import { filterPublicFigures } from '@/lib/publicFigureCatalog' import { normalizeForSearch } from '@/lib/speechSearch' import type { ContentPieceLeaderOption } from '@/utilities/content/contentPieceLeaderOptions' /** One removable chip of the two recortes (design scenes 01/02). */ const PeopleChip = ({ label, onRemove }: { label: string; onRemove: () => void }) => ( {label} ) /** * S37 — "Quem aparece na peça" (approved design scenes 01/01A/02): the two * curated recortes inside the C211 ficha. The leader picker searches the * existing records through a gated server action (async, 2+ chars, only the * display name); the public-figure picker filters the static catalog and * accepts free text ("Usar «…»"). Both keep removable chips and repeated hidden * inputs — the ficha nests no person record, only display names. */ export const ContentPiecePeopleField = ({ leaders, publicFigures, searchLeaders, }: { leaders: readonly ContentPieceLeaderOption[] publicFigures: readonly string[] searchLeaders: (query: string) => Promise }) => { const [selectedLeaders, setSelectedLeaders] = useState([...leaders]) const [selectedFigures, setSelectedFigures] = useState([...publicFigures]) const [leaderQuery, setLeaderQuery] = useState('') const [figureQuery, setFigureQuery] = useState('') const { options: leaderResults, loading: leadersLoading, failed: leadersFailed, } = useAsyncSearchOptions({ open: true, query: leaderQuery, search: searchLeaders, isQueryReady: isContactSearchQueryReady, }) const selectedLeaderIds = new Set(selectedLeaders.map((leader) => leader.id)) const selectedFigureKeys = new Set(selectedFigures.map(normalizeForSearch)) const atLeaderCapacity = selectedLeaders.length >= CONTENT_PIECE_LEADERS_MAX const atFigureCapacity = selectedFigures.length >= CONTENT_PIECE_PUBLIC_FIGURES_MAX const toggleLeader = (option: ContentPieceLeaderOption) => { setSelectedLeaders((current) => { if (current.some((leader) => leader.id === option.id)) { return current.filter((leader) => leader.id !== option.id) } return atLeaderCapacity ? current : [...current, option] }) } const toggleFigure = (name: string) => { const trimmed = name.trim() const key = normalizeForSearch(trimmed) if (!key) return setSelectedFigures((current) => { if (current.some((figure) => normalizeForSearch(figure) === key)) { return current.filter((figure) => normalizeForSearch(figure) !== key) } return atFigureCapacity ? current : [...current, trimmed] }) } const figureResults = filterPublicFigures(figureQuery) const figureText = figureQuery.trim() const canUseFigureText = figureText !== '' && !selectedFigureKeys.has(normalizeForSearch(figureText)) && !atFigureCapacity const nothingMarked = selectedLeaders.length === 0 && selectedFigures.length === 0 return (
Quem aparece na peça

Quem aparece na peça

Marque somente pessoas visíveis ou participantes desta peça. A publicação expõe apenas o nome.

Curadoria humana
{selectedLeaders.map((leader) => ( ))} {selectedFigures.map((figure) => ( ))} {nothingMarked ? (

Ninguém marcado. A peça pode ser publicada normalmente e não cria nenhum nome na faceta “Lideranças”.

) : null}

Lideranças da campanha

Busca nos registros existentes. Nenhum telefone, município ou voto vai para a Central pública.

{selectedLeaders.length ? (
{selectedLeaders.map((leader) => ( toggleLeader(leader)} /> ))}
) : null} {leadersLoading ? (
) : leadersFailed ? (

Não foi possível concluir a busca.

) : leaderResults.length > 0 ? ( leaderResults.map((option) => { const selected = selectedLeaderIds.has(option.id) return ( toggleLeader(option)} > {option.label} Registro da campanha ) }) ) : (

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

)}

Figuras públicas

Texto curado com catálogo, no mesmo molde de Instituição.

{selectedFigures.length ? (
{selectedFigures.map((figure) => ( toggleFigure(figure)} /> ))}
) : null} {figureResults.map((entry) => { const selected = selectedFigureKeys.has(normalizeForSearch(entry.name)) return ( toggleFigure(entry.name)} > {entry.name} Figura pública catalogada ) })} {canUseFigureText ? ( { toggleFigure(figureText) setFigureQuery('') }} > Usar «{figureText}» ) : null} {figureResults.length === 0 && !canUseFigureText ? (

Nenhum resultado encontrado.

) : null}
) }