import { ArrowDownRight, ArrowUpRight, Minus } from 'lucide-react'; import { t } from '@/i18n'; import { cn } from '@/lib/cn'; import { formatChange, formatPeriod } from '@/lib/format'; import type { ChangeValue, FormatSpec } from '@/lib/types'; /** * Signed change with ↑/↓ glyph and direction colour. Colour = direction × whether up is good * (`higher_is_better`): good → up colour, bad → down colour, unknown → neutral ink. Never colour alone. * Uses the API's `formatted` string when present, else lib/format.ts. */ export function ChangeChip({ change, spec, prevPeriod, className, showVs = true }: { change: ChangeValue | null | undefined; spec: FormatSpec; prevPeriod?: string | null; className?: string; showVs?: boolean }) { if (!change) return null; const c = formatChange(change.abs, change.pct, spec); if (!c) return null; const text = change.formatted ?? c.text; const hib = spec.higher_is_better; const tone = c.direction === 'flat' || hib == null ? 'neutral' : (c.direction === 'up') === hib ? 'good' : 'bad'; const Icon = c.direction === 'up' ? ArrowUpRight : c.direction === 'down' ? ArrowDownRight : Minus; const label = c.direction === 'up' ? t('common.up') : c.direction === 'down' ? t('common.down') : t('common.flat'); return ( {label} {text} {showVs && prevPeriod ? {t('common.vs', { period: formatPeriod(prevPeriod, spec.frequency ?? 'A') })} : null} ); }