Skip to content

Commit 82e093b

Browse files
committed
Consolidate formatting functions
1 parent 3f85776 commit 82e093b

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

webview-ui/src/components/cloud/UsagePreview.tsx

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { VSCodeProgressRing } from "@vscode/webview-ui-toolkit/react"
33
import { CircleAlert, SquareArrowOutUpRight } from "lucide-react"
44
import { useAppTranslation } from "@src/i18n/TranslationContext"
55
import { vscode } from "@src/utils/vscode"
6-
import { formatDateShort } from "@/utils/format"
6+
import { formatDateShort, formatLargeNumber, formatCost } from "@/utils/format"
77

88
interface DailyUsage {
99
date: string // ISO date string
@@ -31,21 +31,6 @@ export const UsagePreview = ({ onViewDetails }: UsagePreviewProps) => {
3131
const [error, setError] = useState<string | null>(null)
3232
const [data, setData] = useState<UsageStats | null>(null)
3333

34-
// Format large numbers (e.g., 14800000 -> "14.8M")
35-
const formatTokenCount = (tokens: number): string => {
36-
if (tokens >= 1_000_000) {
37-
return `${(tokens / 1_000_000).toFixed(1)}M`
38-
} else if (tokens >= 1_000) {
39-
return `${(tokens / 1_000).toFixed(1)}K`
40-
}
41-
return tokens.toString()
42-
}
43-
44-
// Format cost (e.g., 17.81 -> "$17.81")
45-
const formatCost = (cost: number): string => {
46-
return `$${cost.toFixed(2)}`
47-
}
48-
4934
// Fetch usage data on mount
5035
useEffect(() => {
5136
setIsLoading(true)
@@ -175,7 +160,7 @@ export const UsagePreview = ({ onViewDetails }: UsagePreviewProps) => {
175160
<span className="text-vscode-foreground">
176161
{t("cloud:usagePreview.tasks", { count: data.totals.tasks })}
177162
<span> · </span>
178-
{t("cloud:usagePreview.tokens", { count: formatTokenCount(data.totals.tokens) })}
163+
{t("cloud:usagePreview.tokens", { count: formatLargeNumber(data.totals.tokens) })}
179164
<span> · </span>
180165
{formatCost(data.totals.cost)}
181166
</span>

webview-ui/src/utils/format.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,7 @@ export const formatTimeAgo = (timestamp: number) => {
8282

8383
return i18next.t("common:time_ago.just_now")
8484
}
85+
86+
export const formatCost = (cost: number): string => {
87+
return `$${cost.toFixed(2)}`
88+
}

0 commit comments

Comments
 (0)