-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathViews.vue
76 lines (65 loc) · 1.71 KB
/
Views.vue
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
<script setup>
import { AreaChart } from '@/components/ui/chart-area'
import { BarChart } from '@/components/ui/chart-bar'
const views = ref([])
const chart = computed(() => views.value.length > 1 ? AreaChart : BarChart)
const id = inject('id')
const time = inject('time')
const filters = inject('filters')
const OneDay = 24 * 60 * 60 // 1 day in seconds
function getUnit(startAt, endAt) {
if (startAt && endAt && endAt - startAt <= OneDay)
return 'hour'
return 'day'
}
async function getLinkViews() {
views.value = []
const { data } = await useAPI('/api/stats/views', {
query: {
id: id.value,
unit: getUnit(time.value.startAt, time.value.endAt),
clientTimezone: getTimeZone(),
startAt: time.value.startAt,
endAt: time.value.endAt,
...filters.value,
},
})
views.value = (data || []).map((item) => {
item.visitors = +item.visitors
item.visits = +item.visits
return item
})
}
const stopWatchQueryChange = watch([time, filters], getLinkViews, {
deep: true,
})
onMounted(async () => {
getLinkViews()
})
onBeforeUnmount(() => {
stopWatchQueryChange()
})
function formatTime(tick) {
if (Number.isInteger(tick) && views.value[tick]) {
if (getUnit(time.value.startAt, time.value.endAt) === 'hour')
return views.value[tick].time.split(' ')[1] || ''
return views.value[tick].time
}
return ''
}
</script>
<template>
<Card class="px-0 py-6 md:px-6">
<CardTitle class="px-6 md:px-0">
{{ $t('dashboard.views') }}
</CardTitle>
<component
:is="chart"
:data="views"
index="time"
:categories="['visitors', 'visits']"
:x-formatter="formatTime"
:y-formatter="formatNumber"
/>
</Card>
</template>