import { Box, Text, TextAttributes, useUiHost } from "../../../ui"; import { blendHex, colors } from "../../../theme/colors"; import { formatNumber, truncateToDisplayWidth } from "../../../utils/format"; import type { ShareBarRow } from "./model"; import { formatShare } from "./model"; /** * Horizontal share bars: label, bar sized against the largest row, count and * share, and the 30-day change when the history reaches back that far. The * terminal draws block runs at half-cell resolution; the desktop draws real * elements so length is fractional. */ export interface ShareBarsProps { rows: ShareBarRow[]; width: number; color?: string; /** Show the 30d delta column when any row has one. */ showDelta?: boolean; } const COUNT_WIDTH = 6; const SHARE_WIDTH = 5; const DELTA_WIDTH = 6; function labelWidthFor(width: number): number { return Math.max(8, Math.min(18, Math.floor(width * 0.28))); } function deltaText(delta: number | null): string { if (delta == null || delta === 0) return ""; return `${delta > 0 ? "+" : ""}${formatNumber(delta, 0)}`; } function deltaColor(delta: number | null): string { if (delta == null || delta === 0) return colors.textDim; return delta > 0 ? colors.positive : colors.negative; } function terminalBar(ratio: number, cells: number): string { const units = Math.round(ratio * cells * 2); const full = Math.floor(units / 2); return `${"█".repeat(full)}${units % 2 ? "▌" : ""}`; } export function ShareBars({ rows, width, color = colors.borderFocused, showDelta = false }: ShareBarsProps) { const isDesktopWeb = useUiHost().kind === "desktop-web"; const hasDelta = showDelta && rows.some((row) => row.delta != null && row.delta !== 0); const labelWidth = labelWidthFor(width); const trailing = COUNT_WIDTH + SHARE_WIDTH + (hasDelta ? DELTA_WIDTH + 1 : 0) + 2; const barWidth = Math.max(4, width - labelWidth - trailing - 2); const track = blendHex(colors.bg, color, 0.16); return ( {rows.map((row) => ( {truncateToDisplayWidth(row.label, labelWidth - 1)} {isDesktopWeb ? ( 0 ? "2px" : "0", borderRadius: "2px" }} /> ) : ( {terminalBar(row.ratio, barWidth)} )} {formatNumber(row.count, 0)} {formatShare(row.share)} {hasDelta ? ( {deltaText(row.delta)} ) : null} ))} ); }