-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathuseCollectionVolume.ts
65 lines (57 loc) · 1.61 KB
/
useCollectionVolume.ts
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
import type { TimeRange } from '@/components/series/types'
import type { CollectionEntityWithVolumes } from '@/components/landing/topCollections/utils/types'
export default (
collection?: CollectionEntityWithVolumes,
timeRange?: Ref<TimeRange>,
) => {
const volume = computed(() => {
if (!collection) {
return 0
}
switch (timeRange?.value) {
case 'All':
return Number(collection.volume)
case 'Quarter':
return Number(collection.threeMonthVolume)
case 'Month':
return Number(collection.monthlyVolume)
case 'Week':
return Number(collection.weeklyVolume)
}
})
const previousVolume = computed(() => {
if (!collection) {
return 0
}
switch (timeRange?.value) {
case 'All':
return 0
case 'Quarter':
return Number(collection.threeMonthlyrangeVolume)
case 'Month':
return Number(collection.monthlyrangeVolume)
case 'Week':
return Number(collection.weeklyrangeVolume)
}
})
const diffPercent = computed(() => {
if (!volume.value || !previousVolume.value) {
return NaN
}
return (volume.value / previousVolume.value - 1) * 100
})
const sign = computed(() => {
const intSign = Math.sign(diffPercent.value)
if (isNaN(diffPercent.value) || intSign == 0) {
return ''
}
return intSign == -1 ? '-' : '+'
})
const formattedDiffPercent = computed(() => {
if (isNaN(diffPercent.value)) {
return ''
}
return `${sign.value} ${Math.abs(Math.round(diffPercent.value))}%`
})
return { volume, diffPercent, formattedDiffPercent }
}