2024-04-05 17:49:32 +00:00
|
|
|
import { DateTime } from 'luxon';
|
|
|
|
|
import { Bar, BarChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts';
|
2024-05-29 08:26:58 +00:00
|
|
|
import type { TooltipProps } from 'recharts';
|
|
|
|
|
import type { NameType, ValueType } from 'recharts/types/component/DefaultTooltipContent';
|
2024-04-05 17:49:32 +00:00
|
|
|
|
|
|
|
|
import type { GetUserWithDocumentMonthlyGrowth } from '@documenso/lib/server-only/admin/get-users-stats';
|
|
|
|
|
|
2025-01-02 15:33:37 +11:00
|
|
|
export type AdminStatsUsersWithDocumentsChartProps = {
|
2024-04-05 17:49:32 +00:00
|
|
|
className?: string;
|
2024-05-21 09:28:23 +00:00
|
|
|
title: string;
|
2024-04-05 17:49:32 +00:00
|
|
|
data: GetUserWithDocumentMonthlyGrowth;
|
2024-05-21 09:28:23 +00:00
|
|
|
completed?: boolean;
|
2024-05-21 22:53:31 +00:00
|
|
|
tooltip?: string;
|
2024-04-05 17:49:32 +00:00
|
|
|
};
|
|
|
|
|
|
2024-05-29 08:26:58 +00:00
|
|
|
const CustomTooltip = ({
|
|
|
|
|
active,
|
|
|
|
|
payload,
|
|
|
|
|
label,
|
|
|
|
|
tooltip,
|
|
|
|
|
}: TooltipProps<ValueType, NameType> & { tooltip?: string }) => {
|
|
|
|
|
if (active && payload && payload.length) {
|
|
|
|
|
return (
|
2025-01-02 15:33:37 +11:00
|
|
|
<div className="z-100 w-60 space-y-1 rounded-md border border-solid bg-white p-2 px-3">
|
2024-05-29 08:26:58 +00:00
|
|
|
<p className="">{label}</p>
|
|
|
|
|
<p className="text-documenso">
|
|
|
|
|
{`${tooltip} : `}
|
|
|
|
|
<span className="text-black">{payload[0].value}</span>
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-02 15:33:37 +11:00
|
|
|
export const AdminStatsUsersWithDocumentsChart = ({
|
2024-05-21 09:28:23 +00:00
|
|
|
className,
|
|
|
|
|
data,
|
|
|
|
|
title,
|
|
|
|
|
completed = false,
|
2024-05-21 22:53:31 +00:00
|
|
|
tooltip,
|
2025-01-02 15:33:37 +11:00
|
|
|
}: AdminStatsUsersWithDocumentsChartProps) => {
|
2024-05-21 09:28:23 +00:00
|
|
|
const formattedData = (data: GetUserWithDocumentMonthlyGrowth, completed: boolean) => {
|
2024-05-21 22:44:44 +00:00
|
|
|
return [...data].reverse().map(({ month, count, signed_count }) => {
|
|
|
|
|
const formattedMonth = DateTime.fromFormat(month, 'yyyy-MM').toFormat('LLL');
|
|
|
|
|
if (completed) {
|
|
|
|
|
return {
|
|
|
|
|
month: formattedMonth,
|
|
|
|
|
count: Number(signed_count),
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
month: formattedMonth,
|
|
|
|
|
count: Number(count),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-05-21 09:28:23 +00:00
|
|
|
};
|
2024-04-05 17:49:32 +00:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={className}>
|
|
|
|
|
<div className="border-border flex flex-1 flex-col justify-center rounded-2xl border p-6 pl-2">
|
2024-05-21 09:28:23 +00:00
|
|
|
<div className="mb-6 flex h-12 px-4">
|
|
|
|
|
<h3 className="text-lg font-semibold">{title}</h3>
|
2024-04-05 17:49:32 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<ResponsiveContainer width="100%" height={400}>
|
2024-07-05 04:02:22 +00:00
|
|
|
<BarChart data={formattedData(data, completed)}>
|
2024-04-05 17:49:32 +00:00
|
|
|
<XAxis dataKey="month" />
|
|
|
|
|
<YAxis />
|
|
|
|
|
|
|
|
|
|
<Tooltip
|
2024-05-29 08:26:58 +00:00
|
|
|
content={<CustomTooltip tooltip={tooltip} />}
|
2024-04-05 17:49:32 +00:00
|
|
|
labelStyle={{
|
|
|
|
|
color: 'hsl(var(--primary-foreground))',
|
|
|
|
|
}}
|
|
|
|
|
cursor={{ fill: 'hsl(var(--primary) / 10%)' }}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<Bar
|
2024-05-21 22:44:44 +00:00
|
|
|
dataKey="count"
|
2024-04-05 17:49:32 +00:00
|
|
|
fill="hsl(var(--primary))"
|
|
|
|
|
radius={[4, 4, 0, 0]}
|
|
|
|
|
maxBarSize={60}
|
2024-05-21 22:53:31 +00:00
|
|
|
label={tooltip}
|
2024-04-05 17:49:32 +00:00
|
|
|
/>
|
|
|
|
|
</BarChart>
|
|
|
|
|
</ResponsiveContainer>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|