Line data Source code
1 : import { formatPlanDate } from '@comfyorg/account-core/billing'
2 : import type {
3 : AutoTopUpCardAction,
4 : AutoTopUpCardState,
5 : AutoTopUpOffView
6 : } from '@comfyorg/account-core/billing'
7 :
8 : type AutoReloadSettings = Extract<
9 : AutoTopUpCardState,
10 : { kind: 'loaded' }
11 : >['settings']
12 : type ServedStatus = AutoReloadSettings['status']
13 :
14 : export type AutoTopUpViewKey =
15 : | ServedStatus
16 : | 'turnedOff'
17 : | 'maximumReached'
18 : | AutoTopUpOffView
19 :
20 : export type AutoTopUpStatusLabel =
21 : | 'failed'
22 : | 'turnedOff'
23 : | 'off'
24 : | 'on'
25 : | 'onHold'
26 : | 'limitReached'
27 : | 'maximumReached'
28 :
29 : export type AutoTopUpActionLabel =
30 : | 'addPaymentMethod'
31 : | 'manage'
32 : | 'turnOn'
33 : | 'updatePayment'
34 :
35 : /** Host-translated strings for the auto top-up card; numbers and dates arrive formatted. */
36 : export interface AutoTopUpCardCopy {
37 : readonly title: string
38 : readonly unavailable: string
39 : readonly tryAgain: string
40 : readonly status: Readonly<Record<AutoTopUpStatusLabel, string>>
41 : readonly actions: Readonly<Record<AutoTopUpActionLabel, string>>
42 : readonly failed: string
43 : readonly turnedOff: string
44 : readonly off: string
45 : readonly noPaymentMethod: string
46 : readonly noDefaultPaymentMethod: string
47 : readonly onHold: string
48 : readonly rule: (target: string, threshold: string) => string
49 : readonly limit: (spent: string, limit: string) => string
50 : readonly limitTooltip: (date: string) => string
51 : readonly limitTooltipNoDate: string
52 : readonly paused: (date: string) => string
53 : readonly pausedNoDate: string
54 : readonly pausedByRemainder: (date: string) => string
55 : readonly pausedByRemainderNoDate: string
56 : readonly maximumReached: (date: string) => string
57 : readonly maximumReachedNoDate: string
58 : }
59 :
60 : export interface AutoTopUpLine {
61 : text: string
62 : tooltip?: string
63 : }
64 :
65 : export interface AutoTopUpCardView {
66 : key: AutoTopUpViewKey
67 : status: AutoTopUpStatusLabel
68 : action: AutoTopUpCardAction
69 : actionLabel: AutoTopUpActionLabel
70 : lines: AutoTopUpLine[]
71 : }
72 :
73 5 : const SERVED_STATUSES: ReadonlySet<string> = new Set<ServedStatus>([
74 : 'failed',
75 : 'off',
76 : 'on_hold',
77 : 'paused_by_budget',
78 : 'on'
79 : ])
80 :
81 5 : const KEYS = {
82 : failed: {
83 : status: 'failed',
84 : action: 'update_payment',
85 : actionLabel: 'updatePayment'
86 : },
87 : turnedOff: {
88 : status: 'turnedOff',
89 : action: 'update_payment',
90 : actionLabel: 'updatePayment'
91 : },
92 : off: { status: 'off', action: 'turn_on', actionLabel: 'turnOn' },
93 : on_hold: {
94 : status: 'onHold',
95 : action: 'update_payment',
96 : actionLabel: 'updatePayment'
97 : },
98 : paused_by_budget: {
99 : status: 'limitReached',
100 : action: 'manage',
101 : actionLabel: 'manage'
102 : },
103 : maximumReached: {
104 : status: 'maximumReached',
105 : action: 'manage',
106 : actionLabel: 'manage'
107 : },
108 : noPaymentMethod: {
109 : status: 'off',
110 : action: 'update_payment',
111 : actionLabel: 'addPaymentMethod'
112 : },
113 : noDefaultPaymentMethod: {
114 : status: 'off',
115 : action: 'update_payment',
116 : actionLabel: 'updatePayment'
117 : },
118 : on: { status: 'on', action: 'manage', actionLabel: 'manage' }
119 : } as const satisfies Record<
120 : AutoTopUpViewKey,
121 : {
122 : status: AutoTopUpStatusLabel
123 : action: AutoTopUpCardAction
124 : actionLabel: AutoTopUpActionLabel
125 : }
126 : >
127 :
128 : /** The card's view for served settings; an unknown served status reads as off. */
129 2 : export function autoTopUpViewKey(
130 : settings: AutoReloadSettings,
131 : offView: AutoTopUpOffView
132 : ): AutoTopUpViewKey {
133 2 : const { status } = settings
134 2 : if (!SERVED_STATUSES.has(status)) return 'off'
135 2 : if (status === 'off') return offView
136 2 : if (
137 : status === 'failed' &&
138 : settings.disabled_reason === 'consecutive_declines'
139 : ) {
140 0 : return 'turnedOff'
141 : }
142 2 : if (status === 'paused_by_budget' && settings.monthly_budget_is_default) {
143 0 : return 'maximumReached'
144 : }
145 0 : return status
146 : }
147 :
148 : /** The status, action and lines the card shows for served settings. */
149 2 : export function autoTopUpCardView(
150 : settings: AutoReloadSettings,
151 : offView: AutoTopUpOffView,
152 : copy: AutoTopUpCardCopy,
153 : locale: string
154 : ): AutoTopUpCardView {
155 2 : const key = autoTopUpViewKey(settings, offView)
156 2 : return { key, ...KEYS[key], lines: linesFor(key, settings, copy, locale) }
157 : }
158 :
159 : interface LineContext {
160 : settings: AutoReloadSettings
161 : copy: AutoTopUpCardCopy
162 : locale: string
163 : rule: AutoTopUpLine[]
164 : tooltip: string
165 : budgetLine: (
166 : dated: (date: string) => string,
167 : undated: string
168 : ) => AutoTopUpLine
169 : }
170 :
171 5 : const statusOnly =
172 : (key: 'failed' | 'turnedOff' | AutoTopUpOffView) =>
173 : ({ copy }: LineContext): AutoTopUpLine[] => [{ text: copy[key] }]
174 :
175 5 : const LINES: Record<
176 : AutoTopUpViewKey,
177 : (context: LineContext) => AutoTopUpLine[]
178 : > = {
179 : failed: statusOnly('failed'),
180 : turnedOff: statusOnly('turnedOff'),
181 : off: statusOnly('off'),
182 : noPaymentMethod: statusOnly('noPaymentMethod'),
183 : noDefaultPaymentMethod: statusOnly('noDefaultPaymentMethod'),
184 : on_hold: ({ rule, copy }) => [...rule, { text: copy.onHold }],
185 : paused_by_budget: ({ rule, copy, settings, budgetLine }) => [
186 : ...rule,
187 : settings.paused_by_budget_remainder
188 : ? budgetLine(copy.pausedByRemainder, copy.pausedByRemainderNoDate)
189 : : budgetLine(copy.paused, copy.pausedNoDate)
190 : ],
191 : maximumReached: ({ rule, copy, budgetLine }) => [
192 : ...rule,
193 : budgetLine(copy.maximumReached, copy.maximumReachedNoDate)
194 : ],
195 : on: ({ rule, copy, settings, locale, tooltip }) =>
196 : settings.monthly_budget_cents === null
197 : ? rule
198 : : [
199 : ...rule,
200 : {
201 : text: copy.limit(
202 : usd(settings.spent_this_cycle_cents, locale),
203 : usd(settings.monthly_budget_cents, locale)
204 : ),
205 : tooltip
206 : }
207 : ]
208 : }
209 :
210 2 : function linesFor(
211 : key: AutoTopUpViewKey,
212 : settings: AutoReloadSettings,
213 : copy: AutoTopUpCardCopy,
214 : locale: string
215 : ): AutoTopUpLine[] {
216 2 : const resetsAt = settings.cycle_resets_at
217 2 : const resetDay =
218 : resetsAt === null ? null : formatPlanDate(resetsAt, locale, 'monthly')
219 2 : const tooltip =
220 : resetDay === null ? copy.limitTooltipNoDate : copy.limitTooltip(resetDay)
221 2 : return LINES[key]({
222 : settings,
223 : copy,
224 : locale,
225 : rule: ruleLines(settings, copy, locale),
226 : tooltip,
227 : budgetLine: (dated, undated) => ({
228 : text: resetDay === null ? undated : dated(resetDay),
229 : tooltip
230 : })
231 : })
232 : }
233 :
234 2 : function ruleLines(
235 : { target_credits: target, threshold_credits: threshold }: AutoReloadSettings,
236 : copy: AutoTopUpCardCopy,
237 : locale: string
238 : ): AutoTopUpLine[] {
239 2 : if (target === null || threshold === null) return []
240 0 : const credits = new Intl.NumberFormat(locale, { maximumFractionDigits: 0 })
241 0 : return [
242 : { text: copy.rule(credits.format(target), credits.format(threshold)) }
243 : ]
244 : }
245 :
246 0 : function usd(cents: number, locale: string): string {
247 0 : return new Intl.NumberFormat(locale, {
248 : style: 'currency',
249 : currency: 'USD',
250 : minimumFractionDigits: cents % 100 === 0 ? 0 : 2,
251 : maximumFractionDigits: 2
252 : }).format(cents / 100)
253 : }
|