import { SearchXIcon } from 'lucide-react' import Link from 'next/link' import { advisorEntriesFromIds, formatAdvisorNamesTooltip, 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 { MunicipalityListGoalCoverageCell } from '@/components/campaign/municipality/MunicipalityListGoalCoverageCell' import { MunicipalityListLevelControl } from '@/components/campaign/municipality/MunicipalityListLevelControl' import { MunicipalityListMobileSection } from '@/components/campaign/municipality/MunicipalityListMobileSection' import { SignalAgeReadout, TerritorialClassReadout, } from '@/components/campaign/municipality/MunicipalityListRowReadouts' import { MunicipalityListSignalControl } from '@/components/campaign/municipality/MunicipalityListSignalControl' import { MunicipalityListTrendControl } from '@/components/campaign/municipality/MunicipalityListTrendControl' import { MunicipalityPriorityIndicator } from '@/components/campaign/municipality/MunicipalityPriorityIndicator' import { MunicipalitySortableHead } from '@/components/campaign/municipality/MunicipalitySortableHead' import { MunicipalityVotePositionReadout } from '@/components/campaign/municipality/MunicipalityVotePositionReadout' import { TerritoryLink } from '@/components/campaign/municipality/TerritoryLink' import { CampaignTransitionAnchor } from '@/components/campaign/shared/CampaignListPending' import { CampaignTable, type CampaignTableColumn } from '@/components/campaign/shared/CampaignTable' import { buttonVariants } from '@/components/ui/button' import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle, } from '@/components/ui/Empty' import { resolveVisibleColumns, type CampaignColumnVisibility, } from '@/lib/campaignColumnVisibility' import { formatEngagementLevelLabel } from '@/lib/engagementLevel' import { cn } from '@/lib/utils' import type { CampaignFormActionState } from '@/utilities/campaignFormActionError' import { formatTerritorialClassWhy, municipalityColumnDescriptions, type MunicipalityListColumnId, } from '@/utilities/municipality/municipalityLabels' import { clearMunicipalityListFilters, type MunicipalityFilterOption, } from '@/utilities/municipality/municipalityListFilters' import { buildMunicipalityListHref, formatMunicipalityListSortSummary, municipalityColumnLabels, resolveMunicipalityListSort, type MunicipalityListState, } from '@/utilities/municipality/municipalityListUrl' import type { EligibleAdvisorOption, MunicipalityAdvisorSummary, MunicipalityListViewModel, } from '@/utilities/municipality/municipalityViewModels' import { toMunicipalityPledgeCoverageView } from '@/utilities/votePledgeViews' const dateFormatter = new Intl.DateTimeFormat('pt-BR') type MunicipalityStaffFormAction = ( state: CampaignFormActionState, formData: FormData, ) => Promise type MunicipalityColumnFilterOptions = { /** Bare catalog slugs — labeled on the client (B16+ payload trim). */ name: readonly string[] region: MunicipalityFilterOption[] advisor: MunicipalityFilterOption[] } export type MunicipalityListProps = { municipalities: MunicipalityListViewModel[] advisorNamesById: ReadonlyMap isStaffView: boolean isCoordinator: boolean /** * E14 — moving the ladder is unrestricted staff (coordinator + candidate), * a wider set than `isCoordinator`, so it travels as its own prop instead of * quietly hiding the control from the candidate. */ canMoveEngagementLevel: boolean advisorOptions: EligibleAdvisorOption[] columnFilterOptions: MunicipalityColumnFilterOptions signalFormAction: MunicipalityStaffFormAction state: MunicipalityListState columnVisibility: CampaignColumnVisibility } /** Replaces only the rows: the filter header row and the overview stay put. */ const MunicipalityListEmptyState = ({ state }: { state: MunicipalityListState }) => ( Nenhum município encontrado Ajuste a busca ou os filtros. Você só vê municípios dentro do seu escopo. {/* Same contract as the filter bar's Limpar: drop filters, keep the sort. */} Limpar busca e filtros ) const advisorEntries = ( municipality: MunicipalityListViewModel, advisorNamesById: ReadonlyMap, ) => advisorEntriesFromIds(municipality.advisorIDs, advisorNamesById) /** * Desktop table columns (Pass 2 W1). `id` is the picker key, cookie key, and B22 * description key — typed instead of widening to `string`. */ type MunicipalityColumn = CampaignTableColumn & { id: MunicipalityListColumnId } const municipalityListColumns = ({ state, isStaffView, isCoordinator, canMoveEngagementLevel, columnFilterOptions, advisorNamesById, advisorOptions, signalFormAction, }: MunicipalityListProps): Array => [ { id: 'name', label: municipalityColumnLabels.name, mandatory: true, head: ( ), cellClassName: 'sticky left-0 z-[5] min-w-56 whitespace-normal bg-background', cell: (municipality) => (
{municipality.name} {municipality.priority === 'alta' && isStaffView ? : null}
), }, { id: 'region', label: municipalityColumnLabels.region, head: ( ), cellClassName: 'max-w-56 whitespace-normal text-muted-foreground', cell: (municipality) => , }, { id: 'votos', label: municipalityColumnLabels.votos, head: ( ), cellClassName: 'text-right', cell: (municipality) => municipality.votePosition2022 ? ( ) : ( — ), }, ...(isStaffView ? ([ { // Sits right after "2022": both read the same TSE artifact, and the // class is the one-word summary of the votes beside it. id: 'classe', label: municipalityColumnLabels.classe, head: ( ), cell: (municipality) => , // The factors behind the label — the cell keeps them as `sr-only` // text, so this stays a redundant affordance (see `cellTooltip`). cellTooltip: (municipality) => municipality.territorialClass === 'sem_base' ? null : formatTerritorialClassWhy(municipality.territorialClassFactors), }, { // Right after "Classe": the pair reads diagnosis then decision. id: 'level', label: municipalityColumnLabels.level, head: ( ), cell: (municipality) => canMoveEngagementLevel ? ( ) : ( ), // The editable cell wraps its own trigger in a tooltip; declaring it // here too would double it up on the same gesture (as in `advisors`). cellTooltip: (municipality) => canMoveEngagementLevel || !municipality.engagementLevel ? null : [formatEngagementLevelLabel(municipality.engagementLevel), municipality.levelNote] .filter(Boolean) .join(' — '), }, { id: 'advisors', label: municipalityColumnLabels.advisors, head: ( ), cell: (municipality) => isCoordinator ? ( ) : ( ), // The coordinator's cell already wraps its own Popover trigger in a // tooltip (see `MunicipalityListAdvisorsControl`) — declaring it // here too would double it up on the same gesture. cellTooltip: (municipality) => isCoordinator ? null : formatAdvisorNamesTooltip(advisorEntries(municipality, advisorNamesById)), }, { id: 'trend', label: municipalityColumnLabels.trend, head: ( ), cell: (municipality) => ( ), }, { id: 'expectedVotes', label: municipalityColumnLabels.expectedVotes, head: ( ), cellClassName: 'relative overflow-visible align-middle text-center', cell: (municipality) => (
), }, { id: 'lastSignal', label: municipalityColumnLabels.lastSignal, head: ( {/* The one header that does NOT inherit its sort label: the column shows the signal, `frescor` sorts by how old it is. */} {municipalityColumnLabels.lastSignal} ), cell: (municipality) => ( ), }, { id: 'goalCoverage', label: municipalityColumnLabels.goalCoverage, head: ( ), cell: (municipality) => ( ), }, ] satisfies Array) : ([ { id: 'lastUpdateAt', label: municipalityColumnLabels.lastUpdateAt, head: ( ), cell: (municipality) => municipality.lastUpdateAt ? dateFormatter.format(new Date(municipality.lastUpdateAt)) : 'Sem atualização', }, ] satisfies Array)), ] export const MunicipalityList = (props: MunicipalityListProps) => { const { municipalities, advisorNamesById, isStaffView, isCoordinator, advisorOptions, state } = props const { sort: activeSort, dir: activeDir } = resolveMunicipalityListSort(state) const sortSummary = formatMunicipalityListSortSummary(activeSort, activeDir) const columns = municipalityListColumns(props) // Asks the same function the table asks, so a column that becomes // `mandatory` cannot be on screen while the caption denies it. const showsVoteColumn = resolveVisibleColumns( columns, props.columnVisibility.hiddenColumnIds, ).some((column) => column.id === 'votos') return ( <>

{sortSummary}

} /> {/* A sticky can't paint the row border, hence the inset shadow. */} {sortSummary} {showsVoteColumn ? `. Coluna 2022: ${municipalityColumnDescriptions.votos}` : null} } columns={columns} columnVisibility={props.columnVisibility} rows={municipalities} rowKey={(municipality) => municipality.id} empty={} /> ) }