From 73819903fe4384eabb87771598f401583c6a57cb Mon Sep 17 00:00:00 2001 From: keiko233 Date: Sun, 7 Jul 2024 18:19:53 +0800 Subject: [PATCH] feat(providers): add proxy provider traffic display support --- frontend/interface/service/types.ts | 6 +++ .../providers/proxies-provider-traffic.tsx | 45 +++++++++++++++++++ .../components/providers/proxies-provider.tsx | 10 ++++- 3 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 frontend/nyanpasu/src/components/providers/proxies-provider-traffic.tsx diff --git a/frontend/interface/service/types.ts b/frontend/interface/service/types.ts index 45db58cf58..4c28781586 100644 --- a/frontend/interface/service/types.ts +++ b/frontend/interface/service/types.ts @@ -188,6 +188,12 @@ export interface ProviderItem { proxies: Clash.Proxy[]; updatedAt: string; vehicleType: string; + subscriptionInfo?: { + Upload?: number; + Download?: number; + Total?: number; + Expire?: number; + }; } export interface Traffic { diff --git a/frontend/nyanpasu/src/components/providers/proxies-provider-traffic.tsx b/frontend/nyanpasu/src/components/providers/proxies-provider-traffic.tsx new file mode 100644 index 0000000000..835e00516b --- /dev/null +++ b/frontend/nyanpasu/src/components/providers/proxies-provider-traffic.tsx @@ -0,0 +1,45 @@ +import parseTraffic from "@/utils/parse-traffic"; +import { LinearProgress, Tooltip } from "@mui/material"; +import { ProxiesProviderProps } from "./proxies-provider"; + +export const ProxiesProviderTraffic = ({ provider }: ProxiesProviderProps) => { + const calc = () => { + let progress = 0; + let total = 0; + let used = 0; + + if (provider.subscriptionInfo) { + const { + Download: download, + Upload: upload, + Total: t, + } = provider.subscriptionInfo; + + total = t ?? 0; + + used = (download ?? 0) + (upload ?? 0); + + progress = (used / (total ?? 0)) * 100; + } + + return { progress, total, used }; + }; + + const { progress, total, used } = calc(); + + return ( +
+
+ +
+ + +
+ {((used / total) * 100).toFixed(2)}% +
+
+
+ ); +}; + +export default ProxiesProviderTraffic; diff --git a/frontend/nyanpasu/src/components/providers/proxies-provider.tsx b/frontend/nyanpasu/src/components/providers/proxies-provider.tsx index d25b60f344..9bae593a0f 100644 --- a/frontend/nyanpasu/src/components/providers/proxies-provider.tsx +++ b/frontend/nyanpasu/src/components/providers/proxies-provider.tsx @@ -1,12 +1,14 @@ import { useMessage } from "@/hooks/use-notification"; +import parseTraffic from "@/utils/parse-traffic"; import { Refresh } from "@mui/icons-material"; import LoadingButton from "@mui/lab/LoadingButton"; -import { Chip, Paper } from "@mui/material"; +import { Chip, LinearProgress, Paper, Tooltip } from "@mui/material"; import { ProviderItem, useClashCore } from "@nyanpasu/interface"; import { useLockFn } from "ahooks"; import dayjs from "dayjs"; import { useState } from "react"; import { useTranslation } from "react-i18next"; +import ProxiesProviderTraffic from "./proxies-provider-traffic"; export interface ProxiesProviderProps { provider: ProviderItem; @@ -36,7 +38,7 @@ export const ProxiesProvider = ({ provider }: ProxiesProviderProps) => { return ( { + {provider.subscriptionInfo && ( + + )} +