'use client' import { UserPlusIcon, XIcon } from 'lucide-react' import { useEffect, useId, useMemo, useRef, useState, type FormEvent } from 'react' import type { MunicipalityListLeadershipsResponse } from '@/app/(campaign)/campanha/(app)/municipios/leaderships/types' import { useMunicipalityLeadershipCreate } from '@/components/campaign/municipality/MunicipalityLeadershipCreateProvider' import { CampaignCellEditOverlay, type CampaignCellEditOverlayVariant, } from '@/components/campaign/shared/CampaignCellEditOverlay' import { useCampaignCellFailureChannel } from '@/components/campaign/shared/useCampaignCellFailureChannel' import { Alert, AlertDescription } from '@/components/ui/Alert' import { Badge } from '@/components/ui/Badge' import { Button } from '@/components/ui/button' import { Command, CommandInput, CommandItem, CommandList } from '@/components/ui/Command' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Spinner } from '@/components/ui/Spinner' import { postCampaignJson } from '@/lib/campaignJsonRequest' import { formatBrazilianPhoneInput, normalizeBrazilianPhone, sanitizeBrazilianPhoneInput, } from '@/lib/phone' import { sameIdSet } from '@/lib/sameIdSet' import { cn } from '@/lib/utils' import { matchesAtWordStart } from '@/lib/wordStartFilter' import type { EligibleLeadershipOption, MunicipalityLeadershipSummary, } from '@/utilities/municipality/municipalityViewModels' const LEADERSHIPS_ENDPOINT = '/campanha/municipios/leaderships' const SAVE_ERROR_MESSAGE = 'Não foi possível atualizar as lideranças. Tente novamente.' /** B155 — sentinel temp id for an in-flight inline create; negative = never a real leadership. */ const isPendingCreateId = (id: number): boolean => id < 0 // Shared empty values so the memos stay referentially stable when the bridge // is absent (standalone renders in unit tests) or nothing is pending. const EMPTY_CREATED_OPTIONS: EligibleLeadershipOption[] = [] const EMPTY_PENDING_CREATES: ReadonlyMap = new Map() /** Resolves leadership ids to summaries, dropping any without a name (deleted/unknown). */ const leadershipEntriesFromIds = ( leadershipIDs: readonly number[], namesById: ReadonlyMap, ): MunicipalityLeadershipSummary[] => leadershipIDs.flatMap((id) => { const leadership = namesById.get(id) return leadership ? [{ id: leadership.id, name: leadership.name }] : [] }) type MunicipalityListLeadershipsControlProps = { municipalityID: number municipalityName: string currentLeadershipIDs: number[] leadershipNamesById: ReadonlyMap options: EligibleLeadershipOption[] variant: CampaignCellEditOverlayVariant } export const MunicipalityListLeadershipsControl = ({ municipalityID, municipalityName, currentLeadershipIDs, leadershipNamesById, options, variant, }: MunicipalityListLeadershipsControlProps) => { const [open, setOpen] = useState(false) const [selectedIDs, setSelectedIDs] = useState(currentLeadershipIDs) const [query, setQuery] = useState('') const { errorMessage, setErrorMessage, reportFailure, noteOpenChange } = useCampaignCellFailureChannel() const [isPending, setIsPending] = useState(false) const pendingCountRef = useRef(0) const lastPropsIDsRef = useRef(currentLeadershipIDs) // Requests can settle out of send order (network jitter), so the send order // is tracked explicitly: only the response with the highest sequence number // seen so far is kept, and it is adopted — win or fail — once every // in-flight delta has settled (`pendingCountRef` back to 0). No response // ever writes `lastPropsIDsRef`: that ref exists solely to tell a genuine // external prop change (nav/refresh) apart from a no-op re-render carrying // the same pre-edit content. const requestSeqRef = useRef(0) const latestConfirmedRef = useRef<{ seq: number; leadershipIDs: number[] } | null>(null) // B155 — the inline-create mini-form, hidden until the user picks // "+ Criar liderança" in the no-results branch. const [createFormOpen, setCreateFormOpen] = useState(false) const [createName, setCreateName] = useState('') const [createPhone, setCreatePhone] = useState('') // Unique per mounted instance: the desktop table and the mobile cards are // sibling trees, so the same município renders two controls at once. const createFormId = useId() // Search box to return focus to once the create form closes (the form // unmounts, so focus would otherwise fall to the body). const commandInputRef = useRef(null) const handleOpenChange = (nextOpen: boolean) => { noteOpenChange(nextOpen) setOpen(nextOpen) } // Adopt server props only when they change from outside (navigation / RSC // refresh) — an in-flight delta's optimistic state must not be clobbered by // a prop identical in content to what we already had. useEffect(() => { if (sameIdSet(currentLeadershipIDs, lastPropsIDsRef.current)) return lastPropsIDsRef.current = currentLeadershipIDs setSelectedIDs(currentLeadershipIDs) }, [currentLeadershipIDs]) // B155 — leaderships created inline since the page last rendered server-side, // shared across every row's popover through `MunicipalityLeadershipCreateProvider` // (null when the control renders standalone, e.g. in unit tests). const createBridge = useMunicipalityLeadershipCreate() const createdOptions = createBridge?.createdOptions ?? EMPTY_CREATED_OPTIONS const registerCreatedLeadership = createBridge?.registerCreatedLeadership // tempId → {name, phone} for in-flight creates: the chip renders optimistically // while the request is out, and the id is not known until the response arrives. const [pendingCreates, setPendingCreates] = useState>(EMPTY_PENDING_CREATES) // Every leadership the actor may add is listed here regardless of current // assignment, so it doubles as the name lookup for an id the popover just // optimistically added — no round trip needed to label it. // `leadershipNamesById` is the fallback for an id that fell out of // eligibility but is still assigned; `leadershipEntriesFromIds` drops the // rare id found in neither, same as every other relation cell in the list. const effectiveOptions = useMemo(() => { // The provider bridge can outlive a server re-render (sort/filter // navigation keeps client state, and `getEligibleLeadershipOptions` then // returns the created record again), so the same leadership can arrive // twice — dedupe by id to keep combobox entries and React keys unique. const byId = new Map() for (const option of options) byId.set(option.id, option) for (const option of createdOptions) byId.set(option.id, option) return [...byId.values()] }, [options, createdOptions]) const leadershipLookup = useMemo(() => { const lookup = new Map(leadershipNamesById) for (const option of effectiveOptions) lookup.set(option.id, { id: option.id, name: option.name }) for (const [tempId, pending] of pendingCreates) lookup.set(tempId, { id: tempId, name: pending.name }) return lookup }, [leadershipNamesById, effectiveOptions, pendingCreates]) const selectedSet = useMemo(() => new Set(selectedIDs), [selectedIDs]) const chips = useMemo( () => [...leadershipEntriesFromIds(selectedIDs, leadershipLookup)].sort((left, right) => left.name.localeCompare(right.name, 'pt-BR'), ), [selectedIDs, leadershipLookup], ) // The trigger already reads the names by extenso — no hover tooltip needed. const assignedLabel = chips.length ? chips.map((chip) => chip.name).join(', ') : 'nenhuma' const filteredOptions = useMemo( () => effectiveOptions.filter((option) => matchesAtWordStart(option.name, query)), [effectiveOptions, query], ) // Single settle point shared by the toggle and the inline create (success, // mapped error, network failure): decrements the pending count and, once it // reaches 0, stops the spinner and reconciles to the latest confirmed server // set — even on a request's own failure, an earlier delta in the same batch // may have already confirmed one. const finishRequest = () => { pendingCountRef.current = Math.max(0, pendingCountRef.current - 1) if (pendingCountRef.current > 0) return setIsPending(false) if (latestConfirmedRef.current) setSelectedIDs(latestConfirmedRef.current.leadershipIDs) } const toggle = (leadershipId: number, assigned: boolean) => { // A negative id is a B155 pending-create sentinel, never a real record — // the temp chip has no remove affordance, so this only guards the path. if (isPendingCreateId(leadershipId)) return setErrorMessage(null) setSelectedIDs((current) => { const next = new Set(current) if (assigned) next.add(leadershipId) else next.delete(leadershipId) return [...next] }) setQuery('') pendingCountRef.current += 1 setIsPending(true) const requestSeq = (requestSeqRef.current += 1) void (async () => { const revertDelta = () => { setSelectedIDs((current) => { const next = new Set(current) if (assigned) next.delete(leadershipId) else next.add(leadershipId) return [...next] }) } try { const { ok, payload } = await postCampaignJson( LEADERSHIPS_ENDPOINT, { municipalityId: municipalityID, leadershipId, assigned }, ) if (!ok || payload.status !== 'success') { revertDelta() reportFailure(payload.status === 'error' ? payload.message : SAVE_ERROR_MESSAGE) finishRequest() return } if (requestSeq > (latestConfirmedRef.current?.seq ?? 0)) { latestConfirmedRef.current = { seq: requestSeq, leadershipIDs: payload.leadershipIDs } } finishRequest() } catch { revertDelta() reportFailure(SAVE_ERROR_MESSAGE) finishRequest() } })() } /** * B155 — name+phone inline create, assigned to this município. The chip goes * up immediately under a sentinel temp id (the real id only exists once the * response lands); the request rides the same seq/pending machinery as the * toggle, so out-of-order settles and the floor/cap failure reconcile exactly * like a toggle — a failed create reverts the temp chip and shows the Alert. */ const createLeadership = (name: string, phone: string) => { // A create is not an idempotent delta: re-activating the same person while // a create is in flight would mint a second record. The temp chip already // signals the pending one — skip. if ( [...pendingCreates.values()].some( (pending) => pending.name === name && pending.phone === phone, ) ) { return } setErrorMessage(null) setCreateFormOpen(false) setCreateName('') setCreatePhone('') setQuery('') pendingCountRef.current += 1 setIsPending(true) const requestSeq = (requestSeqRef.current += 1) const tempId = -requestSeq setSelectedIDs((current) => [...current, tempId]) setPendingCreates((current) => new Map(current).set(tempId, { name, phone })) void (async () => { const revertCreate = () => { setSelectedIDs((current) => current.filter((id) => id !== tempId)) setPendingCreates((current) => { const next = new Map(current) next.delete(tempId) return next }) } try { const { ok, payload } = await postCampaignJson( LEADERSHIPS_ENDPOINT, { municipalityId: municipalityID, name, phone }, ) if (!ok || payload.status !== 'success') { revertCreate() reportFailure(payload.status === 'error' ? payload.message : SAVE_ERROR_MESSAGE) finishRequest() return } if (requestSeq > (latestConfirmedRef.current?.seq ?? 0)) { latestConfirmedRef.current = { seq: requestSeq, leadershipIDs: payload.leadershipIDs } } const createdLeadership = payload.createdLeadership if (createdLeadership) { // Swap the sentinel for the real id right away: if another delta is // still in flight the reconcile is deferred, and the chip must not // vanish in the gap (the lookup drops a temp id the moment its // pending entry is removed). setSelectedIDs((current) => [ ...current.filter((id) => id !== tempId), createdLeadership.id, ]) setPendingCreates((current) => { const next = new Map(current) next.delete(tempId) return next }) // Make the record selectable in every other row's popover on this // page load — `createdOptions` is the client-side bridge until the // next navigation's `getEligibleLeadershipOptions` picks it up. registerCreatedLeadership?.({ id: createdLeadership.id, name: createdLeadership.name }) } finishRequest() } catch { revertCreate() reportFailure(SAVE_ERROR_MESSAGE) finishRequest() } })() } const closeCreateForm = () => { setCreateFormOpen(false) // The form unmounts with it; return focus to the search box once the // Command remounts (otherwise keyboard/SR focus falls to the body). requestAnimationFrame(() => commandInputRef.current?.focus()) } const handleCreateSubmit = (event: FormEvent) => { event.preventDefault() const name = createName.trim() const phone = normalizeBrazilianPhone(createPhone) if (name.length < 2 || !phone) return createLeadership(name, phone) } const handlePhoneChange = (value: string) => { setCreatePhone(formatBrazilianPhoneInput(sanitizeBrazilianPhoneInput(value))) } // Mirrors the zod schema (`name 2–120`, `brazilianMobile`) so a malformed // submit never reaches the server's generic error. const canCreate = createName.trim().length >= 2 && createName.trim().length <= 120 ? normalizeBrazilianPhone(createPhone) !== null : false const statusMessage = errorMessage ? errorMessage : isPending ? 'Salvando lideranças.' : '' // The body pads its own sections (`px-4`, aligned with the Drawer header) so // the `Command` can stay full-bleed in both containers — hence `px-0` on the // sheet body and `p-0` on the popover content. const isSheet = variant === 'sheet' const trimmedQuery = query.trim() return ( Nenhuma ) : ( {chips.map((chip) => ( {chip.name} ))} ) } >
{/* The Drawer's own header already titles it. */} {isSheet ? null :

Gerenciar lideranças

}

A liderança passa a aparecer também na ficha dela, na área Lideranças.

{isPending ? ( ) : null}
{chips.length > 0 ? (
{chips.map((chip) => // A pending-create chip has no remove affordance: the server-side // create cannot be cancelled mid-flight, so removal waits for the // real record (and the real toggle) once the response lands. isPendingCreateId(chip.id) ? ( {chip.name} ) : ( ), )}
) : null} {createFormOpen ? (
setCreateName(event.currentTarget.value)} placeholder="Nome completo" autoComplete="name" maxLength={120} autoFocus />
handlePhoneChange(event.currentTarget.value)} placeholder="(71) 99999-9999" />
) : ( {filteredOptions.length === 0 ? ( trimmedQuery.length >= 2 && trimmedQuery.length <= 120 ? ( { setCreateFormOpen(true) setCreateName(trimmedQuery) }} > ) : (

Nenhum resultado.

) ) : ( filteredOptions.map((option) => { const assigned = selectedSet.has(option.id) return ( toggle(option.id, !assigned)} > {option.name} ) }) )}
)} {errorMessage ? (
{errorMessage}
) : null}
) }