feat(app): Overview capsule chart + list row components

CapsuleChart renders one .c bar per history.daily entry with the peak day
gradient-glowed (.c.hi) and runner-up plain blue (.c.hi2); axis top is the
peak day's spend. ListRow is the reusable .li row (rank, model .mdot,
title+sub, value, chevron) plus seriesColorForModel() for the palette dots.
This commit is contained in:
iamtoruk 2026-07-10 16:10:32 -07:00
parent 48d669bd23
commit 191fca35d1
2 changed files with 130 additions and 0 deletions

View file

@ -0,0 +1,79 @@
import type { DailyHistoryEntry } from '../lib/types'
/** "YYYY-MM-DD" → "Mon D" (parsed as a local date to avoid a TZ day-shift). */
export function fmtDay(key: string): string {
const [y, m, d] = key.split('-').map(Number)
if (!y || !m || !d) return key
return new Date(y, m - 1, d).toLocaleDateString('en-US', { month: 'short', day: 'numeric' })
}
/** Axis-label formatting: whole dollars once we're past $10, else one decimal. */
function fmtAxis(v: number): string {
if (v >= 10) return String(Math.round(v))
return v.toFixed(1)
}
/**
* The daily-spend capsule chart. One `.c` bar per `history.daily` entry, height
* = cost / axisMax where axisMax is the peak day's spend, so the tallest bar
* fills the plot (CSS `min-height` shows a stub for $0 days). The peak day gets
* the gradient+glow treatment (`.c.hi`), the runner-up plain blue (`.c.hi2`).
* Gridline labels read peak / half / zero; the day axis shows up to five evenly
* spaced dates.
*/
export function CapsuleChart({ daily }: { daily: DailyHistoryEntry[] }) {
const costs = daily.map(d => d.cost)
const max = costs.reduce((m, c) => (c > m ? c : m), 0)
const axisMax = max > 0 ? max : 1
// Peak (.c.hi) = highest-cost day, runner-up (.c.hi2) = second highest. Strict
// `>` keeps the earliest day on ties and never highlights a $0 day.
let peak = -1
let second = -1
costs.forEach((c, i) => {
if (c <= 0) return
if (peak === -1 || c > costs[peak]) {
second = peak
peak = i
} else if (second === -1 || c > costs[second]) {
second = i
}
})
const n = daily.length
const labelIdx = [
...new Set(n <= 5 ? daily.map((_, i) => i) : [0, 0.25, 0.5, 0.75, 1].map(f => Math.round(f * (n - 1)))),
]
return (
<>
<div className="plot">
<div className="gl" style={{ top: 0 }}>
<em>{fmtAxis(axisMax)}</em>
</div>
<div className="gl" style={{ top: '50%' }}>
<em>{fmtAxis(axisMax / 2)}</em>
</div>
<div className="gl" style={{ top: '100%' }}>
<em>0</em>
</div>
<div className="bars">
{costs.map((c, i) => {
const cls = i === peak ? 'c hi' : i === second ? 'c hi2' : 'c'
const height = Math.max(0, Math.min(100, (c / axisMax) * 100))
return (
<div className={cls} key={i}>
<div className="b" style={{ height: `${height}%` }} />
</div>
)
})}
</div>
</div>
<div className="days">
{labelIdx.map(i => (
<span key={i}>{fmtDay(daily[i].date)}</span>
))}
</div>
</>
)
}

View file

@ -0,0 +1,51 @@
import type { CSSProperties, ReactNode } from 'react'
/**
* Series-dot colour for a model name, per the wireframe palette
* (blue=Opus, purple=Sonnet, lavender=Haiku, cyan=GPT/Codex). Unknown or
* unmatched models fall back to a neutral slate rather than a fabricated series.
*/
export function seriesColorForModel(model?: string): string {
const m = (model ?? '').toLowerCase()
if (m.includes('opus')) return 'var(--blue)'
if (m.includes('sonnet')) return 'var(--purple)'
if (m.includes('haiku')) return 'var(--lav)'
if (m.includes('gpt') || m.includes('codex')) return 'var(--cyan)'
return 'var(--t3)'
}
/**
* A `.li` list row: optional rank `.no`, optional model series `.mdot`, a
* title + sub line `.lx`, an optional right-aligned `.val`, and the chevron.
* Presentational; used by Overview's "most expensive sessions" and reusable by
* later sections.
*/
export function ListRow({
no,
dotColor,
title,
sub,
value,
valueClass,
}: {
no?: ReactNode
dotColor?: string
title: ReactNode
sub?: ReactNode
value?: ReactNode
valueClass?: string
}) {
const dot: CSSProperties | undefined = dotColor ? { background: dotColor } : undefined
return (
<div className="li">
{no !== undefined && <span className="no">{no}</span>}
{dotColor !== undefined && <span className="mdot" style={dot} />}
<div className="lx">
<b>{title}</b>
{sub !== undefined && <span>{sub}</span>}
</div>
{value !== undefined && <span className={valueClass ? `val ${valueClass}` : 'val'}>{value}</span>}
<span className="chev">›</span>
</div>
)
}