import { CircleAlertIcon, CircleCheckIcon } from 'lucide-react' import Link from 'next/link' import { MunicipalityAdvisorAvatarStack } from '@/components/campaign/MunicipalityAdvisorAvatarStack' import { MunicipalityListAdvisorsControl } from '@/components/campaign/MunicipalityListAdvisorsControl' import { MunicipalityListExpectedVotesControl } from '@/components/campaign/MunicipalityListExpectedVotesControl' import { MunicipalityListGoalCoverageCell } from '@/components/campaign/MunicipalityListGoalCoverageCell' import { MunicipalityListTrendControl } from '@/components/campaign/MunicipalityListTrendControl' import { MunicipalitySortableHead } from '@/components/campaign/MunicipalitySortableHead' import { Badge } from '@/components/ui/Badge' import { Button } from '@/components/ui/button' import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/Table' import { formatElectionNumber } from '@/lib/electionInsights' import { formatMunicipalityVoteRank, formatMunicipalityVoteShare, } from '@/lib/municipalityVoteRank' import { cn } from '@/lib/utils' import type { CampaignFormActionState } from '@/utilities/campaignFormActionError' import { formatMunicipalityConcentrationHint, formatMunicipalityGeographyLabel, formatMunicipalityListSortSummary, formatMunicipalitySignalAgeLabel, isMunicipalitySignalCold, MUNICIPALITY_COLD_SIGNAL_DAYS, municipalityKindLabels, municipalityListCoverageLabels, municipalityPriorityLabels, municipalitySignalAgeInDays, resolveMunicipalityListSort, type MunicipalityListState, } from '@/utilities/municipalityUi' import type { EligibleAdvisorOption, MunicipalityAdvisorSummary, MunicipalityListViewModel, } from '@/utilities/municipalityViewModels' import { DEFAULT_VOTE_ESTIMATE_SCENARIO, voteEstimateScenarioLabels, } from '@/utilities/voteEstimate' import { toMunicipalityPledgeCoverageView } from '@/utilities/votePledgeData' const dateFormatter = new Intl.DateTimeFormat('pt-BR') type MunicipalityStaffFormAction = ( state: CampaignFormActionState, formData: FormData, ) => Promise export type MunicipalityListProps = { municipalities: MunicipalityListViewModel[] advisorNamesById: ReadonlyMap isStaffView: boolean isCoordinator: boolean advisorOptions: EligibleAdvisorOption[] trendFormAction: MunicipalityStaffFormAction advisorsFormAction: MunicipalityStaffFormAction state: MunicipalityListState } const VotePositionReadout = ({ position, layout, }: { position: NonNullable layout: 'table' | 'card' }) => { const share = formatMunicipalityVoteShare(position.share) const rank = formatMunicipalityVoteRank(position.rank) const metaLine = `${formatElectionNumber(position.votes)} · ${rank}` const ariaLabel = `${share} da votação estadual, ${formatElectionNumber(position.votes)} votos, ${rank} de ${formatElectionNumber(position.totalUnits)}` return (
{share} {metaLine}
) } /** * E9 frescor: how long since anybody recorded anything here (staff update or * leadership pledge). * * Cold reads in the warning amber, NOT destructive: early in the campaign * most municípios are past the 21-day threshold, and painting them all red * would drown the one state that really is an error in this list — a priority * município with nobody answering for it. Same reason it stays text with an * icon instead of a third badge: the row already carries the priority and * assessoria pills. */ const SignalAgeReadout = ({ lastSignalAt, layout, }: { lastSignalAt: string | null layout: 'table' | 'card' }) => { const ageInDays = municipalitySignalAgeInDays(lastSignalAt) const isCold = isMunicipalitySignalCold(ageInDays) const ageLabel = formatMunicipalitySignalAgeLabel(ageInDays) const dateLabel = lastSignalAt ? dateFormatter.format(new Date(lastSignalAt)) : null return (
{isCold ? {dateLabel ? ( {dateLabel} ) : null}
) } /** * E9: a priority município with nobody answering for it is the queue's * loudest row, so it reads "Sem responsável" in destructive — the same words * the overview's coluna da vergonha uses to count them. Non-priority ones * keep the softer pending tone: they are a gap, not a fire. */ const AdvisorStatusBadge = ({ municipality }: { municipality: MunicipalityListViewModel }) => { if (municipality.advisorIDs.length > 0) { return ( ) } const isPriority = municipality.priority === 'alta' return ( ) } const advisorEntries = ( municipality: MunicipalityListViewModel, advisorNamesById: ReadonlyMap, ) => municipality.advisorIDs.flatMap((id) => { const advisor = advisorNamesById.get(id) return advisor ? [{ id: advisor.id, name: advisor.name }] : [] }) const advisorNames = ( municipality: MunicipalityListViewModel, advisorNamesById: ReadonlyMap, ): string[] => advisorEntries(municipality, advisorNamesById).map((advisor) => advisor.name) export const MunicipalityList = ({ municipalities, advisorNamesById, isStaffView, isCoordinator, advisorOptions, trendFormAction, advisorsFormAction, state, }: MunicipalityListProps) => { const { sort: activeSort, dir: activeDir } = resolveMunicipalityListSort(state) const sortSummary = formatMunicipalityListSortSummary(activeSort, activeDir) const concentrationHint = formatMunicipalityConcentrationHint() const signalHint = `Última atualização da equipe ou declaração de liderança, o que for mais recente. Fica destacado a partir de ${MUNICIPALITY_COLD_SIGNAL_DAYS} dias sem registro.` // The scenario picker is client state, so the server can only order by one // scenario — named here so the ordering never looks arbitrary. const deficitHint = `Ordena pelo que falta para a meta (meta − comprometido) no cenário ${voteEstimateScenarioLabels[DEFAULT_VOTE_ESTIMATE_SCENARIO]}, independente do cenário selecionado acima.` return ( <>

{sortSummary}

{municipalities.map((municipality) => { const names = advisorNames(municipality, advisorNamesById) const position = municipality.votePosition2022 return (

{municipality.name}

{formatMunicipalityGeographyLabel(municipality)}

{position ? : null}
{municipality.priority === 'alta' && isStaffView ? ( {municipalityPriorityLabels.alta} ) : null}
{isStaffView ? (
Votos estimados
Tendência
Cobertura da meta
Último sinal
Assessoria
{isCoordinator ? ( ) : names.length ? ( names.join(', ') ) : ( )}
) : null}
) })}
{sortSummary}. Coluna 2022: {concentrationHint} Município Território de identidade Tipo {isStaffView ? ( <> Assessores Tendência Votos estimados Último sinal Assessoria Cobertura da meta ) : ( Última atualização )} {municipalities.map((municipality) => { return (
{municipality.name} {municipality.priority === 'alta' && isStaffView ? ( {municipalityPriorityLabels.alta} ) : null}
{municipality.region} {municipalityKindLabels[municipality.kind]} {municipality.votePosition2022 ? ( ) : ( — )} {isStaffView ? ( <> {isCoordinator ? ( ) : ( )}
) : ( {municipality.lastUpdateAt ? dateFormatter.format(new Date(municipality.lastUpdateAt)) : 'Sem atualização'} )}
) })}
) }