forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeAnalysis.tsx
More file actions
138 lines (127 loc) · 4.3 KB
/
Copy pathTimeAnalysis.tsx
File metadata and controls
138 lines (127 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { useState } from "react";
import {
BarChart,
Bar,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
ResponsiveContainer,
} from "recharts";
import type { TimeDistribution } from "../../types/analytics";
import { useTheme } from "../ThemeContext";
interface TimeAnalysisProps {
data: TimeDistribution[];
}
type ViewMode = "dayOfWeek" | "monthly";
function formatCurrency(value: number): string {
return `$${value.toLocaleString("en-US", { minimumFractionDigits: 0, maximumFractionDigits: 0 })}`;
}
function cssVar(name: string): string {
return getComputedStyle(document.documentElement)
.getPropertyValue(name)
.trim();
}
export function TimeAnalysis({ data }: TimeAnalysisProps) {
const [view, setView] = useState<ViewMode>("dayOfWeek");
const { resolvedTheme } = useTheme();
const borderColor = cssVar("--color-border");
const mutedColor = cssVar("--color-text-muted");
const surfaceColor = cssVar("--color-surface");
const cardColor = cssVar("--color-card");
const textColor = cssVar("--color-text");
const peakDay = data.reduce(
(max, d) => (d.count > max.count ? d : max),
data[0],
);
return (
<div
className="bg-card-theme rounded-lg shadow border border-theme p-6"
id="time-analysis"
>
<div className="flex items-center justify-between mb-4">
<h2 className="text-xl font-bold text-theme">Time Analysis</h2>
{/* Toggle pill */}
<div className="flex bg-surface rounded-lg p-0.5 border border-theme">
<button
className="px-3 py-1 text-sm rounded-md transition"
style={{
backgroundColor: view === "dayOfWeek" ? cardColor : "transparent",
color: view === "dayOfWeek" ? textColor : mutedColor,
boxShadow:
view === "dayOfWeek" ? "0 1px 2px rgba(0,0,0,0.15)" : "none",
}}
onClick={() => setView("dayOfWeek")}
>
Day of Week
</button>
<button
className="px-3 py-1 text-sm rounded-md transition"
style={{
backgroundColor: view === "monthly" ? cardColor : "transparent",
color: view === "monthly" ? textColor : mutedColor,
boxShadow:
view === "monthly" ? "0 1px 2px rgba(0,0,0,0.15)" : "none",
}}
onClick={() => setView("monthly")}
>
By Amount
</button>
</div>
</div>
<ResponsiveContainer width="100%" height={280}>
<BarChart data={data}>
<CartesianGrid
strokeDasharray="3 3"
stroke={borderColor}
vertical={false}
/>
<XAxis
dataKey="label"
tick={{ fill: mutedColor, fontSize: 12 }}
axisLine={{ stroke: borderColor }}
/>
<YAxis
tickFormatter={view === "monthly" ? formatCurrency : undefined}
tick={{ fill: mutedColor, fontSize: 12 }}
axisLine={{ stroke: borderColor }}
/>
<Tooltip
// eslint-disable-next-line @typescript-eslint/no-explicit-any
formatter={(value: any, name: any) => {
const v = Number(value ?? 0);
return [
name === "amount" ? formatCurrency(v) : v,
name === "amount" ? "Amount" : "Transactions",
];
}}
contentStyle={{
backgroundColor: surfaceColor,
border: `1px solid ${borderColor}`,
borderRadius: "0.5rem",
boxShadow:
resolvedTheme === "dark"
? "0 1px 3px rgba(0,0,0,0.4)"
: "0 1px 3px rgba(0,0,0,0.1)",
color: textColor,
}}
labelStyle={{ color: textColor }}
/>
<Bar
dataKey={view === "monthly" ? "amount" : "count"}
fill="#8b5cf6"
radius={[4, 4, 0, 0]}
animationDuration={800}
/>
</BarChart>
</ResponsiveContainer>
{peakDay && (
<p className="mt-3 text-sm text-muted-theme">
Peak day:{" "}
<span className="font-medium text-theme">{peakDay.label}</span> with{" "}
{peakDay.count} transactions ({formatCurrency(peakDay.amount)})
</p>
)}
</div>
);
}