From e7ba6c7c34ceae395cd59337d965ee74b672e799 Mon Sep 17 00:00:00 2001 From: kunish Date: Sun, 17 Sep 2023 19:44:47 +0800 Subject: [PATCH] fix: subscriptionInfo may not be valid --- src/components/SubscriptionInfo.tsx | 17 ++++++++--------- src/types/index.d.ts | 11 ++++++----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/components/SubscriptionInfo.tsx b/src/components/SubscriptionInfo.tsx index f9cd05dd..89a97351 100644 --- a/src/components/SubscriptionInfo.tsx +++ b/src/components/SubscriptionInfo.tsx @@ -3,21 +3,20 @@ import dayjs from 'dayjs' import type { SubscriptionInfo as ISubscriptionInfo } from '~/types' const getSubscriptionsInfo = (subscriptionInfo: ISubscriptionInfo) => { - const total = byteSize(subscriptionInfo.Total, { units: 'iec' }) - const used = byteSize(subscriptionInfo.Download + subscriptionInfo.Upload, { + const { Download = 0, Upload = 0, Total = 0, Expire = 0 } = subscriptionInfo + + const total = byteSize(Total, { units: 'iec' }) + const used = byteSize(Download + Upload, { units: 'iec', }) - const percentage = ( - ((subscriptionInfo.Download + subscriptionInfo.Upload) / - subscriptionInfo.Total) * - 100 - ).toFixed(2) + const percentage = (((Download + Upload) / Total) * 100).toFixed(2) + const expireStr = () => { if (subscriptionInfo.Expire === 0) { return 'Null' } - return dayjs(subscriptionInfo.Expire * 1000).format('YYYY-MM-DD') + return dayjs(Expire * 1000).format('YYYY-MM-DD') } return { @@ -29,7 +28,7 @@ const getSubscriptionsInfo = (subscriptionInfo: ISubscriptionInfo) => { } export const SubscriptionInfo = (props: { - subscriptionInfo: ISubscriptionInfo + subscriptionInfo?: ISubscriptionInfo }) => { if (!props.subscriptionInfo) { return diff --git a/src/types/index.d.ts b/src/types/index.d.ts index d1554ce1..d84d70e6 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -38,15 +38,16 @@ export type ProxyNode = { delay: number }[] } + export type SubscriptionInfo = { - Download: number - Upload: number - Total: number - Expire: number + Download?: number + Upload?: number + Total?: number + Expire?: number } export type ProxyProvider = { - subscriptionInfo: SubscriptionInfo + subscriptionInfo?: SubscriptionInfo name: string proxies: ProxyNode[] testUrl: string