'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 { 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 } /** Dense chip trigger styling shared by the Tendência/Nível chips (B193). */ const chipTriggerClassName = 'min-h-8 rounded-full px-2.5' /** Ghost trigger styling for the read-only relation groups (no hover pill). */ const relationTriggerClassName = 'min-h-0 rounded-md bg-transparent px-0 hover:bg-transparent' const ChipLabel = ({ children }: { children: ReactNode }) => ( {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, }: 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) 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}
{isCity ? ( Tendência Não registrada ) : ( ( <> Tendência {trend.status ? politicalTrendLabels[trend.status] : 'Não registrada'} )} /> )} {isCity ? ( — ) : canMoveEngagementLevel ? ( ( <> Nível )} /> ) : ( Nível )}
{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}
) }