teqo/src/utilities/municipality/municipalitySignal.ts
Cursor Agent 6955cccc6b
B150 — Município v2 bloco Agora (encaminhamento + sugestão + visita)
- Loader e seção Agora na rota /campanha/municipio/[slug]/v2
- Encaminhamento editável com auto-save via POST /municipios/next-steps
- Até 2 sugestões E11 com resolve/dispensa existente; silêncio explícito
- Visita E13 condensada em uma linha + link ao compositor de giros
- Revalidação v2 ao resolver sugestão; helper compartilhado de empty state E11

Co-authored-by: Francisco Solla <fsolla@users.noreply.github.com>
2026-08-03 00:28:47 +00:00

66 lines
2.7 KiB
TypeScript

/**
* E9 "frescor" — signal freshness for the allocation queue. One rule shared by
* the server ordering (`municipalityPageData.ts`) and the list cell, so
* "há N dias" never disagrees with the position in the queue. Split out of the
* former `municipalityUi.ts` in Pass 2 W1.
*/
import { latestIsoTimestamp } from '@/lib/campaignTime'
import { DAY_MS } from '@/lib/text'
/**
* The last time ANYBODY recorded something here: a staff update or a
* leadership pledge declaration/estimate.
*/
export const resolveMunicipalityLastSignalAt = (
lastUpdateAt: string | null,
lastPledgeAt: string | null,
): string | null => latestIsoTimestamp(lastUpdateAt, lastPledgeAt)
/**
* Days since the last signal, floored. The research report only says a
* commitment left untouched "for weeks" is worth less, so the threshold below
* is a convention (3 weeks), not a measured decay curve.
*/
export const MUNICIPALITY_COLD_SIGNAL_DAYS = 21
export const municipalitySignalAgeInDays = (
lastSignalAt: string | null,
now: Date = new Date(),
): number | null => {
if (!lastSignalAt) return null
const elapsed = now.getTime() - new Date(lastSignalAt).getTime()
if (Number.isNaN(elapsed)) return null
return Math.max(0, Math.floor(elapsed / DAY_MS))
}
export const isMunicipalitySignalCold = (ageInDays: number | null): boolean =>
ageInDays === null || ageInDays >= MUNICIPALITY_COLD_SIGNAL_DAYS
/**
* "há 3 dias" / "hoje" / "Sem sinal" — dense cell copy for the queue.
* Deliberately not `formatRelativeAge`: its `numeric: 'auto'` yields "ontem"/
* "anteontem" and minute/hour granularity, which breaks both the day-based
* cold threshold and the tabular-nums scan down the column.
*/
export const formatMunicipalitySignalAgeLabel = (ageInDays: number | null): string => {
if (ageInDays === null) return 'Sem sinal'
if (ageInDays === 0) return 'hoje'
if (ageInDays === 1) return 'há 1 dia'
return `há ${ageInDays} dias`
}
/**
* The same age as a lowercase clause for prose ("o município nunca recebeu
* sinal", "…sem sinal há 40 dias") — E11's silence review embeds it
* mid-sentence, where the cell label above would break the flow.
*/
export const formatSilenceAgeLabel = (ageInDays: number | null): string =>
ageInDays === null ? 'nunca recebeu sinal' : `sem sinal há ${ageInDays} dias`
/** E11 empty queue copy — shared by the detail overview and the v2 Agora block. */
export const formatMunicipalitySuggestionEmptyMessage = (
silence: { lastSignalAgeDays: number | null } | null,
): string =>
silence
? `Nenhum padrão do catálogo dispara aqui — e o município ${formatSilenceAgeLabel(silence.lastSignalAgeDays)}. Silêncio é pergunta: vale checar a rede e o registro.`
: 'Nenhum padrão do catálogo dispara aqui agora.'