teqo-backup/src/lib/campaignOps/municipalityDetailLocalViews.ts
Cursor Agent 4e65559fdd
OH9: dual-path municipality detail (HeaderView + Local + OfflineBoundary)
Extract presentational header shared by RSC and offline Local; OfflineBoundary
swaps to mirror-backed Local only when OPS_HYBRID is on and the browser is
offline (sync failure stays on RSC + chrome). Closes #172.

Co-authored-by: Francisco Solla <fsolla@users.noreply.github.com>
2026-08-02 01:10:00 +00:00

47 lines
1.7 KiB
TypeScript

import type { OpsLeadership, OpsMunicipality, OpsVotePledge } from '@/lib/campaignOps/opsContract'
import {
toVoteEstimateScenarioViewModel,
type VoteEstimateScenarioViewModel,
} from '@/lib/voteEstimate'
/** Mirror→panel row — structural match for `StaffPledgeRow` without importing utilities. */
export type OpsLocalStaffPledgeRow = {
id: number
leadershipID: number
contactName: string
declaredVotes: number
declaredAt: string | null
estimatedVotes: VoteEstimateScenarioViewModel
estimateNote: string | null
estimatedAt: string | null
}
export const findOpsMunicipalityBySlug = (
municipalities: ReadonlyArray<OpsMunicipality>,
slug: string,
): OpsMunicipality | null => municipalities.find((row) => row.slug === slug) ?? null
export const toLocalStaffPledgeRows = (
municipalityId: number,
pledges: ReadonlyArray<OpsVotePledge>,
leaderships: ReadonlyArray<OpsLeadership>,
): OpsLocalStaffPledgeRow[] => {
const leadershipById = new Map(leaderships.map((row) => [row.id, row]))
return pledges
.filter((pledge) => pledge.municipality === municipalityId)
.map((pledge) => {
const leadership = leadershipById.get(pledge.leadership)
return {
id: pledge.id,
leadershipID: pledge.leadership,
contactName: leadership?.contact.name ?? 'Liderança',
declaredVotes: pledge.declaredVotes,
declaredAt: pledge.declaredAt ?? null,
estimatedVotes: toVoteEstimateScenarioViewModel(pledge.estimatedVotes),
estimateNote: pledge.estimateNote ?? null,
estimatedAt: pledge.estimatedAt ?? null,
}
})
.sort((left, right) => left.contactName.localeCompare(right.contactName, 'pt-BR'))
}