Line data Source code
1 : import type { AutoReloadSettings, AutoTopUpSetup } from '@comfyorg/ingest-types'
2 :
3 : export type AutoTopUpCardState =
4 : | { kind: 'loading' }
5 : | { kind: 'unavailable' }
6 : | { kind: 'loaded'; settings: AutoReloadSettings }
7 :
8 : export interface AutoTopUpCardFacts {
9 : isLoading: boolean
10 : settings: AutoReloadSettings | null
11 : }
12 :
13 : /** Served settings win over an in-flight refresh; no settings after a read is unavailable. */
14 13 : export function deriveAutoTopUpCard(
15 : facts: AutoTopUpCardFacts
16 : ): AutoTopUpCardState {
17 13 : if (facts.settings) return { kind: 'loaded', settings: facts.settings }
18 13 : return facts.isLoading ? { kind: 'loading' } : { kind: 'unavailable' }
19 : }
20 :
21 : export type AutoTopUpSection =
22 : | { kind: 'card' }
23 : | { kind: 'memberBlock' }
24 : | { kind: 'hidden' }
25 :
26 : export interface AutoTopUpSectionFacts {
27 : autoTopUpEnabled: boolean
28 : canViewAutoTopUpCard: boolean
29 : canViewAutoTopUpMemberNotice: boolean
30 : }
31 :
32 : /** The card when `can_view_auto_top_up` is served true, else the member block when `can_view_auto_top_up_member_notice` is. */
33 5 : export function deriveAutoTopUpSection(
34 : facts: AutoTopUpSectionFacts
35 : ): AutoTopUpSection {
36 5 : if (!facts.autoTopUpEnabled) return { kind: 'hidden' }
37 2 : if (facts.canViewAutoTopUpCard) return { kind: 'card' }
38 5 : return facts.canViewAutoTopUpMemberNotice
39 : ? { kind: 'memberBlock' }
40 : : { kind: 'hidden' }
41 : }
42 :
43 : export interface AutoTopUpSetupFacts {
44 : autoTopUpEnabled: boolean
45 : canManageAutoTopUp: boolean
46 : setup: AutoTopUpSetup | null
47 : }
48 :
49 2356 : const PAUSED_PLAN_REASONS: ReadonlySet<AutoTopUpSetup['reason']> = new Set([
50 : 'payment_past_due',
51 : 'billing_paused'
52 : ])
53 :
54 : /** Auto top-up is hidden on every surface while setup is unavailable or frozen for a paused plan. */
55 12 : export function isAutoTopUpNotAvailable(setup: AutoTopUpSetup | null): boolean {
56 12 : if (setup === null) return false
57 12 : if (setup.state === 'unavailable') return true
58 12 : return setup.state === 'frozen' && PAUSED_PLAN_REASONS.has(setup.reason)
59 : }
60 :
61 : /** Platform Billing's Turn on block: served `auto_top_up_enabled` and `can_manage_auto_top_up`, and a served setup that is not Not available. */
62 0 : export function canSetUpAutoTopUp(facts: AutoTopUpSetupFacts): boolean {
63 0 : if (!facts.autoTopUpEnabled || !facts.canManageAutoTopUp) return false
64 0 : return facts.setup !== null && !isAutoTopUpNotAvailable(facts.setup)
65 : }
66 :
67 : export type AutoTopUpOffView =
68 : | 'off'
69 : | 'noPaymentMethod'
70 : | 'noDefaultPaymentMethod'
71 :
72 : export interface AutoTopUpOffViewFacts {
73 : setup: AutoTopUpSetup | null
74 : savedPaymentMethodCount: number | null
75 : }
76 :
77 : /** An off card frozen for a missing default asks for a card when none is saved, else for a default. */
78 8 : export function autoTopUpOffView(
79 : facts: AutoTopUpOffViewFacts
80 : ): AutoTopUpOffView {
81 : const needsDefault =
82 : facts.setup?.state === 'frozen' &&
83 : facts.setup.reason === 'default_payment_method_missing'
84 8 : if (!needsDefault) return 'off'
85 8 : return facts.savedPaymentMethodCount === 0
86 : ? 'noPaymentMethod'
87 : : 'noDefaultPaymentMethod'
88 : }
|