Line data Source code
1 : import { CREDITS_PER_USD, formatPlanDate } from '@comfyorg/account-core/billing'
2 : import type { PlanCreditsState } from '@comfyorg/account-core/billing'
3 :
4 : export { CREDITS_PER_USD }
5 :
6 : /** Host-translated strings for the Credits card. */
7 : export interface CreditsCardCopy {
8 : readonly title: string
9 : readonly rateInfo: string
10 : readonly rateTooltip: string
11 : readonly available: string
12 : readonly unavailable: string
13 : readonly tryAgain: string
14 : readonly overdrawn: (count: string, credits: number) => string
15 : readonly plan: string
16 : readonly planTooltip: string
17 : /** Template holding the `{remaining}` and `{total}` placeholders. */
18 : readonly planOfPool: string
19 : readonly planUsed: (percent: number) => string
20 : readonly refills: (date: string) => string
21 : readonly refillsAfterPayment: string
22 : readonly availableUntil: (date: string) => string
23 : readonly topUp: string
24 : readonly topUpTooltip: string
25 : readonly inUse: string
26 : readonly addCredits: string
27 : readonly addCreditsReason: (reasonKey: string) => string
28 : }
29 :
30 2356 : const WHOLE: Intl.NumberFormatOptions = {
31 : minimumFractionDigits: 0,
32 : maximumFractionDigits: 0
33 : }
34 :
35 : /** Whole credits for a served cent amount. */
36 14 : export function centsToCredits(cents: number): number {
37 14 : return Math.round((cents * CREDITS_PER_USD) / 100)
38 : }
39 :
40 : /** Locale-grouped whole credits. */
41 9 : export function formatWholeCredits(credits: number, locale: string): string {
42 9 : return new Intl.NumberFormat(locale, WHOLE).format(credits)
43 : }
44 :
45 : /** The share of the plan pool already spent, or null when usage is not shown. */
46 0 : export function planUsedPercent(plan: PlanCreditsState): number | null {
47 0 : if (!plan.showUsage || !plan.planPoolCredits) return null
48 0 : const remaining = centsToCredits(plan.planCents) / plan.planPoolCredits
49 0 : return Math.round(Math.min(Math.max(1 - remaining, 0), 1) * 100)
50 : }
51 :
52 : /** Host-translated strings for the credits flyout breakdown. */
53 : export interface CreditsBreakdownCopy extends Pick<
54 : CreditsCardCopy,
55 : | 'planOfPool'
56 : | 'refills'
57 : | 'refillsAfterPayment'
58 : | 'availableUntil'
59 : | 'unavailable'
60 : > {
61 : readonly planCredits: string
62 : readonly topUpCredits: string
63 : }
64 :
65 : type PlanDateCopy = Pick<
66 : CreditsCardCopy,
67 : 'refills' | 'refillsAfterPayment' | 'availableUntil'
68 : >
69 :
70 : /** The plan pool's refill or availability line; a null cadence keeps the year. */
71 0 : export function planDateLine(
72 : plan: PlanCreditsState,
73 : copy: PlanDateCopy,
74 : locale: string,
75 : cadence: 'monthly' | 'yearly' | null = null
76 : ): string {
77 0 : if (plan.planDate.kind === 'afterPayment') return copy.refillsAfterPayment
78 0 : const date = plan.planDate.date
79 : ? formatPlanDate(plan.planDate.date, locale, cadence)
80 : : ''
81 0 : if (!date) return ''
82 0 : return plan.planDate.kind === 'refills'
83 : ? copy.refills(date)
84 : : copy.availableUntil(date)
85 : }
86 :
87 : /** The plan-of-pool template split around the remaining amount. */
88 0 : export function splitPlanOfPool(
89 : template: string,
90 : total: string
91 : ): { before: string; after: string } {
92 0 : const [before = '', after = ''] = template
93 : .replaceAll('{total}', total)
94 : .split('{remaining}')
95 0 : return { before, after }
96 : }
|