2023-09-01 12:40:34 +08:00
|
|
|
import byteSize from 'byte-size'
|
|
|
|
import dayjs from 'dayjs'
|
2023-09-17 21:18:23 +08:00
|
|
|
import { toFinite } from 'lodash'
|
2023-09-22 17:14:35 +08:00
|
|
|
import { useI18n } from '~/i18n'
|
2023-09-03 03:26:29 +08:00
|
|
|
import type { SubscriptionInfo as ISubscriptionInfo } from '~/types'
|
2023-09-01 12:40:34 +08:00
|
|
|
|
2023-09-03 03:26:29 +08:00
|
|
|
const getSubscriptionsInfo = (subscriptionInfo: ISubscriptionInfo) => {
|
2023-09-17 19:44:47 +08:00
|
|
|
const { Download = 0, Upload = 0, Total = 0, Expire = 0 } = subscriptionInfo
|
|
|
|
|
|
|
|
const total = byteSize(Total, { units: 'iec' })
|
|
|
|
const used = byteSize(Download + Upload, {
|
2023-09-02 10:44:13 +08:00
|
|
|
units: 'iec',
|
|
|
|
})
|
2023-09-17 21:18:23 +08:00
|
|
|
const percentage = toFinite((((Download + Upload) / Total) * 100).toFixed(2))
|
2023-09-17 19:44:47 +08:00
|
|
|
|
2023-09-18 13:45:15 +08:00
|
|
|
const expirePrefix = () => {
|
2023-09-23 01:38:36 +08:00
|
|
|
const [t] = useI18n()
|
2023-09-18 13:45:15 +08:00
|
|
|
|
|
|
|
return t('expire')
|
|
|
|
}
|
|
|
|
|
2023-09-01 12:40:34 +08:00
|
|
|
const expireStr = () => {
|
2023-09-23 01:38:36 +08:00
|
|
|
const [t] = useI18n()
|
2023-09-18 13:45:15 +08:00
|
|
|
|
2023-09-17 21:18:23 +08:00
|
|
|
if (Expire === 0) {
|
2023-09-18 13:45:15 +08:00
|
|
|
return t('noExpire')
|
2023-09-01 12:40:34 +08:00
|
|
|
}
|
|
|
|
|
2023-09-17 19:44:47 +08:00
|
|
|
return dayjs(Expire * 1000).format('YYYY-MM-DD')
|
2023-09-01 12:40:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
total,
|
|
|
|
used,
|
|
|
|
percentage,
|
2023-09-18 13:45:15 +08:00
|
|
|
expirePrefix,
|
2023-09-01 12:40:34 +08:00
|
|
|
expireStr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-03 03:26:29 +08:00
|
|
|
export const SubscriptionInfo = (props: {
|
2023-09-17 19:44:47 +08:00
|
|
|
subscriptionInfo?: ISubscriptionInfo
|
2023-09-03 03:26:29 +08:00
|
|
|
}) => {
|
2023-09-01 12:40:34 +08:00
|
|
|
if (!props.subscriptionInfo) {
|
|
|
|
return
|
|
|
|
}
|
2023-09-03 15:30:56 +08:00
|
|
|
|
2023-09-01 12:40:34 +08:00
|
|
|
const info = getSubscriptionsInfo(props.subscriptionInfo)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-09-01 13:45:30 +08:00
|
|
|
<progress class="progress" value={info.percentage} max="100" />
|
|
|
|
|
2023-09-01 12:40:34 +08:00
|
|
|
<div class="text-sm text-slate-500">
|
|
|
|
{`${info.used}`} / {`${info.total}`} ( {info.percentage}% )
|
|
|
|
</div>
|
2023-09-01 13:45:30 +08:00
|
|
|
|
2023-09-18 13:45:15 +08:00
|
|
|
<div class="text-sm text-slate-500">
|
|
|
|
{info.expirePrefix()}: {info.expireStr()}
|
|
|
|
</div>
|
2023-09-01 12:40:34 +08:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|