Line data Source code
1 : import { formatPlanDate } from '@comfyorg/account-core/billing'
2 : import type {
3 : PlanRowAction,
4 : PlanRowState
5 : } from '@comfyorg/account-core/billing'
6 :
7 : /** Host-translated strings for the plan row; dates arrive already formatted. */
8 : export interface PlanRowCopy {
9 : readonly noPlanTitle: string
10 : readonly unknownTitle: string
11 : readonly salesManagedTitle: string
12 : readonly activating: (plan: string) => string
13 : readonly activeMonthly: (date: string) => string
14 : readonly activeYearly: (date: string) => string
15 : readonly scheduledChange: (plan: string, date: string) => string
16 : readonly paused: string
17 : readonly noPlanLine: string
18 : readonly freeRunsLeft: (remaining: number, total: number) => string
19 : readonly freeRunsUsed: (total: number) => string
20 : readonly salesManagedLine: string
21 : readonly salesManagedRenews: (date: string) => string
22 : readonly ending: (date: string) => string
23 : readonly ended: (date: string) => string
24 : readonly endedNoDate: string
25 : readonly managedByOwner: string
26 : readonly actions: Readonly<Record<PlanRowAction, string>>
27 : }
28 :
29 : export interface PlanRowText {
30 : title: string
31 : line: string
32 : }
33 :
34 : /** The plan row's title and status line for a served plan state. */
35 5 : export function planRowText(
36 : state: PlanRowState,
37 : copy: PlanRowCopy,
38 : locale: string,
39 : canManageSubscription: boolean
40 : ): PlanRowText {
41 5 : const status = statusLine(state, copy, locale, canManageSubscription)
42 5 : return {
43 : title: titleFor(state, copy),
44 : line: canManageSubscription
45 : ? status
46 : : [status, copy.managedByOwner].filter(Boolean).join(' ')
47 : }
48 : }
49 :
50 5 : function titleFor(state: PlanRowState, copy: PlanRowCopy): string {
51 5 : switch (state.kind) {
52 : case 'noPlan':
53 0 : return copy.noPlanTitle
54 : case 'unknown':
55 0 : return copy.unknownTitle
56 : case 'ended':
57 5 : return state.planName ?? copy.unknownTitle
58 : case 'salesManaged':
59 0 : return state.planName ?? copy.salesManagedTitle
60 : default:
61 0 : return state.planName
62 : }
63 : }
64 :
65 5 : function statusLine(
66 : state: PlanRowState,
67 : copy: PlanRowCopy,
68 : locale: string,
69 : canManageSubscription: boolean
70 : ): string {
71 5 : switch (state.kind) {
72 : case 'activating':
73 0 : return copy.activating(state.planName)
74 : case 'active':
75 0 : return activeLine(state, copy, locale)
76 : case 'paused':
77 0 : return copy.paused
78 : case 'noPlan':
79 0 : return canManageSubscription ? noPlanLine(state, copy) : ''
80 : case 'salesManaged':
81 0 : return salesManagedLine(state, copy, locale)
82 : case 'ending':
83 0 : return copy.ending(formatPlanDate(state.endsAt, locale, state.cadence))
84 : case 'ended':
85 5 : return endedLine(state, copy, locale)
86 : default:
87 0 : return ''
88 : }
89 : }
90 :
91 0 : function salesManagedLine(
92 : state: Extract<PlanRowState, { kind: 'salesManaged' }>,
93 : copy: PlanRowCopy,
94 : locale: string
95 : ): string {
96 0 : if (!state.renewsAt) return copy.salesManagedLine
97 0 : return copy.salesManagedRenews(
98 : formatPlanDate(state.renewsAt, locale, state.cadence)
99 : )
100 : }
101 :
102 5 : function endedLine(
103 : state: Extract<PlanRowState, { kind: 'ended' }>,
104 : copy: PlanRowCopy,
105 : locale: string
106 : ): string {
107 5 : if (!state.endedAt) return copy.endedNoDate
108 0 : return copy.ended(formatPlanDate(state.endedAt, locale, state.cadence))
109 : }
110 :
111 0 : function activeLine(
112 : state: Extract<PlanRowState, { kind: 'active' }>,
113 : copy: PlanRowCopy,
114 : locale: string
115 : ): string {
116 0 : const date = formatPlanDate(state.nextInvoice, locale, state.cadence)
117 0 : const billed =
118 : state.cadence === 'monthly'
119 : ? copy.activeMonthly(date)
120 : : copy.activeYearly(date)
121 0 : if (!state.scheduledChange) return billed
122 : const change = copy.scheduledChange(
123 : state.scheduledChange.planName,
124 : formatPlanDate(state.scheduledChange.effectiveAt, locale, state.cadence)
125 : )
126 0 : return `${billed} ${change}`
127 : }
128 :
129 0 : function noPlanLine(
130 : state: Extract<PlanRowState, { kind: 'noPlan' }>,
131 : copy: PlanRowCopy
132 : ): string {
133 0 : if (!state.freeRuns) return copy.noPlanLine
134 0 : return state.freeRuns.remaining > 0
135 : ? copy.freeRunsLeft(state.freeRuns.remaining, state.freeRuns.total)
136 : : copy.freeRunsUsed(state.freeRuns.total)
137 : }
|