Line data Source code
1 : import type {
2 : AutoReloadSettings,
3 : BillingStatus,
4 : PeriodCreditGrant
5 : } from '@comfyorg/ingest-types'
6 :
7 : import type { PlanRowState } from './planRow'
8 :
9 : export interface CreditsCardBalance {
10 : amountMicros: number
11 : effectiveBalanceMicros?: number
12 : effectiveBalanceAuthoritative?: boolean
13 : prepaidBalanceMicros?: number
14 : cloudCreditBalanceMicros?: number
15 : cloudCreditDisplayBalanceCents?: number
16 : }
17 :
18 : export interface CreditsCardFacts {
19 : balance: CreditsCardBalance | null
20 : planKind: PlanRowState['kind']
21 : billingStatus: BillingStatus | null
22 : periodCreditGrant: PeriodCreditGrant | null
23 : planPoolCredits: number | null
24 : nextCreditRefillAt: string | null
25 : endDate: string | null
26 : }
27 :
28 : export type CreditsCardState =
29 : | { kind: 'unavailable' }
30 : | { kind: 'noPlan'; availableCents: number }
31 : | {
32 : kind: 'plan'
33 : availableCents: number
34 : planCents: number
35 : planPoolCredits: number | null
36 : topUpCents: number
37 : topUpInUse: boolean
38 : showUsage: boolean
39 : planDate:
40 : | { kind: 'refills' | 'availableUntil'; date: string | null }
41 : | { kind: 'afterPayment' }
42 : }
43 :
44 : export type PlanCreditsState = Extract<CreditsCardState, { kind: 'plan' }>
45 :
46 2355 : const PLAN_USAGE_SHOWN: Record<BillingStatus, boolean> = {
47 : awaiting_payment_method: true,
48 : pending_payment: true,
49 : paid: true,
50 : payment_failed: true,
51 : paused: false,
52 : inactive: true
53 : }
54 :
55 : /** Maps the served balance and plan state onto the Credits card. */
56 9 : export function deriveCreditsCard(facts: CreditsCardFacts): CreditsCardState {
57 9 : const { balance } = facts
58 9 : if (!balance || balance.effectiveBalanceAuthoritative === false) {
59 0 : return { kind: 'unavailable' }
60 : }
61 9 : const availableCents = balance.effectiveBalanceMicros ?? balance.amountMicros
62 :
63 9 : if (
64 : facts.planKind === 'noPlan' ||
65 : facts.planKind === 'ended' ||
66 : facts.periodCreditGrant?.state === 'none'
67 : ) {
68 9 : return { kind: 'noPlan', availableCents }
69 : }
70 :
71 0 : const overdrawn = availableCents < 0
72 9 : const planCents = overdrawn
73 : ? 0
74 : : (balance.cloudCreditDisplayBalanceCents ??
75 : balance.cloudCreditBalanceMicros ??
76 : 0)
77 9 : const topUpCents = overdrawn ? 0 : (balance.prepaidBalanceMicros ?? 0)
78 9 : return {
79 : kind: 'plan',
80 : availableCents,
81 : planCents: facts.billingStatus === 'paused' ? 0 : planCents,
82 : planPoolCredits: planPoolCreditsFor(facts),
83 : topUpCents,
84 : topUpInUse: planCents === 0 && topUpCents > 0,
85 : showUsage: facts.billingStatus
86 : ? PLAN_USAGE_SHOWN[facts.billingStatus]
87 : : true,
88 : planDate: planDateFor(facts)
89 : }
90 : }
91 :
92 : export interface AddCreditsEmphasisFacts {
93 : availableCents: number | null
94 : canTopUp: boolean
95 : autoTopUpStatus: AutoReloadSettings['status'] | null
96 : planActionIsPrimary: boolean
97 : }
98 :
99 : /** White when out of credits, auto top-up won't refill, and no plan action already takes white. */
100 7 : export function addCreditsEmphasis(
101 : facts: AddCreditsEmphasisFacts
102 : ): 'secondary' | 'inverted' {
103 : const outOfCredits =
104 : facts.availableCents !== null && facts.availableCents <= 0
105 7 : return outOfCredits &&
106 : facts.canTopUp &&
107 : facts.autoTopUpStatus !== 'on' &&
108 : !facts.planActionIsPrimary
109 : ? 'inverted'
110 : : 'secondary'
111 : }
112 :
113 0 : function planPoolCreditsFor(facts: CreditsCardFacts): number | null {
114 0 : const grant = facts.periodCreditGrant
115 0 : if (!grant) return facts.planPoolCredits
116 0 : return grant.state === 'amount' ? (grant.credits ?? null) : null
117 : }
118 :
119 0 : function planDateFor(facts: CreditsCardFacts): PlanCreditsState['planDate'] {
120 0 : if (facts.billingStatus === 'paused') return { kind: 'afterPayment' }
121 0 : switch (facts.planKind) {
122 : case 'ending':
123 0 : return { kind: 'availableUntil', date: facts.endDate }
124 : default:
125 0 : return { kind: 'refills', date: facts.nextCreditRefillAt }
126 : }
127 : }
|