✨ feat: Add network statistics reset function
This commit is contained in:
parent
59a7ab046b
commit
efca5c6f2c
1 changed files with 45 additions and 14 deletions
|
|
@ -10,12 +10,16 @@ import {
|
||||||
Image,
|
Image,
|
||||||
Music,
|
Music,
|
||||||
Network,
|
Network,
|
||||||
PauseCircle, Upload,
|
PauseCircle,
|
||||||
|
Upload,
|
||||||
Video,
|
Video,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { request } from "@/lib/api";
|
import { request, telegramApi, type TelegramApiArg } from "@/lib/api";
|
||||||
import prettyBytes from "pretty-bytes";
|
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 defining the structure of the data returned from the API
|
||||||
interface StatisticsData {
|
interface StatisticsData {
|
||||||
|
|
@ -42,11 +46,22 @@ interface FileStatisticsProps {
|
||||||
|
|
||||||
const FileStatistics: React.FC<FileStatisticsProps> = ({ telegramId }) => {
|
const FileStatistics: React.FC<FileStatisticsProps> = ({ telegramId }) => {
|
||||||
// Use SWR for data fetching and caching
|
// Use SWR for data fetching and caching
|
||||||
const { data, error } = useSWR<StatisticsData, Error>(
|
const { data, error, mutate } = useSWR<StatisticsData, Error>(
|
||||||
`/telegram/${telegramId}/download-statistics`,
|
`/telegram/${telegramId}/download-statistics`,
|
||||||
request,
|
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
|
// Render an error message if the API call fails
|
||||||
if (error) {
|
if (error) {
|
||||||
return (
|
return (
|
||||||
|
|
@ -193,26 +208,42 @@ const FileStatistics: React.FC<FileStatisticsProps> = ({ telegramId }) => {
|
||||||
<span>Network Statistics</span>
|
<span>Network Statistics</span>
|
||||||
</h3>
|
</h3>
|
||||||
<div className="mt-4 h-full">
|
<div className="mt-4 h-full">
|
||||||
<div className="flex justify-center items-center space-x-2 rounded-lg bg-gray-50 p-4 shadow-sm">
|
<div className="flex items-center justify-center space-x-2 rounded-lg bg-gray-50 p-4 shadow-sm">
|
||||||
<Upload className="h-5 w-5 text-yellow-500" />
|
<Upload className="h-5 w-5 text-yellow-500" />
|
||||||
<span className="text-lg font-semibold text-gray-800">
|
<span className="text-lg font-semibold text-gray-800">
|
||||||
{prettyBytes(data.networkStatistics.sentBytes)}
|
{prettyBytes(data.networkStatistics.sentBytes)}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="mt-4 flex justify-center items-center space-x-2 rounded-lg bg-gray-50 p-4 shadow-sm">
|
<div className="mt-4 flex items-center justify-center space-x-2 rounded-lg bg-gray-50 p-4 shadow-sm">
|
||||||
<Download className="h-5 w-5 text-blue-600" />
|
<Download className="h-5 w-5 text-blue-600" />
|
||||||
<span className="text-lg font-semibold text-gray-800">
|
<span className="text-lg font-semibold text-gray-800">
|
||||||
{prettyBytes(data.networkStatistics.receivedBytes)}
|
{prettyBytes(data.networkStatistics.receivedBytes)}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<p className="mt-2 text-right text-sm text-gray-400">
|
<div className="mt-4 flex items-center justify-between">
|
||||||
Since: {formatDistanceToNow(
|
<Button
|
||||||
new Date(data.networkStatistics.sinceDate * 1000),
|
variant="outline"
|
||||||
{
|
size="sm"
|
||||||
addSuffix: true,
|
disabled={isResetMutating}
|
||||||
},
|
onClick={() => {
|
||||||
)}
|
void triggerReset({
|
||||||
</p>
|
data: {},
|
||||||
|
method: "ResetNetworkStatistics",
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{isResetMutating ? "Resetting..." : "Reset"}
|
||||||
|
</Button>
|
||||||
|
<p className="text-right text-sm text-gray-400">
|
||||||
|
Since:{" "}
|
||||||
|
{formatDistanceToNow(
|
||||||
|
new Date(data.networkStatistics.sinceDate * 1000),
|
||||||
|
{
|
||||||
|
addSuffix: true,
|
||||||
|
},
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue