2023-06-06 18:32:48 +03:00
|
|
|
"use client";
|
2023-06-05 18:58:59 +03:00
|
|
|
import prettyBytes from "pretty-bytes";
|
2023-08-07 15:13:41 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-06-15 12:52:46 +03:00
|
|
|
|
2023-10-05 18:50:02 +03:00
|
|
|
import Card, { CardBody, CardHeader } from "@/lib/components/ui/Card";
|
|
|
|
import Spinner from "@/lib/components/ui/Spinner";
|
|
|
|
import { useUserData } from "@/lib/hooks/useUserData";
|
2023-06-15 12:52:46 +03:00
|
|
|
import { UserStats } from "@/lib/types/User";
|
|
|
|
|
2023-06-05 18:58:59 +03:00
|
|
|
import { BrainConsumption } from "./BrainConsumption";
|
|
|
|
import { DateComponent } from "./Date";
|
2023-06-06 18:32:48 +03:00
|
|
|
import BrainSpaceChart from "./Graphs/BrainSpaceChart";
|
|
|
|
import { RequestsPerDayChart } from "./Graphs/RequestsPerDayChart";
|
2023-06-05 18:58:59 +03:00
|
|
|
|
2023-10-05 18:50:02 +03:00
|
|
|
export const formatBrainSizeUsage = (
|
|
|
|
currentBrainSize: number,
|
|
|
|
maxBrainSize: number
|
|
|
|
): string => {
|
|
|
|
const sizeInUse = prettyBytes(maxBrainSize - currentBrainSize, {
|
|
|
|
binary: true,
|
|
|
|
});
|
2023-06-15 12:52:46 +03:00
|
|
|
|
2023-10-05 18:50:02 +03:00
|
|
|
const totalSize = prettyBytes(maxBrainSize - 0, { binary: true });
|
|
|
|
|
|
|
|
return `${sizeInUse} / ${totalSize}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const UserStatistics = (): JSX.Element => {
|
|
|
|
const { userData }: { userData?: UserStats } = useUserData();
|
|
|
|
const { t } = useTranslation(["user"]);
|
2023-06-06 18:32:48 +03:00
|
|
|
|
2023-10-05 18:50:02 +03:00
|
|
|
if (!userData) {
|
|
|
|
return (
|
|
|
|
<div className="flex items-center justify-center">
|
|
|
|
<span>{t("fetching", { ns: "user" })}</span>
|
|
|
|
<Spinner />
|
2023-06-06 18:32:48 +03:00
|
|
|
</div>
|
2023-10-05 18:50:02 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { current_brain_size, max_brain_size, date, requests_stats } = userData;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="flex flex-col md:flex-row gap-2 w-full">
|
|
|
|
<Card className="shadow-none hover:shadow-none w-full md:w-1/4 md:self-start">
|
|
|
|
<CardHeader className="border-b-0">
|
|
|
|
<h3 className="font-semibold">{t("brainUsage")}</h3>
|
|
|
|
</CardHeader>
|
|
|
|
<CardBody className="flex justify-center items-center">
|
|
|
|
<BrainConsumption {...userData} />
|
|
|
|
</CardBody>
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
<div className="w-full md:w-3/4 flex flex-col md:flex-row gap-2">
|
|
|
|
<Card className="shadow-none hover:shadow-none w-full md:w-1/2">
|
|
|
|
<CardHeader className="border-b-0">
|
|
|
|
<h3 className="font-semibold">
|
2023-06-06 18:32:48 +03:00
|
|
|
{/* The last element corresponds to today's request_count */}
|
2023-08-21 15:05:13 +03:00
|
|
|
{t("requestsCount", {
|
|
|
|
count: requests_stats.at(-1)?.daily_requests_count,
|
|
|
|
})}
|
2023-10-05 18:50:02 +03:00
|
|
|
</h3>
|
|
|
|
<p className="text-slate-500 font-light text-sm">
|
|
|
|
<DateComponent date={date} />
|
|
|
|
</p>
|
|
|
|
</CardHeader>
|
2023-06-06 18:32:48 +03:00
|
|
|
|
2023-10-05 18:50:02 +03:00
|
|
|
<CardBody>
|
|
|
|
<RequestsPerDayChart {...userData} />
|
|
|
|
</CardBody>
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
<Card className="shadow-none hover:shadow-none w-full md:w-1/2">
|
|
|
|
<CardHeader className="border-b-0">
|
|
|
|
<h3 className="font-semibold">{t("brainSize")}</h3>
|
|
|
|
<p className="text-slate-500 font-light text-sm">
|
|
|
|
{formatBrainSizeUsage(current_brain_size, max_brain_size)}
|
2023-06-06 18:32:48 +03:00
|
|
|
</p>
|
2023-10-05 18:50:02 +03:00
|
|
|
</CardHeader>
|
|
|
|
<CardBody>
|
2023-06-06 18:32:48 +03:00
|
|
|
<BrainSpaceChart
|
|
|
|
current_brain_size={current_brain_size}
|
|
|
|
max_brain_size={max_brain_size}
|
|
|
|
/>
|
2023-10-05 18:50:02 +03:00
|
|
|
</CardBody>
|
|
|
|
</Card>
|
2023-06-06 18:32:48 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default UserStatistics;
|