Skip to content

Commit b283191

Browse files
committed
show true usage
1 parent 1afb1a7 commit b283191

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

apps/dashboard/app/(main)/billing/hooks/use-billing.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,30 @@ export function useBilling(refetch?: () => void) {
138138

139139
const includedUsage = feature.included_usage || 0;
140140
const balance = feature.balance || 0;
141-
// Calculate used amount: included_usage - balance
142-
// If usage field exists and is greater, use that instead (for accuracy)
143-
const calculatedUsed = Math.max(0, includedUsage - balance);
144141
const reportedUsage = feature.usage || 0;
145-
const actualUsed = Math.max(calculatedUsed, reportedUsage);
142+
143+
// Handle special cases: infinity values or unlimited features
144+
const isUnlimited =
145+
feature.unlimited ||
146+
!Number.isFinite(balance) ||
147+
!Number.isFinite(reportedUsage) ||
148+
balance >= Number.MAX_SAFE_INTEGER;
149+
150+
let actualUsed = 0;
151+
if (!isUnlimited) {
152+
// Calculate used amount: included_usage - balance
153+
// If usage field exists and is positive, use that instead
154+
const calculatedUsed = Math.max(0, includedUsage - balance);
155+
const positiveReportedUsage = Math.max(0, reportedUsage);
156+
actualUsed = Math.max(calculatedUsed, positiveReportedUsage);
157+
}
146158

147159
return {
148160
id: feature.id,
149161
name: feature.name,
150162
used: actualUsed,
151-
limit: feature.unlimited ? Number.POSITIVE_INFINITY : includedUsage,
152-
unlimited: feature.unlimited,
163+
limit: isUnlimited ? Number.POSITIVE_INFINITY : includedUsage,
164+
unlimited: isUnlimited,
153165
nextReset: feature.next_reset_at
154166
? dayjs(feature.next_reset_at).format('MMM D, YYYY')
155167
: null,
@@ -207,18 +219,30 @@ export function useBillingData() {
207219
? Object.values(customer.features).map((feature) => {
208220
const includedUsage = feature.included_usage || 0;
209221
const balance = feature.balance || 0;
210-
// Calculate used amount: included_usage - balance
211-
// If usage field exists and is greater, use that instead (for accuracy)
212-
const calculatedUsed = Math.max(0, includedUsage - balance);
213222
const reportedUsage = feature.usage || 0;
214-
const actualUsed = Math.max(calculatedUsed, reportedUsage);
223+
224+
// Handle special cases: infinity values or unlimited features
225+
const isUnlimited =
226+
feature.unlimited ||
227+
!Number.isFinite(balance) ||
228+
!Number.isFinite(reportedUsage) ||
229+
balance >= Number.MAX_SAFE_INTEGER;
230+
231+
let actualUsed = 0;
232+
if (!isUnlimited) {
233+
// Calculate used amount: included_usage - balance
234+
// If usage field exists and is positive, use that instead
235+
const calculatedUsed = Math.max(0, includedUsage - balance);
236+
const positiveReportedUsage = Math.max(0, reportedUsage);
237+
actualUsed = Math.max(calculatedUsed, positiveReportedUsage);
238+
}
215239

216240
return {
217241
id: feature.id,
218242
name: feature.name,
219243
used: actualUsed,
220-
limit: feature.unlimited ? Number.POSITIVE_INFINITY : includedUsage,
221-
unlimited: !!feature.unlimited,
244+
limit: isUnlimited ? Number.POSITIVE_INFINITY : includedUsage,
245+
unlimited: isUnlimited,
222246
nextReset: feature.next_reset_at
223247
? new Date(feature.next_reset_at).toLocaleDateString()
224248
: null,

0 commit comments

Comments
 (0)