import { ChevronDownIcon, CircleAlertIcon, CircleCheckIcon } from 'lucide-react' import Link from 'next/link' import type { ReactNode } from 'react' import { CampaignTransitionAnchor } from '@/components/campaign/CampaignListPending' import { MunicipalityAdvisorAvatarStack } from '@/components/campaign/MunicipalityAdvisorAvatarStack' import { MunicipalityListAdvisorsControl } from '@/components/campaign/MunicipalityListAdvisorsControl' import { MunicipalityListExpectedVotesControl } from '@/components/campaign/MunicipalityListExpectedVotesControl' import { MunicipalityListTrendControl } from '@/components/campaign/MunicipalityListTrendControl' import { Badge } from '@/components/ui/Badge' import { Button } from '@/components/ui/button' import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/Table' import { cn } from '@/lib/utils' import type { CampaignFormActionState } from '@/utilities/campaignFormActionError' import { buildMunicipalitySortHref, DEFAULT_MUNICIPALITY_LIST_SORT_DIR, DEFAULT_MUNICIPALITY_LIST_SORT_KEY, formatMunicipalityGeographyLabel, municipalityKindLabels, municipalityListSortLabels, municipalityPriorityLabels, type MunicipalityListSortKey, type MunicipalityListState, } from '@/utilities/municipalityUi' import type { EligibleAdvisorOption, MunicipalityAdvisorSummary, MunicipalityListViewModel, } from '@/utilities/municipalityViewModels' 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 } type MunicipalitySortableHeadProps = { state: MunicipalityListState sortKey: MunicipalityListSortKey children: ReactNode className?: string align?: 'left' | 'center' } const MunicipalitySortableHead = ({ state, sortKey, children, className, align = 'left', }: MunicipalitySortableHeadProps) => { const active = (state.sort ?? DEFAULT_MUNICIPALITY_LIST_SORT_KEY) === sortKey const dir = active ? state.dir ?? DEFAULT_MUNICIPALITY_LIST_SORT_DIR : undefined const isAscending = dir === 'asc' return ( {children} ) } 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) => ( <>
{municipalities.map((municipality) => { const names = advisorNames(municipality, advisorNamesById) return (

{municipality.name}

{formatMunicipalityGeographyLabel(municipality)}

{municipality.priority === 'alta' && isStaffView ? ( {municipalityPriorityLabels.alta} ) : null}
{isStaffView ? (
Votos estimados
Tendência
Assessoria
{isCoordinator ? ( ) : names.length ? ( names.join(', ') ) : ( 'Sem assessor' )}
) : null}
) })}
Praça Território de identidade Tipo {isStaffView ? ( <> Assessores Tendência Votos estimados Última atualização Cobertura ) : ( Última atualização )} {municipalities.map((municipality) => { const hasAdvisor = municipality.advisorIDs.length > 0 return (
{municipality.name} {municipality.priority === 'alta' && isStaffView ? ( {municipalityPriorityLabels.alta} ) : null}
{municipality.region} {municipalityKindLabels[municipality.kind]} {isStaffView ? ( <> {isCoordinator ? ( ) : ( )}
{municipality.lastUpdateAt ? dateFormatter.format(new Date(municipality.lastUpdateAt)) : 'Sem atualização'} {hasAdvisor ? ( ) : ( {municipality.lastUpdateAt ? dateFormatter.format(new Date(municipality.lastUpdateAt)) : 'Sem atualização'} )}
) })}
)