2023-08-15 08:45:24 +00:00
|
|
|
'use client';
|
|
|
|
|
|
2024-03-21 01:39:14 +00:00
|
|
|
import type { HTMLAttributes } from 'react';
|
2023-08-15 08:45:24 +00:00
|
|
|
|
|
|
|
|
import { Bar, BarChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts';
|
|
|
|
|
|
|
|
|
|
import { formatMonth } from '@documenso/lib/client-only/format-month';
|
|
|
|
|
|
2023-09-22 14:08:25 +00:00
|
|
|
export type BarMetricProps<T extends Record<string, unknown>> = HTMLAttributes<HTMLDivElement> & {
|
|
|
|
|
data: T;
|
|
|
|
|
metricKey: keyof T[string];
|
2023-08-15 09:43:24 +00:00
|
|
|
title: string;
|
|
|
|
|
label: string;
|
|
|
|
|
chartHeight?: number;
|
2023-09-23 12:02:37 +00:00
|
|
|
extraInfo?: JSX.Element;
|
2023-08-15 09:43:24 +00:00
|
|
|
};
|
2023-08-15 08:45:24 +00:00
|
|
|
|
2023-09-22 14:08:25 +00:00
|
|
|
export const BarMetric = <T extends Record<string, Record<keyof T[string], unknown>>>({
|
2023-08-15 09:43:24 +00:00
|
|
|
className,
|
|
|
|
|
data,
|
|
|
|
|
metricKey,
|
|
|
|
|
title,
|
|
|
|
|
label,
|
|
|
|
|
chartHeight = 400,
|
2023-09-23 12:02:37 +00:00
|
|
|
extraInfo,
|
2023-08-15 09:43:24 +00:00
|
|
|
...props
|
2023-09-22 14:08:25 +00:00
|
|
|
}: BarMetricProps<T>) => {
|
2023-08-15 08:45:24 +00:00
|
|
|
const formattedData = Object.keys(data)
|
|
|
|
|
.map((key) => ({
|
|
|
|
|
month: formatMonth(key),
|
2023-08-15 09:43:24 +00:00
|
|
|
[metricKey]: data[key][metricKey],
|
2023-08-15 08:45:24 +00:00
|
|
|
}))
|
|
|
|
|
.reverse();
|
|
|
|
|
|
|
|
|
|
return (
|
2024-03-21 01:39:14 +00:00
|
|
|
<div className={className} {...props}>
|
|
|
|
|
<div className="border-border flex flex-col justify-center rounded-2xl border p-6 pl-2 shadow-sm hover:shadow">
|
|
|
|
|
<div className="mb-6 flex px-4">
|
|
|
|
|
<h3 className="text-lg font-semibold">{title}</h3>
|
|
|
|
|
<span>{extraInfo}</span>
|
|
|
|
|
</div>
|
2023-08-15 08:45:24 +00:00
|
|
|
|
2023-08-15 09:43:24 +00:00
|
|
|
<ResponsiveContainer width="100%" height={chartHeight}>
|
2023-11-05 12:48:05 +11:00
|
|
|
<BarChart data={formattedData}>
|
2023-08-15 08:45:24 +00:00
|
|
|
<XAxis dataKey="month" />
|
|
|
|
|
<YAxis />
|
|
|
|
|
<Tooltip
|
2023-10-19 15:30:36 +05:30
|
|
|
labelStyle={{
|
|
|
|
|
color: 'hsl(var(--primary-foreground))',
|
|
|
|
|
}}
|
2023-08-15 08:45:24 +00:00
|
|
|
itemStyle={{
|
|
|
|
|
color: 'hsl(var(--primary-foreground))',
|
|
|
|
|
}}
|
2023-08-15 09:43:24 +00:00
|
|
|
formatter={(value) => [Number(value), label]}
|
2023-08-15 08:45:24 +00:00
|
|
|
cursor={{ fill: 'hsl(var(--primary) / 10%)' }}
|
|
|
|
|
/>
|
2023-11-05 12:48:05 +11:00
|
|
|
<Bar
|
2024-04-15 14:22:34 +05:30
|
|
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
2023-11-05 12:48:05 +11:00
|
|
|
dataKey={metricKey as string}
|
|
|
|
|
maxBarSize={60}
|
|
|
|
|
fill="hsl(var(--primary))"
|
|
|
|
|
label={label}
|
|
|
|
|
radius={[4, 4, 0, 0]}
|
|
|
|
|
/>
|
2023-08-15 08:45:24 +00:00
|
|
|
</BarChart>
|
|
|
|
|
</ResponsiveContainer>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|