HomeStats.vue
2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<script setup lang="ts">
import type { Period, Range, Stat } from '~/types'
const props = defineProps<{
period: Period
range: Range
}>()
const { locale, t } = useAppI18n()
function formatCurrency(value: number): string {
return value.toLocaleString(locale.value === 'zh-CN' ? 'zh-CN' : 'en-US', {
style: 'currency',
currency: 'USD',
maximumFractionDigits: 0
})
}
const baseStats = computed(() => [{
title: t('home.stats.customers'),
icon: 'i-lucide-users',
minValue: 400,
maxValue: 1000,
minVariation: -15,
maxVariation: 25
}, {
title: t('home.stats.conversions'),
icon: 'i-lucide-chart-pie',
minValue: 1000,
maxValue: 2000,
minVariation: -10,
maxVariation: 20
}, {
title: t('home.stats.revenue'),
icon: 'i-lucide-circle-dollar-sign',
minValue: 200000,
maxValue: 500000,
minVariation: -20,
maxVariation: 30,
formatter: formatCurrency
}, {
title: t('home.stats.orders'),
icon: 'i-lucide-shopping-cart',
minValue: 100,
maxValue: 300,
minVariation: -5,
maxVariation: 15
}])
const { data: stats } = await useAsyncData<Stat[]>('stats', async () => {
return baseStats.value.map((stat) => {
const value = randomInt(stat.minValue, stat.maxValue)
const variation = randomInt(stat.minVariation, stat.maxVariation)
return {
title: stat.title,
icon: stat.icon,
value: stat.formatter ? stat.formatter(value) : value,
variation
}
})
}, {
watch: [() => props.period, () => props.range, () => locale.value],
default: () => []
})
</script>
<template>
<UPageGrid class="lg:grid-cols-4 gap-4 sm:gap-6 lg:gap-px">
<UPageCard
v-for="(stat, index) in stats"
:key="index"
:icon="stat.icon"
:title="stat.title"
to="/customers"
variant="subtle"
:ui="{
container: 'gap-y-1.5',
wrapper: 'items-start',
leading: 'p-2.5 rounded-full bg-primary/10 ring ring-inset ring-primary/25 flex-col',
title: 'font-normal text-muted text-xs uppercase'
}"
class="lg:rounded-none first:rounded-l-lg last:rounded-r-lg hover:z-1"
>
<div class="flex items-center gap-2">
<span class="text-2xl font-semibold text-highlighted">
{{ stat.value }}
</span>
<UBadge
:color="stat.variation > 0 ? 'success' : 'error'"
variant="subtle"
class="text-xs"
>
{{ stat.variation > 0 ? '+' : '' }}{{ stat.variation }}%
</UBadge>
</div>
</UPageCard>
</UPageGrid>
</template>