|
| 1 | +<script lang="ts" setup> |
| 2 | +import { NFlex, NNumberAnimation, NProgress, NStatistic } from 'naive-ui'; |
| 3 | +import { computed, onMounted, ref, toRefs, watch } from 'vue'; |
| 4 | +
|
| 5 | +import TextField from '~/components/common/typography/TextField.vue'; |
| 6 | +import { useI18n } from '~/utils/i18n.utils'; |
| 7 | +
|
| 8 | +const props = defineProps({ |
| 9 | + rating: { |
| 10 | + type: Number, |
| 11 | + required: false, |
| 12 | + default: 0, |
| 13 | + }, |
| 14 | + votes: { |
| 15 | + type: Number, |
| 16 | + required: false, |
| 17 | + default: 0, |
| 18 | + }, |
| 19 | + score: { |
| 20 | + type: Number, |
| 21 | + required: false, |
| 22 | + default: 0, |
| 23 | + }, |
| 24 | + duration: { |
| 25 | + type: Number, |
| 26 | + required: false, |
| 27 | + default: 1000, |
| 28 | + }, |
| 29 | + precision: { |
| 30 | + type: Number, |
| 31 | + required: false, |
| 32 | + default: 0, |
| 33 | + }, |
| 34 | +}); |
| 35 | +
|
| 36 | +const i18n = useI18n('panel', 'ratings'); |
| 37 | +
|
| 38 | +const { votes, rating, score } = toRefs(props); |
| 39 | +
|
| 40 | +const votesUnit = computed(() => { |
| 41 | + if (votes?.value >= 10000) return 'k'; |
| 42 | + if (votes?.value >= 1000000) return 'm'; |
| 43 | + if (votes?.value >= 1000000000) return 'b'; |
| 44 | + return null; |
| 45 | +}); |
| 46 | +const _votes = computed(() => { |
| 47 | + if (!votes?.value || !votesUnit.value) return votes?.value ?? 0; |
| 48 | + if (votesUnit.value === 'k') return votes.value / 10000; |
| 49 | + if (votesUnit.value === 'm') return votes.value / 1000000; |
| 50 | + if (votesUnit.value === 'b') return votes.value / 1000000000; |
| 51 | + return votes?.value ?? 0; |
| 52 | +}); |
| 53 | +
|
| 54 | +const _rating = computed(() => (rating?.value ?? 0) * 10); |
| 55 | +const _ratingProgress = ref(0); |
| 56 | +
|
| 57 | +const _score = computed(() => (score?.value ?? 0) * 10); |
| 58 | +const _scoreProgress = ref(0); |
| 59 | +
|
| 60 | +onMounted(() => { |
| 61 | + watch( |
| 62 | + _rating, |
| 63 | + val => { |
| 64 | + _ratingProgress.value = val; |
| 65 | + }, |
| 66 | + { immediate: true }, |
| 67 | + ); |
| 68 | + watch( |
| 69 | + _score, |
| 70 | + val => { |
| 71 | + _scoreProgress.value = val; |
| 72 | + }, |
| 73 | + { immediate: true }, |
| 74 | + ); |
| 75 | +}); |
| 76 | +</script> |
| 77 | + |
| 78 | +<template> |
| 79 | + <NFlex class="rating-container" align="center" justify="center" vertical> |
| 80 | + <TextField :label="i18n('label_votes')" vertical size="small" flex="0 1 auto"> |
| 81 | + <NStatistic class="statistics" tabular-nums> |
| 82 | + <NNumberAnimation |
| 83 | + :from="0" |
| 84 | + :to="_votes" |
| 85 | + :duration="duration" |
| 86 | + :precision="votesUnit ? 2 : 0" |
| 87 | + /> |
| 88 | + <span v-if="votesUnit" class="unit">{{ votesUnit }}</span> |
| 89 | + </NStatistic> |
| 90 | + </TextField> |
| 91 | + <TextField :label="i18n('label_rating')" vertical flex="0 1 auto"> |
| 92 | + <NProgress |
| 93 | + class="progress" |
| 94 | + type="circle" |
| 95 | + :percentage="_ratingProgress" |
| 96 | + :style="{ '--duration': `${duration}ms` }" |
| 97 | + > |
| 98 | + <NStatistic class="statistics" tabular-nums> |
| 99 | + <NNumberAnimation |
| 100 | + :from="0" |
| 101 | + :to="_rating" |
| 102 | + :duration="duration" |
| 103 | + :precision="precision" |
| 104 | + /> |
| 105 | + </NStatistic> |
| 106 | + </NProgress> |
| 107 | + </TextField> |
| 108 | + <TextField :label="i18n('label_score')" vertical flex="0 1 auto"> |
| 109 | + <NProgress |
| 110 | + class="progress" |
| 111 | + type="circle" |
| 112 | + :percentage="_scoreProgress" |
| 113 | + :style="{ '--duration': `${duration}ms` }" |
| 114 | + > |
| 115 | + <NStatistic class="statistics" tabular-nums> |
| 116 | + <NNumberAnimation |
| 117 | + :from="0" |
| 118 | + :to="_pStores" |
| 119 | + :duration="duration" |
| 120 | + :precision="precision" |
| 121 | + /> |
| 122 | + </NStatistic> |
| 123 | + </NProgress> |
| 124 | + </TextField> |
| 125 | + </NFlex> |
| 126 | +</template> |
| 127 | + |
| 128 | +<style lang="scss" scoped> |
| 129 | +.rating-container { |
| 130 | + --duration: 1000ms; |
| 131 | +
|
| 132 | + gap: 1rem; |
| 133 | + padding: 1rem; |
| 134 | +
|
| 135 | + .progress { |
| 136 | + width: 3rem; |
| 137 | +
|
| 138 | + :deep(path.n-progress-graph-circle-fill) { |
| 139 | + transition: |
| 140 | + opacity var(--duration) var(--n-bezier), |
| 141 | + stroke var(--duration) var(--n-bezier), |
| 142 | + stroke-dasharray var(--duration) var(--n-bezier); |
| 143 | + } |
| 144 | + } |
| 145 | +
|
| 146 | + .statistics { |
| 147 | + --n-value-font-size: 1rem !important; |
| 148 | +
|
| 149 | + font-variant-numeric: tabular-nums; |
| 150 | + align-self: center; |
| 151 | +
|
| 152 | + .unit { |
| 153 | + padding-left: 0.125rem; |
| 154 | + color: var(--white-50); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | +</style> |
0 commit comments