teqo-backup/src/components/campaign/municipality/MunicipalityLevelBadge.tsx
Francisco Ignácio Fontoura Solla c7001b75f0 E14: engagement levels N0-N4 and the first allocationDecision writer.
`municipality.engagementLevel` (staff-only, no default) records what the
campaign decided to invest in a município, next to the E8/E10 metrics that
describe what it knows. Moving it is a reallocation decision, so only
unrestricted staff may, every move carries a rationale plus the signals that
would reverse it, and the write files an `allocationDecision` in the same
transaction under an advisory lock taken before the current level is read —
otherwise the snapshot's `from` is a guess. The enum gains `movimento`: a level
move is neither `aceita` nor `descarta`.

No backfill. Null is "Sem nível", and filtering by it is the triage queue.
That is also why the list sorts `nivel` in memory rather than in the query:
Postgres puts NULLs first on DESC, so a native sort would open "N4 primeiro"
on a page of municípios nobody has decided about yet. The filter stays native.

The hysteresis rules from research §6.8 are pure and client-safe, so the
Popover explains a block before submitting and the server re-runs them anyway;
a blocked move answers 409 with typed violations and the override is recorded
with exactly the rules it contradicted.
2026-07-27 21:19:33 -03:00

67 lines
2.4 KiB
TypeScript

import { Badge } from '@/components/ui/Badge'
import {
EMPTY_ENGAGEMENT_LEVEL_LABEL,
formatEngagementLevelLabel,
type EngagementLevel,
} from '@/lib/engagementLevel'
/**
* Deliberately NOT a colour ramp. A five-step scale of importance painted
* across a staff list is the "mapa de onde você não vai defender" the research
* warns about (anti-goal 11), and this row already spends red on priority,
* amber on cold signals and five fills on the E10 class. The ordinal
* information is in the numeral; the pill is a hairline. Absence of a level is
* not a sixth step, so it reads as flat muted text instead.
*/
const ENGAGEMENT_LEVEL_BADGE_VARIANT = 'outline' as const
const EMPTY_ENGAGEMENT_LEVEL_BADGE_VARIANT = 'ghost' as const
type MunicipalityLevelBadgeProps = {
level: EngagementLevel | null
/** The rationale behind the current level, when the caller has it. */
note?: string | null
/**
* In the table the numeral carries it and the long name lives in the
* column's `cellTooltip`; elsewhere there is room to spell it out.
*/
layout: 'table' | 'card'
}
/**
* E14 — the read-only face of the ladder, for staff who cannot move it.
*
* The long name (and the motivo, when given) is always TEXT next to the pill —
* `sr-only` in the table, visible in the card — because the B23 `cellTooltip`
* contract is that the tooltip only repeats what the cell already says: Radix
* mounts tooltip content on open, and the table adds no tab stops. Same shape
* as `TerritorialClassReadout` in the neighbouring column.
*/
export const MunicipalityLevelBadge = ({ level, note, layout }: MunicipalityLevelBadgeProps) => {
if (!level) {
return (
<Badge variant={EMPTY_ENGAGEMENT_LEVEL_BADGE_VARIANT} className="text-muted-foreground">
{EMPTY_ENGAGEMENT_LEVEL_LABEL}
</Badge>
)
}
const label = formatEngagementLevelLabel(level)
// The card spells the name out in the pill itself, so only the motivo is
// left to say; the table shows the numeral and owes the reader both.
if (layout === 'card') {
return (
<>
<Badge variant={ENGAGEMENT_LEVEL_BADGE_VARIANT}>{label}</Badge>
{note ? <span className="text-xs text-muted-foreground">{note}</span> : null}
</>
)
}
return (
<>
<Badge variant={ENGAGEMENT_LEVEL_BADGE_VARIANT}>{level.toUpperCase()}</Badge>
<span className="sr-only">{[label, note].filter(Boolean).join(' — ')}</span>
</>
)
}