Files
gohoarder/frontend/src/lib/reason.ts
T
lukaszraczylo ad8295455d fix(ui): clearer hold reasons, aligned details and chart states
Render the hold reason as a sentence, pluralise counts, give the details grid a fixed rhythm, use distinct registry colours, label the plotted series, and show a note for a single day of history.
2026-11-24 01:27:22 +00:00

34 lines
1.3 KiB
TypeScript

import { formatSeverityName } from '@/lib/severity'
/** "1 finding", "2 findings". */
export function plural(count: number, noun: string): string {
return `${count} ${noun}${count === 1 ? '' : 's'}`
}
const THRESHOLD_REASON = /^Package has (\d+) (CRITICAL|HIGH|MODERATE|MEDIUM|LOW) vulnerabilities \(threshold: (-?\d+)\)$/i
const FLOOR_REASON = /^Package has ([A-Za-z, ]+?) vulnerabilities$/
/** Turns the server's hold reason into a sentence a reader can act on. Unknown reasons pass through. */
export function humanReason(reason: string | undefined): string {
if (!reason) return ''
const t = THRESHOLD_REASON.exec(reason)
if (t) {
const count = Number(t[1])
const level = t[2].toLowerCase()
const limit = Number(t[3])
const rule = limit <= 0 ? `Policy holds any ${level} finding.` : `Policy holds more than ${plural(limit, `${level} finding`)}.`
return `${plural(count, `${level} finding`)}. ${rule}`
}
const f = FLOOR_REASON.exec(reason)
if (f) {
const levels = f[1]
.split(/,\s*(?:or\s+)?|\s+or\s+/)
.map((s) => s.trim())
.filter(Boolean)
.map((s) => formatSeverityName(s).toLowerCase())
const named = levels.length > 1 ? `${levels.slice(0, -1).join(', ')} or ${levels[levels.length - 1]}` : levels[0]
return `Has a ${named} finding. Policy holds a finding at that level.`
}
return reason
}