/** * Server-rendered Report pages: a list (filter by source, noteworthy or quiet) * and one report with its findings and the cards it filed. Pure (no * kody:runtime). Read-only pages; they work without JavaScript. */ import { esc, icon, renderShell } from './ui.ts' import { renderMarkdown } from '../lib/markdown.ts' import type { FactoryReport, FindingSeverity } from '../reports/model.ts' import type { WorkItemState } from '../core/work-item.ts' const TZ = 'America/Denver' function fmtTime(iso: string | null | undefined): string { const t = iso ? Date.parse(iso) : Number.NaN if (!Number.isFinite(t)) return 'unknown' return new Intl.DateTimeFormat('en-US', { timeZone: TZ, month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit' }).format(new Date(t)) } const SEVERITY: Record = { info: ['Info', 'off'], notice: ['Notice', 'st-triaged'], warning: ['Warning', 'lvl-warn'], critical: ['Critical', 'lvl-bad'], } const STATE_LABEL: Record = { intake: 'New', triaged: 'Triaged', planned: 'Planned', running: 'Running', verifying: 'Verifying', shipped: 'Shipped', 'needs-kent': 'Needs you', dropped: 'Dropped', missing: 'Removed', } function stateChip(state: string): string { return `${esc(STATE_LABEL[state] ?? state)}` } function periodText(r: FactoryReport): string { if (!r.period) return '' if (r.period.label) return r.period.label if (r.period.start && r.period.end) return `${r.period.start} to ${r.period.end}` return r.period.start ?? r.period.end ?? '' } function countsChips(r: FactoryReport): string { const created = r.cards.filter((c) => c.relation === 'created').length const existing = r.cards.length - created const needsYou = r.cards.filter((c) => c.needsKent).length const flagged = r.findings.filter((f) => f.flagged).length const chips: string[] = [] if (!r.noteworthy) chips.push('Quiet') if (created) chips.push(`${created} new card${created === 1 ? '' : 's'}`) if (existing) chips.push(`${existing} still open`) if (needsYou) chips.push(`${needsYou} need${needsYou === 1 ? 's' : ''} you`) if (flagged) chips.push(`${flagged} flagged`) return `
${chips.join('')}
` } function reportRow(bp: string, r: FactoryReport): string { const period = periodText(r) return `
  • ${esc(r.title)}
    ${esc(r.source)}${r.projectId ? `${esc(r.projectId)}` : ''}${period ? `${esc(period)}` : ''}${esc(fmtTime(r.createdAt))}${r.findings.length} finding${r.findings.length === 1 ? '' : 's'}
    ${countsChips(r)}
  • ` } export type ReportsPageOptions = { basePath: string reports: FactoryReport[] total: number sources: string[] sourceFilter: string showFilter: '' | 'noteworthy' | 'quiet' } export function renderReportsPage(opts: ReportsPageOptions): string { const bp = esc(opts.basePath) const tab = (value: ReportsPageOptions['showFilter'], label: string) => { const params = new URLSearchParams() if (value) params.set('show', value) if (opts.sourceFilter) params.set('source', opts.sourceFilter) const qs = params.toString() return `${esc(label)}` } const sourceOptions = ['', ...opts.sources] .map((s) => ``) .join('') const list = opts.reports.length ? `` : `
    ${icon('inbox')}

    No reports yet. Audits and weekly reviews post here, with or without cards.

    ` const main = `

    Reports

    What audits and scheduled reviews found. A report can need no work, or file one or many cards that link back to it.

    Report list

    ${opts.reports.length} of ${opts.total} reports shown

    ${list}
    ` return renderShell({ basePath: opts.basePath, view: 'reports', title: 'Reports', main }) } export type ReportDetailOptions = { basePath: string report: FactoryReport /** Live state of each card, by id. Missing ids were removed. */ cardStates: Record } export function renderReportDetail(opts: ReportDetailOptions): string { const bp = esc(opts.basePath) const r = opts.report const period = periodText(r) const cardFor = new Map(r.cards.map((c) => [c.cardId, c])) const cardLink = (cardId: string, label?: string) => `${esc(label ?? 'Open card')}` const cardRows = r.cards.length ? `
      ${r.cards .map((c) => { const live = opts.cardStates[c.cardId] const state = live?.state ?? 'missing' return `
    • ${live ? `${esc(c.title)}` : `${esc(c.title)}`}
      ${c.relation === 'created' ? 'Filed by this report' : 'Already open, seen again'}${(live?.needsKent ?? c.needsKent) ? 'Decision for you' : 'Work card'}${esc(c.cardId)}
      ${stateChip(state)}
    • ` }) .join('')}
    ` : `

    No cards. Nothing here needs work.

    ` const findingRows = r.findings.length ? `
      ${r.findings .map((f) => { const [label, cls] = SEVERITY[f.severity] ?? SEVERITY.info const card = f.cardId ? cardFor.get(f.cardId) : undefined return `
    • ${esc(f.title)} ${f.detail ? `
      ${renderMarkdown(f.detail)}
      ` : ''} ${card ? `
      ${cardLink(card.cardId, card.relation === 'created' ? 'Card filed' : 'Open card')}
      ` : ''}
      ${f.flagged ? 'Flagged' : ''}${esc(label)}
    • ` }) .join('')}
    ` : `

    No findings.

    ` const metrics = r.metrics && Object.keys(r.metrics).length ? `

    Numbers

      ${Object.entries(r.metrics) .map(([k, v]) => `
    • ${esc(k)}
      ${esc(typeof v === 'number' ? v.toLocaleString('en-US') : v)}
    • `) .join('')}
    ` : '' const links = r.links.length ? `` : '' const main = `

    ${esc(r.title)}

    ${esc([r.source, r.projectId, period, fmtTime(r.createdAt)].filter(Boolean).join(' ยท '))}

    ${countsChips(r)}

    Summary

    ${renderMarkdown(r.summary)}
    ${links}

    Cards (${r.cards.length})

    ${cardRows}
    ${metrics || `

    Notification

    ${r.push?.sent ? 'Sent a push for this report.' : r.noteworthy ? 'No push sent.' : 'Quiet report: recorded without a push.'}

    `}

    Findings (${r.findings.length})

    ${findingRows}

    Report ${esc(r.id)}

    ` return renderShell({ basePath: opts.basePath, view: 'reports', title: r.title, main }) } export function renderReportMissing(basePath: string): string { const bp = esc(basePath) return renderShell({ basePath, view: 'reports', title: 'Report not found', main: `

    Report not found

    It may have been removed, or only the newest reports are kept.

    `, }) }