Line data Source code
1 : <template>
2 4 : <div
3 : :class="cn('flex flex-col gap-4 px-4 pt-1 pb-3', className)"
4 : data-testid="credits-breakdown"
5 : >
6 : <div v-if="state.kind !== 'noPlan'" class="flex flex-col gap-2 pt-2">
7 : <div class="flex items-start justify-between gap-2 text-sm">
8 : <span class="text-muted-foreground">{{ copy.planCredits }}</span>
9 : <div v-if="loading" :class="cn(SKELETON, 'w-18')" />
10 : <span
11 : v-else
12 : class="text-muted-foreground tabular-nums"
13 : data-testid="credits-breakdown-plan"
14 : >{{ planLine.before
15 : }}<span class="font-semibold text-foreground">{{
16 : planLine.value
17 : }}</span
18 : >{{ planLine.after }}</span
19 : >
20 : </div>
21 : <div
22 : v-if="loading || usedPercent !== null"
23 : v-bind="meterAttrs"
24 : class="h-1 overflow-hidden rounded-full bg-accent"
25 : >
26 : <div
27 : class="h-full rounded-full bg-foreground"
28 : :style="{ width: `${usedPercent ?? 0}%` }"
29 : />
30 : </div>
31 : <p
32 : v-if="planNote"
33 : :role="state.kind === 'unavailable' ? 'alert' : undefined"
34 : class="m-0 text-xs text-muted-foreground"
35 : data-testid="credits-breakdown-plan-note"
36 : >
37 : {{ planNote }}
38 : </p>
39 : </div>
40 : <div class="flex flex-col gap-2 pt-2">
41 : <div class="flex items-start justify-between gap-2 text-sm">
42 : <span class="text-muted-foreground">{{ copy.topUpCredits }}</span>
43 : <div v-if="loading" :class="cn(SKELETON, 'w-10')" />
44 : <span
45 : v-else
46 : class="font-semibold text-foreground tabular-nums"
47 : data-testid="credits-breakdown-top-up"
48 : >
49 : {{ topUp }}
50 : </span>
51 : </div>
52 : <slot name="topUpNote" />
53 : </div>
54 : </div>
55 : </template>
56 :
57 : <script setup lang="ts">
58 : import { computed } from 'vue'
59 : import type { HTMLAttributes } from 'vue'
60 :
61 : import type { CreditsFlyoutState } from '@comfyorg/account-core/billing'
62 : import { cn } from '@comfyorg/tailwind-utils'
63 :
64 : import {
65 : centsToCredits,
66 : formatWholeCredits,
67 : planDateLine,
68 : planUsedPercent,
69 : splitPlanOfPool
70 : } from './creditsCardCopy'
71 : import type { CreditsBreakdownCopy } from './creditsCardCopy'
72 :
73 : const {
74 : state,
75 : copy,
76 : locale,
77 : class: className
78 : } = defineProps<{
79 : state: CreditsFlyoutState
80 : copy: CreditsBreakdownCopy
81 : locale: string
82 : class?: HTMLAttributes['class']
83 : }>()
84 :
85 2355 : const SKELETON = 'h-3 animate-pulse rounded-sm bg-secondary'
86 :
87 4 : const loading = computed(() => state.kind === 'loading')
88 4 : const plan = computed(() => (state.kind === 'plan' ? state : null))
89 :
90 4 : const credits = (cents: number) =>
91 : formatWholeCredits(centsToCredits(cents), locale)
92 :
93 4 : const planLine = computed(() => {
94 0 : if (!plan.value) return { before: '', value: '—', after: '' }
95 0 : const value = credits(plan.value.planCents)
96 0 : const total = plan.value.planPoolCredits
97 0 : if (!total) return { before: '', value, after: '' }
98 0 : return {
99 : ...splitPlanOfPool(copy.planOfPool, formatWholeCredits(total, locale)),
100 : value
101 : }
102 : })
103 :
104 4 : const usedPercent = computed(() =>
105 : plan.value ? planUsedPercent(plan.value) : null
106 : )
107 :
108 4 : const meterAttrs = computed<HTMLAttributes>(() =>
109 : usedPercent.value === null
110 : ? { 'aria-hidden': true }
111 : : {
112 : role: 'meter',
113 : 'aria-label': copy.planCredits,
114 : 'aria-valuemin': 0,
115 : 'aria-valuemax': 100,
116 : 'aria-valuenow': usedPercent.value
117 : }
118 : )
119 :
120 4 : const planNote = computed(() => {
121 0 : if (state.kind === 'unavailable') return copy.unavailable
122 0 : return plan.value
123 : ? planDateLine(plan.value, copy, locale, plan.value.cadence)
124 : : ''
125 : })
126 :
127 4 : const topUp = computed(() =>
128 : state.kind === 'plan' || state.kind === 'noPlan'
129 : ? credits(state.topUpCents)
130 : : '—'
131 : )
132 : </script>
|