metacubexd/src/components/SubscriptionInfo.tsx

64 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-09-01 12:40:34 +08:00
import byteSize from 'byte-size'
import dayjs from 'dayjs'
import { toFinite } from 'lodash'
2023-09-22 17:14:35 +08:00
import { useI18n } from '~/i18n'
import type { SubscriptionInfo as ISubscriptionInfo } from '~/types'
2023-09-01 12:40:34 +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',
})
const percentage = toFinite((((Download + Upload) / Total) * 100).toFixed(2))
2023-09-17 19:44:47 +08:00
const expirePrefix = () => {
const [t] = useI18n()
return t('expire')
}
2023-09-01 12:40:34 +08:00
const expireStr = () => {
const [t] = useI18n()
if (Expire === 0) {
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,
expirePrefix,
2023-09-01 12:40:34 +08:00
expireStr,
}
}
export const SubscriptionInfo = (props: {
2023-09-17 19:44:47 +08:00
subscriptionInfo?: ISubscriptionInfo
}) => {
2023-09-01 12:40:34 +08:00
if (!props.subscriptionInfo) {
return
}
2023-09-01 12:40:34 +08:00
const info = getSubscriptionsInfo(props.subscriptionInfo)
return (
<>
<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>
<div class="text-sm text-slate-500">
{info.expirePrefix()}: {info.expireStr()}
</div>
2023-09-01 12:40:34 +08:00
</>
)
}