|
| 1 | +<script setup lang="ts"> |
| 2 | +const { t } = useContent() |
| 3 | +const { usage, loading, fetchUsage, toggleOverage } = useUsage() |
| 4 | +const { billingState } = useBilling() |
| 5 | +const config = useRuntimeConfig() |
| 6 | +const billingEnabled = computed(() => config.public.billingEnabled === 'true') |
| 7 | +const toast = useToast() |
| 8 | +
|
| 9 | +defineProps<{ |
| 10 | + workspaceId: string |
| 11 | +}>() |
| 12 | +
|
| 13 | +const togglingKey = ref<string | null>(null) |
| 14 | +
|
| 15 | +const hasSubscription = computed(() => |
| 16 | + ['subscribed', 'trial_active', 'past_due', 'canceled'].includes(billingState.value), |
| 17 | +) |
| 18 | +
|
| 19 | +// Can toggle overages only with active subscription and billing enabled |
| 20 | +const canToggleOverage = computed(() => hasSubscription.value && billingEnabled.value) |
| 21 | +
|
| 22 | +onMounted(() => { |
| 23 | + fetchUsage() |
| 24 | +}) |
| 25 | +
|
| 26 | +async function handleToggle(settingsKey: string, enabled: boolean) { |
| 27 | + togglingKey.value = settingsKey |
| 28 | + try { |
| 29 | + await toggleOverage(settingsKey, enabled) |
| 30 | + } |
| 31 | + catch (err: unknown) { |
| 32 | + toast.error(resolveApiError(err, t('common.server_error'))) |
| 33 | + } |
| 34 | + finally { |
| 35 | + togglingKey.value = null |
| 36 | + } |
| 37 | +} |
| 38 | +
|
| 39 | +function formatMonth(period: string): string { |
| 40 | + const [year, month] = period.split('-') |
| 41 | + const date = new Date(Number(year), Number(month) - 1) |
| 42 | + return date.toLocaleDateString('en-US', { month: 'long', year: 'numeric' }) |
| 43 | +} |
| 44 | +
|
| 45 | +/** Icon per category */ |
| 46 | +function categoryIcon(key: string): string { |
| 47 | + switch (key) { |
| 48 | + case 'ai_messages': return 'icon-[annon--message-dots]' |
| 49 | + case 'form_submissions': return 'icon-[annon--file-text]' |
| 50 | + case 'cdn_bandwidth': return 'icon-[annon--globe]' |
| 51 | + case 'media_storage': return 'icon-[annon--image]' |
| 52 | + case 'api_messages': return 'icon-[annon--code]' |
| 53 | + default: return 'icon-[annon--chart-bar]' |
| 54 | + } |
| 55 | +} |
| 56 | +</script> |
| 57 | + |
| 58 | +<template> |
| 59 | + <div class="space-y-4"> |
| 60 | + <!-- Section header --> |
| 61 | + <div class="flex items-center justify-between"> |
| 62 | + <h3 class="text-sm font-medium text-heading dark:text-secondary-100"> |
| 63 | + {{ t('billing.usage_title') }} |
| 64 | + </h3> |
| 65 | + <span v-if="usage" class="text-xs text-muted"> |
| 66 | + {{ formatMonth(usage.billingPeriod) }} |
| 67 | + </span> |
| 68 | + </div> |
| 69 | + |
| 70 | + <!-- Loading state --> |
| 71 | + <div v-if="loading && !usage" class="flex items-center justify-center py-8"> |
| 72 | + <span class="icon-[annon--loader] size-5 animate-spin text-muted" /> |
| 73 | + </div> |
| 74 | + |
| 75 | + <!-- Usage meters --> |
| 76 | + <div v-else-if="usage" class="space-y-3"> |
| 77 | + <div |
| 78 | + v-for="category in usage.categories" |
| 79 | + :key="category.key" |
| 80 | + class="rounded-lg border border-secondary-200 p-4 dark:border-secondary-800" |
| 81 | + > |
| 82 | + <div class="mb-3 flex items-center justify-between"> |
| 83 | + <div class="flex items-center gap-2"> |
| 84 | + <span :class="categoryIcon(category.key)" class="size-4 text-muted" aria-hidden="true" /> |
| 85 | + <span class="text-sm font-medium text-heading dark:text-secondary-100"> |
| 86 | + {{ category.name }} |
| 87 | + </span> |
| 88 | + </div> |
| 89 | + |
| 90 | + <!-- Overage toggle --> |
| 91 | + <div v-if="category.limit !== -1 && category.limit > 0"> |
| 92 | + <AtomsFormSwitch |
| 93 | + :model-value="category.overageEnabled" |
| 94 | + :disabled="!canToggleOverage || togglingKey !== null" |
| 95 | + :label="t('billing.allow_overage')" |
| 96 | + @update:model-value="handleToggle(category.key, $event)" |
| 97 | + /> |
| 98 | + </div> |
| 99 | + <span v-else class="text-xs text-success-600 dark:text-success-400"> |
| 100 | + {{ t('billing.usage_unlimited') }} |
| 101 | + </span> |
| 102 | + </div> |
| 103 | + |
| 104 | + <AtomsUsageMeter |
| 105 | + :current="category.current" |
| 106 | + :limit="category.limit" |
| 107 | + :unit="category.unit" |
| 108 | + :overage-enabled="category.overageEnabled" |
| 109 | + :overage-units="category.overageUnits" |
| 110 | + :overage-unit-price="category.overageUnitPrice" |
| 111 | + /> |
| 112 | + |
| 113 | + <!-- Limit reached warning (overage disabled) --> |
| 114 | + <div |
| 115 | + v-if="category.percentage >= 100 && !category.overageEnabled && category.limit > 0" |
| 116 | + class="mt-2 rounded-md bg-danger-50 px-3 py-2 dark:bg-danger-950/30" |
| 117 | + > |
| 118 | + <p class="text-xs text-danger-700 dark:text-danger-300"> |
| 119 | + {{ t('billing.overage_disabled') }} — {{ category.name }} {{ t('billing.limit_reached') }} |
| 120 | + </p> |
| 121 | + </div> |
| 122 | + |
| 123 | + <!-- Approaching limit warning --> |
| 124 | + <div |
| 125 | + v-else-if="category.percentage >= 80 && category.percentage < 100" |
| 126 | + class="mt-2 rounded-md bg-warning-50 px-3 py-2 dark:bg-warning-950/30" |
| 127 | + > |
| 128 | + <p class="text-xs text-warning-700 dark:text-warning-300"> |
| 129 | + {{ category.percentage }}% {{ t('billing.usage_percentage') }} |
| 130 | + </p> |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + |
| 135 | + <!-- Overage summary --> |
| 136 | + <div |
| 137 | + v-if="usage && (usage.totalOverageAmount > 0 || usage.projectedOverageAmount > 0)" |
| 138 | + class="rounded-lg border border-secondary-200 bg-secondary-50 p-4 dark:border-secondary-800 dark:bg-secondary-900" |
| 139 | + > |
| 140 | + <div class="space-y-2"> |
| 141 | + <div v-if="usage.totalOverageAmount > 0" class="flex items-center justify-between text-sm"> |
| 142 | + <span class="text-heading dark:text-secondary-100"> |
| 143 | + {{ t('billing.total_overage') }} |
| 144 | + </span> |
| 145 | + <span class="font-medium tabular-nums text-danger-600 dark:text-danger-400"> |
| 146 | + ${{ usage.totalOverageAmount.toFixed(2) }} |
| 147 | + </span> |
| 148 | + </div> |
| 149 | + <div v-if="usage.projectedOverageAmount > 0" class="flex items-center justify-between text-sm"> |
| 150 | + <span class="text-muted"> |
| 151 | + {{ t('billing.projected_overage') }} |
| 152 | + </span> |
| 153 | + <span class="tabular-nums text-muted"> |
| 154 | + ~${{ usage.projectedOverageAmount.toFixed(2) }} |
| 155 | + </span> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + |
| 160 | + <!-- No overages --> |
| 161 | + <div |
| 162 | + v-else-if="usage" |
| 163 | + class="rounded-lg border border-dashed border-secondary-200 px-4 py-3 text-center dark:border-secondary-800" |
| 164 | + > |
| 165 | + <p class="text-xs text-muted"> |
| 166 | + {{ t('billing.no_overage') }} |
| 167 | + </p> |
| 168 | + </div> |
| 169 | + </div> |
| 170 | +</template> |
0 commit comments