metacubexd/src/components/SubscriptionInfo.tsx

51 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-09-01 12:40:34 +08:00
import byteSize from 'byte-size'
import dayjs from 'dayjs'
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',
})
2023-09-17 19:44:47 +08:00
const percentage = (((Download + Upload) / Total) * 100).toFixed(2)
2023-09-01 12:40:34 +08:00
const expireStr = () => {
if (subscriptionInfo.Expire === 0) {
return 'Null'
}
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,
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>
2023-09-01 12:40:34 +08:00
<div class="text-sm text-slate-500">Expire: {info.expireStr()} </div>
</>
)
}