LCOV - code coverage report
Current view: top level - packages/account-ui/src/billing/addCredits - addCreditsPresentation.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 66.7 % 63 42
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import type {
       2              :   AddCreditsAmountView,
       3              :   AddCreditsBounds,
       4              :   AddCreditsCopyKey,
       5              :   AddCreditsNotice,
       6              :   AddCreditsPendingTopup,
       7              :   AddCreditsRefusal,
       8              :   AddCreditsTranslate,
       9              :   AutoTopUpDefaultCard,
      10              :   AutoTopUpFieldError,
      11              :   AutoTopUpFormValues,
      12              :   AutoTopUpRules
      13              : } from '@comfyorg/account-core/billing'
      14              : import {
      15              :   addCreditsWait,
      16              :   creditsToCents,
      17              :   smallestTopUpCents,
      18              :   amountErrorCopyKey,
      19              :   waitCopy
      20              : } from '@comfyorg/account-core/billing'
      21              : import { buttonVariants } from '@comfyorg/design-system/button.variants'
      22              : import { cn } from '@comfyorg/tailwind-utils'
      23              : 
      24              : import type { AddCreditsQuote } from './useAddCreditsDialog'
      25              : 
      26              : type AddCreditsUiSlot =
      27              :   | 'root'
      28              :   | 'header'
      29              :   | 'title'
      30              :   | 'close'
      31              :   | 'body'
      32              :   | 'presets'
      33              :   | 'preset'
      34              :   | 'line'
      35              :   | 'field'
      36              :   | 'error'
      37              :   | 'notice'
      38              :   | 'summary'
      39              :   | 'footer'
      40              :   | 'primary'
      41              :   | 'secondary'
      42              : 
      43              : export type AddCreditsUi = Partial<Record<AddCreditsUiSlot, string>>
      44              : 
      45         2356 : export const SUPPORT_TOKEN = '{support}'
      46              : 
      47              : export interface AddCreditsFormat {
      48              :   readonly money: (cents: number) => string
      49              :   readonly dollars: (cents: number) => string
      50              :   readonly count: (value: number) => string
      51              : }
      52              : 
      53            4 : export function addCreditsFormat(locale: string): AddCreditsFormat {
      54            4 :   const money = new Intl.NumberFormat(locale, {
      55              :     style: 'currency',
      56              :     currency: 'USD',
      57              :     minimumFractionDigits: 2,
      58              :     maximumFractionDigits: 2
      59              :   })
      60            4 :   const dollars = new Intl.NumberFormat(locale, {
      61              :     style: 'currency',
      62              :     currency: 'USD',
      63              :     maximumFractionDigits: 0
      64              :   })
      65            4 :   const count = new Intl.NumberFormat(locale)
      66            4 :   return {
      67              :     money: (cents) => money.format(cents / 100),
      68              :     dollars: (cents) =>
      69              :       (cents % 100 === 0 ? dollars : money).format(cents / 100),
      70              :     count: (value) => count.format(value)
      71              :   }
      72              : }
      73              : 
      74              : export interface AddCreditsCopyContext {
      75              :   readonly translate: AddCreditsTranslate
      76              :   readonly format: AddCreditsFormat
      77              : }
      78              : 
      79              : export interface AddCreditsNoticeCopy {
      80              :   readonly tone: 'warning' | 'neutral'
      81              :   readonly title: string
      82              :   readonly body: readonly string[]
      83              : }
      84              : 
      85              : export interface AddCreditsFooterCopy {
      86              :   readonly credits: string
      87              :   readonly loading: boolean
      88              :   readonly detail: string
      89              : }
      90              : 
      91              : /** A billing link's URL, or `true` when the host opens billing itself and the link is a button. */
      92              : export type AddCreditsBillingLinkTarget = string | true
      93              : 
      94           13 : export function primaryActionClass(ui: AddCreditsUi): string {
      95           13 :   return cn(
      96              :     buttonVariants({ variant: 'inverted', size: 'lg' }),
      97              :     'shrink-0 no-underline',
      98              :     ui.primary
      99              :   )
     100              : }
     101              : 
     102           14 : export function footerBarClass(ui: AddCreditsUi, layout: string): string {
     103           14 :   return cn(
     104              :     'flex shrink-0 items-center gap-2 border-x-0 border-t border-b-0 border-solid px-4 py-4',
     105              :     layout,
     106              :     ui.footer
     107              :   )
     108              : }
     109              : 
     110            8 : export function amountErrorFor(
     111              :   view: AddCreditsAmountView,
     112              :   bounds: AddCreditsBounds,
     113              :   { translate, format }: AddCreditsCopyContext
     114              : ): string | undefined {
     115            8 :   if (view.check.status !== 'invalid') return undefined
     116            8 :   const bound =
     117              :     view.check.error === 'below_min' ? bounds.minCents : bounds.maxCents
     118            8 :   return translate(amountErrorCopyKey(view.check.error), {
     119              :     amount: format.dollars(bound)
     120              :   })
     121              : }
     122              : 
     123         2356 : const REFUSAL_COPY: Record<
     124              :   AddCreditsRefusal['kind'],
     125              :   readonly [AddCreditsCopyKey, AddCreditsCopyKey]
     126              : > = {
     127              :   checkout_failed: ['checkoutFailed.title', 'checkoutFailed.body'],
     128              :   not_allowed: ['notAllowed.title', 'notAllowed.body'],
     129              :   rate_limited: ['rateLimited.title', 'rateLimited.body'],
     130              :   purchases_paused: ['purchasesPaused.title', 'purchasesPaused.body']
     131              : }
     132              : 
     133         2356 : const PENDING_COPY: Record<
     134              :   AddCreditsPendingTopup['kind'],
     135              :   readonly [AddCreditsCopyKey, AddCreditsCopyKey, AddCreditsCopyKey]
     136              : > = {
     137              :   processing: [
     138              :     'processing.title',
     139              :     'processing.titleNoAmount',
     140              :     'processing.body'
     141              :   ],
     142              :   needs_verification: [
     143              :     'needsVerification.title',
     144              :     'needsVerification.titleNoAmount',
     145              :     'needsVerification.body'
     146              :   ],
     147              :   not_verified: [
     148              :     'notVerified.title',
     149              :     'notVerified.titleNoAmount',
     150              :     'notVerified.body'
     151              :   ]
     152              : }
     153              : 
     154            1 : function splitSupport(text: string): string[] {
     155            1 :   return text.split(/(\{support\})/).filter((part) => part !== '')
     156              : }
     157              : 
     158            1 : function refusalBody(
     159              :   refusal: AddCreditsRefusal,
     160              :   bodyKey: AddCreditsCopyKey,
     161              :   translate: AddCreditsTranslate
     162              : ): string {
     163            1 :   const support = SUPPORT_TOKEN
     164            1 :   if (refusal.kind === 'checkout_failed' && refusal.serverMessage)
     165            1 :     return refusal.serverMessage
     166            0 :   if (refusal.kind !== 'rate_limited') return translate(bodyKey, { support })
     167            0 :   const wait = addCreditsWait(refusal.retryAfterSeconds)
     168            1 :   return wait.unit === 'later'
     169              :     ? translate('rateLimited.bodyNoWait', { support })
     170              :     : translate(bodyKey, { wait: waitCopy(wait, translate), support })
     171              : }
     172              : 
     173            1 : function refusalCopy(
     174              :   refusal: AddCreditsRefusal,
     175              :   translate: AddCreditsTranslate
     176              : ): AddCreditsNoticeCopy {
     177            1 :   const [titleKey, bodyKey] = REFUSAL_COPY[refusal.kind]
     178            1 :   return {
     179              :     tone: 'warning',
     180              :     title: translate(titleKey),
     181              :     body: splitSupport(refusalBody(refusal, bodyKey, translate))
     182              :   }
     183              : }
     184              : 
     185            0 : function pendingCopy(
     186              :   pending: AddCreditsPendingTopup,
     187              :   { translate, format }: AddCreditsCopyContext
     188              : ): AddCreditsNoticeCopy {
     189            0 :   const [withAmount, withoutAmount, bodyKey] = PENDING_COPY[pending.kind]
     190            0 :   return {
     191              :     tone: 'neutral',
     192              :     title:
     193              :       pending.amountCents === undefined
     194              :         ? translate(withoutAmount)
     195              :         : translate(withAmount, {
     196              :             amount: format.dollars(pending.amountCents)
     197              :           }),
     198              :     body: [translate(bodyKey)]
     199              :   }
     200              : }
     201              : 
     202            9 : export function noticeCopyFor(
     203              :   notice: AddCreditsNotice | undefined,
     204              :   context: AddCreditsCopyContext
     205              : ): AddCreditsNoticeCopy | undefined {
     206            9 :   if (notice === undefined) return undefined
     207            9 :   return notice.kind === 'refusal'
     208              :     ? refusalCopy(notice.refusal, context.translate)
     209              :     : pendingCopy(notice, context)
     210              : }
     211              : 
     212            2 : export function noticeKindOf(
     213              :   notice: AddCreditsNotice | undefined
     214              : ): string | undefined {
     215            2 :   return notice?.kind === 'refusal' ? notice.refusal.kind : notice?.kind
     216              : }
     217              : 
     218           13 : function quotedCredits(
     219              :   quote: AddCreditsQuote | undefined,
     220              :   { translate, format }: AddCreditsCopyContext
     221              : ): string {
     222           13 :   if (quote?.status === 'ok') {
     223            8 :     return translate('credits', { credits: format.count(quote.credits) })
     224              :   }
     225           13 :   return quote?.status === 'unavailable'
     226              :     ? translate('creditsUnavailable')
     227              :     : translate('credits', { credits: 0 })
     228              : }
     229              : 
     230            0 : function notVerifiedFooter(
     231              :   cents: number | undefined,
     232              :   { translate, format }: AddCreditsCopyContext
     233              : ): AddCreditsFooterCopy {
     234            0 :   return cents === undefined
     235              :     ? { credits: translate('notVerified.footer'), loading: false, detail: '' }
     236              :     : {
     237              :         credits: translate('notVerified.footerTitle', {
     238              :           amount: format.money(cents)
     239              :         }),
     240              :         loading: false,
     241              :         detail: translate('notVerified.footer')
     242              :       }
     243              : }
     244              : 
     245           13 : export function footerCopyFor(
     246              :   view: AddCreditsAmountView,
     247              :   quote: AddCreditsQuote | undefined,
     248              :   context: AddCreditsCopyContext
     249              : ): AddCreditsFooterCopy {
     250           13 :   const { notice, amountCents } = view
     251           13 :   if (notice?.kind === 'not_verified') {
     252            0 :     return notVerifiedFooter(amountCents, context)
     253              :   }
     254           13 :   const verifying = notice?.kind === 'needs_verification'
     255           13 :   if (verifying && amountCents === undefined) {
     256            0 :     return { credits: '', loading: false, detail: '' }
     257              :   }
     258           13 :   return {
     259              :     credits: quotedCredits(quote, context),
     260              :     loading: quote?.status === 'loading',
     261              :     detail: context.translate(
     262              :       verifying ? 'needsVerification.footer' : 'paidAtCheckout',
     263              :       { amount: context.format.money(amountCents ?? 0) }
     264              :     )
     265              :   }
     266              : }
     267              : 
     268            0 : export function autoTopUpFieldMessage(
     269              :   error: AutoTopUpFieldError,
     270              :   values: AutoTopUpFormValues,
     271              :   rules: AutoTopUpRules,
     272              :   { translate, format }: AddCreditsCopyContext
     273              : ): string {
     274            0 :   const threshold = values.thresholdCredits ?? 0
     275            0 :   switch (error) {
     276              :     case 'notWhole':
     277              :     case 'belowMinimum':
     278            0 :       return translate(`autoTopUp.fieldError.${error}`)
     279              :     case 'belowTarget':
     280            0 :       return translate('autoTopUp.fieldError.belowTarget', {
     281              :         minimum: format.count(threshold + rules.minReloadCredits),
     282              :         usd: format.dollars(Math.round(creditsToCents(rules.minReloadCredits)))
     283              :       })
     284              :     case 'aboveTarget': {
     285            0 :       const max = threshold + (rules.maxReloadCredits ?? 0)
     286            0 :       return translate('autoTopUp.fieldError.aboveTarget', {
     287              :         max: format.count(max),
     288              :         usd: format.dollars(Math.round(creditsToCents(max) / 100) * 100)
     289              :       })
     290              :     }
     291              :     case 'belowTopUp':
     292            0 :       return translate('autoTopUp.fieldError.belowTopUp', {
     293              :         amount: format.dollars(
     294              :           Math.ceil(
     295              :             (smallestTopUpCents(values, rules.maxReloadCredits) ?? 0) / 100
     296              :           ) * 100
     297              :         )
     298              :       })
     299              :     case 'invalid':
     300              :     case 'aboveCeiling':
     301            0 :       return translate('autoTopUp.invalid')
     302              :   }
     303              : }
     304              : 
     305         2356 : export const BILLING_TOKEN = '{billing}'
     306              : 
     307            0 : export function autoTopUpConsentParts(
     308              :   defaultCard: AutoTopUpDefaultCard | null,
     309              :   max: string,
     310              :   translate: AddCreditsTranslate
     311              : ): readonly string[] {
     312            0 :   const billing = BILLING_TOKEN
     313              :   const sentence =
     314              :     defaultCard === null
     315              :       ? translate('autoTopUp.consentNoCard', { billing, max })
     316              :       : translate('autoTopUp.consentDefaultCard', {
     317              :           billing,
     318              :           max,
     319              :           card: translate('autoTopUp.card', { ...defaultCard })
     320              :         })
     321            0 :   return sentence.split(/(\{billing\})/).filter((part) => part !== '')
     322              : }
        

Generated by: LCOV version 2.0-1