Line data Source code
1 : import type {
2 : BillingCapabilitiesResponse,
3 : BillingStatus,
4 : BillingStatusResponse,
5 : SubscriptionDuration,
6 : SubscriptionState
7 : } from '@comfyorg/ingest-types'
8 :
9 : type BillingSubscriptionStatus = NonNullable<
10 : BillingStatusResponse['subscription_status']
11 : >
12 :
13 : type BillingCadence = 'monthly' | 'yearly'
14 :
15 : export interface PlanRowFacts {
16 : subscriptionStatus: BillingSubscriptionStatus | null
17 : subscriptionState: SubscriptionState | null
18 : billingStatus: BillingStatus | null
19 : duration: SubscriptionDuration | null
20 : planName: string | null
21 : renewalDate: string | null
22 : endDate: string | null
23 : endedAt?: string | null
24 : scheduledChange: { planName: string; effectiveAt: string } | null
25 : freeRuns: { remaining: number; total: number } | null
26 : planKind: PlanKind | null
27 : }
28 :
29 : export type PlanRowState =
30 : | { kind: 'activating'; planName: string }
31 : | { kind: 'paused'; planName: string }
32 : | {
33 : kind: 'active'
34 : planName: string
35 : cadence: BillingCadence
36 : nextInvoice: string
37 : scheduledChange: { planName: string; effectiveAt: string } | null
38 : }
39 : | {
40 : kind: 'ending'
41 : planName: string
42 : cadence: BillingCadence | null
43 : endsAt: string
44 : }
45 : | {
46 : kind: 'ended'
47 : planName: string | null
48 : cadence: BillingCadence | null
49 : endedAt: string | null
50 : }
51 : | {
52 : kind: 'noPlan'
53 : freeRuns: { remaining: number; total: number } | null
54 : }
55 : | {
56 : kind: 'salesManaged'
57 : planName: string | null
58 : cadence: BillingCadence | null
59 : renewsAt: string | null
60 : }
61 : | { kind: 'unknown' }
62 :
63 : export type PlanRowAction =
64 : | 'manage'
65 : | 'subscribe'
66 : | 'choosePlan'
67 : | 'contactSales'
68 :
69 : export type PlanKind = NonNullable<BillingCapabilitiesResponse['plan_kind']>
70 :
71 2356 : export const CONTACT_SALES_URL = 'https://comfy.org/contact/'
72 :
73 : type BillingTabLoad = 'loading' | 'error' | 'ready'
74 :
75 2356 : const UNKNOWN: PlanRowState = { kind: 'unknown' }
76 :
77 9 : function toCadence(
78 : duration: SubscriptionDuration | null
79 : ): BillingCadence | null {
80 9 : if (duration === 'MONTHLY') return 'monthly'
81 0 : if (duration === 'ANNUAL') return 'yearly'
82 0 : return null
83 : }
84 :
85 9 : function endedDate(facts: PlanRowFacts): string | null {
86 9 : return facts.endedAt === undefined ? facts.endDate : facts.endedAt
87 : }
88 :
89 9 : function endedRow(facts: PlanRowFacts): PlanRowState {
90 9 : return {
91 : kind: 'ended',
92 : planName: facts.planName,
93 : cadence: toCadence(facts.duration),
94 : endedAt: endedDate(facts)
95 : }
96 : }
97 :
98 0 : function salesManagedRow(facts: PlanRowFacts): PlanRowState | null {
99 0 : if (facts.planKind !== 'sales_managed') return null
100 0 : switch (facts.billingStatus) {
101 : case 'paused':
102 0 : return null
103 : }
104 0 : switch (facts.subscriptionStatus) {
105 : case 'active':
106 0 : return {
107 : kind: 'salesManaged',
108 : planName: facts.planName,
109 : cadence: toCadence(facts.duration),
110 : renewsAt: facts.renewalDate
111 : }
112 : default:
113 0 : return null
114 : }
115 : }
116 :
117 : /** Maps the served status fields onto the plan row; whatever they cannot render is `unknown`. */
118 9 : export function derivePlanRow(facts: PlanRowFacts): PlanRowState {
119 9 : const { subscriptionStatus, planName, renewalDate, endDate } = facts
120 9 : if (facts.subscriptionState === 'activating' && planName !== null) {
121 0 : return { kind: 'activating', planName }
122 : }
123 9 : if (facts.subscriptionState === 'ended') {
124 0 : return endedRow(facts)
125 : }
126 9 : switch (subscriptionStatus) {
127 : case 'ended':
128 9 : return endedRow(facts)
129 : }
130 0 : if (subscriptionStatus === null) {
131 0 : return { kind: 'noPlan', freeRuns: facts.freeRuns }
132 : }
133 0 : const salesManaged = salesManagedRow(facts)
134 0 : if (salesManaged) return salesManaged
135 0 : if (planName === null) return UNKNOWN
136 9 : switch (facts.billingStatus) {
137 : case 'paused':
138 0 : return { kind: 'paused', planName }
139 : }
140 :
141 0 : const cadence = toCadence(facts.duration)
142 9 : switch (subscriptionStatus) {
143 : case 'active':
144 0 : if (!renewalDate || cadence === null) return UNKNOWN
145 0 : return {
146 : kind: 'active',
147 : planName,
148 : cadence,
149 : nextInvoice: renewalDate,
150 : scheduledChange: facts.scheduledChange
151 : }
152 : case 'canceled':
153 0 : return endDate
154 : ? { kind: 'ending', planName, cadence, endsAt: endDate }
155 : : UNKNOWN
156 : default:
157 0 : return UNKNOWN
158 : }
159 : }
160 :
161 : /** Self-serve actions follow `can_open_plans`. `sales_managed` is Contact sales. */
162 5 : export function planRowAction(
163 : state: PlanRowState,
164 : {
165 : canOpenPlans,
166 : planKind
167 : }: { canOpenPlans: boolean; planKind: PlanKind | null }
168 : ): PlanRowAction | null {
169 5 : if (planKind === 'sales_managed') return 'contactSales'
170 5 : if (!canOpenPlans) return null
171 5 : switch (state.kind) {
172 : case 'activating':
173 : case 'active':
174 : case 'ending':
175 : case 'paused':
176 : case 'unknown':
177 0 : return 'manage'
178 : case 'noPlan':
179 0 : return 'subscribe'
180 : case 'ended':
181 5 : return 'choosePlan'
182 : case 'salesManaged':
183 0 : return null
184 : }
185 : }
186 :
187 : interface BillingTabLoadFacts {
188 : workspaceLoadFailed: boolean
189 : billingError: string | null
190 : billingInitialized: boolean
191 : capabilitiesReady: boolean
192 : capabilitiesResolved: boolean
193 : }
194 :
195 5 : export function deriveBillingTabLoad(
196 : facts: BillingTabLoadFacts
197 : ): BillingTabLoad {
198 5 : if (
199 : facts.workspaceLoadFailed ||
200 : facts.billingError !== null ||
201 : (facts.capabilitiesReady && !facts.capabilitiesResolved)
202 : ) {
203 0 : return 'error'
204 : }
205 5 : return facts.billingInitialized && facts.capabilitiesReady
206 : ? 'ready'
207 : : 'loading'
208 : }
209 :
210 : /** Full month name, no ordinal; the year unless the plan is monthly. */
211 0 : export function formatPlanDate(
212 : isoDate: string,
213 : locale: string,
214 : cadence: BillingCadence | null
215 : ): string {
216 0 : const date = new Date(isoDate)
217 0 : if (Number.isNaN(date.getTime())) return ''
218 0 : return new Intl.DateTimeFormat(locale, {
219 : month: 'long',
220 : day: 'numeric',
221 : ...(cadence === 'monthly' ? {} : { year: 'numeric' }),
222 : timeZone: 'UTC'
223 : }).format(date)
224 : }
|