Line data Source code
1 6 : import type { BillingStatus } from '@comfyorg/ingest-types'
2 :
3 : export type BillingStatusBannerKind =
4 : | 'paused'
5 : | 'planEnded'
6 : | 'outOfCredits'
7 : | 'ending'
8 : | 'planChange'
9 :
10 : export type BillingStatusBannerAudience = 'team' | 'personal'
11 :
12 : export type EnterpriseEndingNotice = 'scheduled' | 'started'
13 :
14 : export interface BillingStatusBannerFacts {
15 : billingControlEnabled: boolean
16 : audience: BillingStatusBannerAudience | null
17 : enterpriseEndingNotice: EnterpriseEndingNotice | null
18 : isLoaded: boolean
19 : billingStatus: BillingStatus | null
20 : canAccessSubscriptionFeatures: boolean
21 : hasFunds: boolean | null
22 : isCancelled: boolean
23 : endDate: string | null
24 : canManage: boolean
25 : isPlanEnded: boolean
26 : isPlanTerminal: boolean
27 : planEndedDismissed: boolean
28 : outOfCreditsDismissed: boolean
29 : planChangeDismissed: boolean
30 : hasScheduledChange: boolean
31 : topUpDeniedPastDue: boolean
32 : runDeniedPastDue: boolean
33 : unifiedBillingSettingsEnabled: boolean
34 : isActive: boolean
35 : }
36 :
37 : function deriveEnterpriseBanner(
38 : facts: BillingStatusBannerFacts
39 : ): BillingStatusBannerKind | null {
40 : if (!facts.canAccessSubscriptionFeatures) return null
41 : if (!facts.billingControlEnabled) return null
42 : return facts.enterpriseEndingNotice === 'started' ? 'ending' : null
43 : }
44 :
45 : function deriveLifecycleNotice(
46 : facts: BillingStatusBannerFacts,
47 : audience: BillingStatusBannerAudience
48 : ): BillingStatusBannerKind | null {
49 : if (facts.isCancelled && facts.endDate) return 'ending'
50 : const showsPlanChange =
51 : audience === 'team' &&
52 : facts.hasScheduledChange &&
53 : !facts.isCancelled &&
54 : facts.canManage &&
55 : !facts.planChangeDismissed
56 : return showsPlanChange ? 'planChange' : null
57 : }
58 :
59 : function showsPastDueOutOfCredits(facts: BillingStatusBannerFacts): boolean {
60 : return (
61 : (facts.topUpDeniedPastDue || facts.runDeniedPastDue) &&
62 : facts.hasFunds === false &&
63 : !facts.outOfCreditsDismissed
64 : )
65 : }
66 :
67 : function cloudCreditBannersOpen(facts: BillingStatusBannerFacts): boolean {
68 : if (!facts.unifiedBillingSettingsEnabled) return true
69 : return facts.isActive
70 : }
71 :
72 : function deriveNoticeBanner(
73 : facts: BillingStatusBannerFacts,
74 : audience: BillingStatusBannerAudience
75 : ): BillingStatusBannerKind | null {
76 : if (!facts.billingControlEnabled) return null
77 : const creditsOpen = cloudCreditBannersOpen(facts)
78 : if (creditsOpen && showsPastDueOutOfCredits(facts)) return 'outOfCredits'
79 : if (!facts.canAccessSubscriptionFeatures) return null
80 : if (creditsOpen && facts.hasFunds === false && !facts.outOfCreditsDismissed) {
81 : return 'outOfCredits'
82 : }
83 : return deriveLifecycleNotice(facts, audience)
84 : }
85 :
86 : function hasPlanEnded(
87 : facts: BillingStatusBannerFacts,
88 : audience: BillingStatusBannerAudience | null
89 : ): boolean {
90 : return facts.isPlanEnded || (audience === 'personal' && facts.isPlanTerminal)
91 : }
92 :
93 : /** The one billing status banner for the served billing status, plan lifecycle and credit facts. */
94 : export function deriveBillingStatusBanner(
95 : facts: BillingStatusBannerFacts
96 : ): BillingStatusBannerKind | null {
97 : if (facts.billingStatus === 'paused') return 'paused'
98 : if (!facts.isLoaded) return null
99 : const { audience } = facts
100 : if (hasPlanEnded(facts, audience)) {
101 : return facts.planEndedDismissed ? null : 'planEnded'
102 : }
103 : if (facts.enterpriseEndingNotice) return deriveEnterpriseBanner(facts)
104 : return audience ? deriveNoticeBanner(facts, audience) : null
105 : }
|