'use client' import { ChevronDownIcon, CircleAlertIcon } from 'lucide-react' import Link from 'next/link' import { useState, type ReactNode } from 'react' import { advisorEntriesFromIds, MunicipalityAdvisorAvatarStack, } from '@/components/campaign/municipality/MunicipalityAdvisorAvatarStack' import { MunicipalityLevelBadge } from '@/components/campaign/municipality/MunicipalityLevelBadge' import { MunicipalityListAdvisorsControl } from '@/components/campaign/municipality/MunicipalityListAdvisorsControl' import { MunicipalityListExpectedVotesControl } from '@/components/campaign/municipality/MunicipalityListExpectedVotesControl' import { MunicipalityListLeadershipsControl } from '@/components/campaign/municipality/MunicipalityListLeadershipsControl' import { MunicipalityListLevelControl } from '@/components/campaign/municipality/MunicipalityListLevelControl' import { TerritorialClassCardReadout } from '@/components/campaign/municipality/MunicipalityListRowReadouts' import { MunicipalityListTrendControl } from '@/components/campaign/municipality/MunicipalityListTrendControl' import { MunicipalityListUpdateControl } from '@/components/campaign/municipality/MunicipalityListUpdateControl' import { MunicipalityVotePositionReadout } from '@/components/campaign/municipality/MunicipalityVotePositionReadout' import { TerritoryLink } from '@/components/campaign/municipality/TerritoryLink' import { MunicipalityRelationAvatarStack, type MunicipalityRelationEntry, } from '@/components/campaign/shared/MunicipalityRelationAvatarStack' import { MunicipalityStateDeputyRelationCell, type MunicipalityStateDeputyCreateAction, } from '@/components/campaign/shared/MunicipalityStateDeputyRelationCell' import { VoteEstimateScenarioStrip } from '@/components/campaign/votePledge/VoteEstimateScenarioStrip' import { Badge } from '@/components/ui/Badge' import { rowEditingAllowed, type AdvisorEditingScope } from '@/lib/campaignAdvisorProfile' import { SALVADOR_CITY_AGGREGATE_LABEL } from '@/lib/salvadorCity' import { municipalityUpdatePolarityBadgeVariant, municipalityUpdatePolarityLabels, } from '@/lib/schemas/municipalityUpdate' import { cn } from '@/lib/utils' import type { CampaignFormActionState } from '@/utilities/campaignFormActionError' import type { StateDeputyRelationOption } from '@/utilities/campaignRelationOptions' import { municipalityGeographyParts, politicalTrendBadgeVariant, politicalTrendLabels, } from '@/utilities/municipality/municipalityLabels' import { formatMunicipalitySignalAgeLabel, isMunicipalitySignalCold, municipalitySignalAgeInDays, } from '@/utilities/municipality/municipalitySignal' import type { EligibleAdvisorOption, EligibleLeadershipOption, MunicipalityAdvisorSummary, MunicipalityLeadershipSummary, MunicipalityListViewModel, } from '@/utilities/municipality/municipalityViewModels' import { toMunicipalityPledgeCoverageView } from '@/utilities/votePledgeViews' const dateTimeFormatter = new Intl.DateTimeFormat('pt-BR', { dateStyle: 'short', timeStyle: 'short', }) type MunicipalityStaffFormAction = ( state: CampaignFormActionState, formData: FormData, ) => Promise type MunicipalityMobileCardProps = { municipality: MunicipalityListViewModel advisorNamesById: ReadonlyMap leadershipNamesById: ReadonlyMap isStaffView: boolean isCoordinator: boolean isCampaignUnrestricted: boolean canMoveEngagementLevel: boolean advisorOptions: EligibleAdvisorOption[] leadershipOptions: EligibleLeadershipOption[] stateDeputyOptions: StateDeputyRelationOption[] stateDeputyCommitAction: MunicipalityStaffFormAction stateDeputyCreateAction: MunicipalityStateDeputyCreateAction signalFormAction: MunicipalityStaffFormAction /** C142 — the UI write scope for the quick-edit chips. */ editingScope: AdvisorEditingScope portfolioIDs: ReadonlySet | null } /** * B196 — dense chip trigger styling shared by the Tendência/Nível chips: * the whole label+value block is the tap target, label above the pill * (gate B). `hover:bg-transparent` kills the base trigger's hover pill — * the block reads as label+value, not a button. `min-h-11` keeps the touch * target the plan's own `min-h-0` would have cut. */ const chipTriggerClassName = 'flex min-h-11 flex-col items-start justify-center gap-0.5 rounded-md px-1 hover:bg-transparent' /** Ghost trigger styling for the read-only relation groups (no hover pill). * `w-full` keeps the avatar overlap row constrained to the group's flex-1 * box — without it the inline-block button grows with the avatars' min-content * and the stack spills into the neighbouring group (B196). */ const relationTriggerClassName = 'min-h-0 w-full rounded-md bg-transparent px-0 hover:bg-transparent' const ChipLabel = ({ children }: { children: ReactNode }) => ( {children} ) /** B196 (gate B) — one chip block: label above the value pill. */ const ChipBlock = ({ label, children }: { label: string; children: ReactNode }) => (
{label} {children}
) /** Read-only city rows render a plain dash everywhere a control would sit. */ const CityDash = () => — const RelationGroupTrigger = ({ label, entries, emptyState, }: { label: string entries: MunicipalityRelationEntry[] emptyState: ReactNode }) => (
{label}
) /** * One third of the avatar row: the label + the relation control (or the * read-only display) for a single association type. */ const RelationGroup = ({ label, children }: { label: string; children: ReactNode }) => (
{children ?? (
{label}
)}
) /** * B193 — the dense mobile municipality card: header with the 2022 vote * position on the right, the estimate scenario bar with the active marker, * classe/tendência/nível chips, three labelled avatar groups and the * expandable "Última atualização" footer. Every data point is its own edit * target (bottom sheet) — anything else on the card falls through to the * stretched município link and opens the detail page. */ export const MunicipalityMobileCard = ({ municipality, advisorNamesById, leadershipNamesById, isStaffView, isCoordinator, isCampaignUnrestricted, canMoveEngagementLevel, advisorOptions, leadershipOptions, stateDeputyOptions, stateDeputyCommitAction, stateDeputyCreateAction, signalFormAction, editingScope, portfolioIDs, }: MunicipalityMobileCardProps) => { const [expanded, setExpanded] = useState(false) const isCity = municipality.isCity const isPriority = municipality.priority === 'alta' && isStaffView const { region, zoneSuffix } = municipalityGeographyParts(municipality) const position = municipality.votePosition2022 const advisors = advisorEntriesFromIds(municipality.advisorIDs, advisorNamesById) const update = municipality.lastUpdate const updateAge = municipalitySignalAgeInDays(update?.createdAt ?? null) const updateIsCold = isMunicipalitySignalCold(updateAge) // C142 — per-row write gate for the mobile quick-edit chips. const rowEditable = !isCity && rowEditingAllowed(editingScope, portfolioIDs ? [...portfolioIDs] : null, [municipality.id]) const registerCta = ( Registrar atualização ) const updateControl = (trigger: ReactNode) => ( {trigger} ) return (

{municipality.name} {isCity ? Cidade : null}

{isCity ? ( <> {SALVADOR_CITY_AGGREGATE_LABEL} · {region} ) : ( <> {zoneSuffix ? ` ${zoneSuffix}` : null} )}

{position ? (
) : null}
{isStaffView ? ( <> {!isCity ? ( ( )} /> ) : null} {/* B196 (gate B) — every chip is a label-above-value block; the row accepts 1–2 lines, but no chip ever breaks in the middle. */}
{isCity ? ( Não registrada ) : ( ( {trend.status ? politicalTrendLabels[trend.status] : 'Não registrada'} )} /> )} {isCity ? ( ) : canMoveEngagementLevel ? ( ( )} /> ) : ( )}
{isCity ? null : isCoordinator ? ( ( )} triggerClassName={relationTriggerClassName} /> ) : (
Assessores
)}
{isCity ? null : ( ( )} triggerClassName={relationTriggerClassName} /> )} {isCity ? null : ( ( )} triggerClassName={relationTriggerClassName} /> )}
{isCity ? ( ) : update ? ( <> {expanded ? (
{municipalityUpdatePolarityLabels[update.polarity]} {update.urgent ? Urgente : null} {update.adversarySignal ? Adversário : null}

{update.body ?? 'Sem texto.'}

{update.authorName} {dateTimeFormatter.format(new Date(update.createdAt))}

{updateControl(registerCta)}
) : null} ) : ( updateControl(registerCta) )} ) : null}
) }