diff --git a/src/widget.tsx b/src/widget.tsx index 16f3330..022730f 100644 --- a/src/widget.tsx +++ b/src/widget.tsx @@ -5,6 +5,23 @@ import { BALANCE_OPEN_KV, formatBalanceError, formatCredits, formatTokens, DEFAU import { dayTotals, monthTotals, type ModelTotal } from "./usage" import type { BalanceStore } from "./store" +/** Maximum characters before a model name is ellipsized in the table. */ +const MODEL_NAME_MAX = 28 + +/** Number of model rows shown before the "other models" summary. */ +const MODEL_ROWS_VISIBLE = 3 + +/** + * Shorten a model id for a narrow sidebar column. + * + * @param model Model id reported by OpenCode. + * @returns The id unchanged when short enough, otherwise an ellipsized form. + */ +function shortModel(model: string): string { + if (model.length <= MODEL_NAME_MAX) return model + return `${model.slice(0, MODEL_NAME_MAX - 1)}…` +} + /** * Sidebar section showing the remaining GreenPT credit balance and the * consumption of the API key: real credit spend per day/month plus a @@ -61,7 +78,7 @@ export function GreenptWidget(props: { const d = new Date(s.fetchedAt) const hh = String(d.getHours()).padStart(2, "0") const mm = String(d.getMinutes()).padStart(2, "0") - return `updated ${hh}:${mm}` + return `${hh}:${mm}` } const usage = () => props.store.usageDays() @@ -70,69 +87,80 @@ export function GreenptWidget(props: { const usageVisible = () => todayUsage().creditsSpent > 0 || monthUsage().cost > 0 const todayText = () => `${formatCredits(todayUsage().creditsSpent)} ${unit()}` const monthText = () => `${formatCredits(monthUsage().cost)} ${unit()}` - const foldedSuffix = () => (usageVisible() ? ` · today ${todayText()}` : "") - const modelRows = (): ModelTotal[] => monthUsage().byModel.slice(0, 5) + const modelRows = (): ModelTotal[] => monthUsage().byModel.slice(0, MODEL_ROWS_VISIBLE) + const otherModels = () => Math.max(0, monthUsage().byModel.length - MODEL_ROWS_VISIBLE) return ( }> - - - {open() ? "▼ " : "▶ "} - GreenPT - - {(text) => ( - - {" "} - {text()} - {foldedSuffix()} - - )} - - + + + + {open() ? "▼ " : "▶ "} + GreenPT + + + + {(text) => ( + + {text()} + + )} + + + · today {todayText()} + + + + - - Balance - - {balanceText()} - {updatedAt()} - + + + today {todayText()} · month {monthText()} · {updatedAt()} + - loading… + + loading… + - + + + - {formatBalanceError(error()!)} + + {formatBalanceError(error()!)} + + - - Spent - - today {todayText()} - month {monthText()} - - - - {(row) => ( - - {row.model} - - - {formatTokens(row.usage.tokens.input + row.usage.tokens.output)} tok - - - ~{formatCredits(row.usage.cost)} {unit()} - + + + {(row) => ( + + {shortModel(row.model)} + + + {formatTokens(row.usage.tokens.input + row.usage.tokens.output)} tok + + + ~{formatCredits(row.usage.cost)} {unit()} + + + )} + + 0}> + + + {otherModels()} more models - )} - + +