Line data Source code
1 : <template>
2 0 : <div
3 : :class="
4 : cn('flex flex-col gap-4 rounded-lg border border-solid p-3', ui.summary)
5 : "
6 : data-testid="add-credits-auto-top-up"
7 : >
8 : <div class="flex items-start gap-3">
9 : <Checkbox
10 : :id="checkboxId"
11 : :model-value="checked"
12 : :disabled="locked"
13 : class="mt-0.5"
14 : data-testid="add-credits-auto-top-up-checkbox"
15 : @update:model-value="checked = $event === true"
16 : />
17 : <label :for="checkboxId" class="flex cursor-pointer flex-col gap-1">
18 : <span class="text-foreground">{{ translate('autoTopUp.label') }}</span>
19 : <span v-if="!checked" class="text-muted-foreground">
20 : {{ translate('autoTopUp.body') }}
21 : </span>
22 : </label>
23 : </div>
24 :
25 : <template v-if="checked">
26 : <AddCreditsAmountField
27 : input-test-id="add-credits-auto-top-up-threshold"
28 : note-test-id="add-credits-auto-top-up-threshold-note"
29 : :input="input.threshold"
30 : :label="translate('autoTopUp.threshold')"
31 : :error="messageFor('threshold')"
32 : :hint="translate('autoTopUp.thresholdHint')"
33 : :locked
34 : :ui
35 : @type="emit('edit', 'threshold', $event)"
36 : />
37 : <AddCreditsAmountField
38 : input-test-id="add-credits-auto-top-up-target"
39 : note-test-id="add-credits-auto-top-up-target-note"
40 : :input="input.target"
41 : :label="translate('autoTopUp.target')"
42 : :error="messageFor('target')"
43 : :hint="targetHint"
44 : :locked
45 : :ui
46 : @type="emit('edit', 'target', $event)"
47 : />
48 : <div class="flex flex-col gap-2">
49 : <div class="flex items-center justify-between gap-3">
50 : <label :for="limitSwitchId" class="text-muted-foreground">
51 : {{ translate('autoTopUp.limit') }}
52 : </label>
53 : <Switch
54 : :id="limitSwitchId"
55 : :model-value="input.limitOn"
56 : :disabled="locked"
57 : data-testid="add-credits-auto-top-up-limit-switch"
58 : @update:model-value="emit('edit', 'limitOn', $event)"
59 : />
60 : </div>
61 : <AddCreditsAmountField
62 : v-if="input.limitOn"
63 : input-test-id="add-credits-auto-top-up-limit"
64 : note-test-id="add-credits-auto-top-up-limit-note"
65 : prefix="$"
66 : :input="input.limit"
67 : :label="translate('autoTopUp.limit')"
68 : :error="messageFor('limit')"
69 : :hint="translate('autoTopUp.limitHint', { max: maxLimit })"
70 : :locked
71 : :ui
72 : @type="emit('edit', 'limit', $event)"
73 : />
74 : <p v-else class="m-0 text-muted-foreground">
75 : {{ translate('autoTopUp.limitOff', { max: maxLimit }) }}
76 : </p>
77 : </div>
78 : <p
79 : class="m-0 text-muted-foreground"
80 : data-testid="add-credits-auto-top-up-consent"
81 : >
82 0 : <template v-for="(part, index) in consent" :key="index">
83 : <AddCreditsBillingLink
84 : v-if="part === BILLING_TOKEN"
85 : :to="billingUrl"
86 : rel="noopener noreferrer"
87 : inline
88 : @click="emit('openBilling')"
89 : >{{ translate('autoTopUp.billing')
90 : }}<i
91 : class="ml-0.5 icon-[lucide--arrow-up-right] size-3"
92 : aria-hidden="true"
93 : /></AddCreditsBillingLink>
94 : <template v-else>{{ part }}</template>
95 : </template>
96 : </p>
97 : </template>
98 : </div>
99 : </template>
100 :
101 : <script setup lang="ts">
102 : import { computed, useId } from 'vue'
103 :
104 : import type {
105 : AddCreditsTranslate,
106 : AutoTopUpDefaultCard,
107 : AutoTopUpErrors,
108 : AutoTopUpField,
109 : AutoTopUpFormInput,
110 : AutoTopUpFormValues,
111 : AutoTopUpRules
112 : } from '@comfyorg/account-core/billing'
113 : import { eachTopUpAtLeastCents } from '@comfyorg/account-core/billing'
114 : import Checkbox from '@comfyorg/design-system/checkbox/Checkbox'
115 : import Switch from '@comfyorg/design-system/switch/Switch'
116 : import { cn } from '@comfyorg/tailwind-utils'
117 :
118 : import AddCreditsAmountField from './AddCreditsAmountField.vue'
119 : import AddCreditsBillingLink from './AddCreditsBillingLink.vue'
120 : import type {
121 : AddCreditsBillingLinkTarget,
122 : AddCreditsFormat,
123 : AddCreditsUi
124 : } from './addCreditsPresentation'
125 : import type { AutoTopUpEdit } from './useAddCreditsAutoTopUp'
126 : import {
127 : BILLING_TOKEN,
128 : autoTopUpConsentParts,
129 : autoTopUpFieldMessage
130 : } from './addCreditsPresentation'
131 :
132 : const {
133 : rules,
134 : defaultCard,
135 : input,
136 : values,
137 : errors,
138 : billingUrl,
139 : locked,
140 : translate,
141 : format,
142 : ui
143 : } = defineProps<{
144 : rules: AutoTopUpRules
145 : defaultCard: AutoTopUpDefaultCard | null
146 : input: AutoTopUpFormInput
147 : values: AutoTopUpFormValues
148 : errors: AutoTopUpErrors
149 : billingUrl: AddCreditsBillingLinkTarget
150 : locked: boolean
151 : translate: AddCreditsTranslate
152 : format: AddCreditsFormat
153 : ui: AddCreditsUi
154 : }>()
155 :
156 0 : const checked = defineModel<boolean>('checked', { required: true })
157 :
158 0 : const emit = defineEmits<{
159 : edit: AutoTopUpEdit
160 : openBilling: []
161 : }>()
162 :
163 0 : const checkboxId = useId()
164 0 : const limitSwitchId = useId()
165 :
166 0 : const maxLimit = computed(() => format.dollars(rules.maxMonthlyBudgetCents))
167 :
168 0 : const targetHint = computed(() => {
169 0 : const cents = eachTopUpAtLeastCents(values)
170 0 : return cents === null || values.targetCredits === null
171 : ? undefined
172 : : translate('autoTopUp.targetHint', {
173 : target: format.count(values.targetCredits),
174 : amount: format.money(cents)
175 : })
176 : })
177 :
178 0 : const consentMax = computed(() =>
179 : format.dollars(values.budgetCents ?? rules.maxMonthlyBudgetCents)
180 : )
181 :
182 0 : const consent = computed(() =>
183 : autoTopUpConsentParts(defaultCard, consentMax.value, translate)
184 : )
185 :
186 0 : function messageFor(field: AutoTopUpField): string | undefined {
187 0 : const error = errors[field]
188 0 : return error === undefined
189 : ? undefined
190 : : autoTopUpFieldMessage(error, values, rules, { translate, format })
191 : }
192 : </script>
|