From efca5c6f2c6bf4610da819fc5b4415ca31bf92af Mon Sep 17 00:00:00 2001 From: jarvis2f <137974272+jarvis2f@users.noreply.github.com> Date: Tue, 24 Dec 2024 13:56:04 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Add=20network=20statistics?= =?UTF-8?q?=20reset=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/components/file-statistics.tsx | 59 ++++++++++++++++++++------ 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/web/src/components/file-statistics.tsx b/web/src/components/file-statistics.tsx index fe96750..9b828fa 100644 --- a/web/src/components/file-statistics.tsx +++ b/web/src/components/file-statistics.tsx @@ -10,12 +10,16 @@ import { Image, Music, Network, - PauseCircle, Upload, + PauseCircle, + Upload, Video, } from "lucide-react"; -import { request } from "@/lib/api"; +import { request, telegramApi, type TelegramApiArg } from "@/lib/api"; import prettyBytes from "pretty-bytes"; -import { formatDistanceToNow } from "date-fns"; // Define a fetcher function to handle the API request +import { formatDistanceToNow } from "date-fns"; +import { Button } from "@/components/ui/button"; +import useSWRMutation from "swr/mutation"; +import type { TelegramApiResult } from "@/lib/types"; // Define a fetcher function to handle the API request // Interface defining the structure of the data returned from the API interface StatisticsData { @@ -42,11 +46,22 @@ interface FileStatisticsProps { const FileStatistics: React.FC = ({ telegramId }) => { // Use SWR for data fetching and caching - const { data, error } = useSWR( + const { data, error, mutate } = useSWR( `/telegram/${telegramId}/download-statistics`, request, ); + const { trigger: triggerReset, isMutating: isResetMutating } = useSWRMutation< + TelegramApiResult, + Error, + string, + TelegramApiArg + >("/telegram/api", telegramApi, { + onSuccess: () => { + void mutate(); + }, + }); + // Render an error message if the API call fails if (error) { return ( @@ -193,26 +208,42 @@ const FileStatistics: React.FC = ({ telegramId }) => { Network Statistics
-
+
{prettyBytes(data.networkStatistics.sentBytes)}
-
+
{prettyBytes(data.networkStatistics.receivedBytes)}
-

- Since: {formatDistanceToNow( - new Date(data.networkStatistics.sinceDate * 1000), - { - addSuffix: true, - }, - )} -

+
+ +

+ Since:{" "} + {formatDistanceToNow( + new Date(data.networkStatistics.sinceDate * 1000), + { + addSuffix: true, + }, + )} +

+