forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTvlChart.tsx
More file actions
137 lines (128 loc) · 3.59 KB
/
Copy pathTvlChart.tsx
File metadata and controls
137 lines (128 loc) · 3.59 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
"use client";
import { useEffect, useState } from "react";
import { Box, Skeleton, Text, useBreakpointValue } from "@chakra-ui/react";
import {
AreaChart,
Area,
XAxis,
YAxis,
Tooltip,
ResponsiveContainer,
} from "recharts";
import { sorobanService } from "@/lib/soroban";
const ACCENT = "#4ae292";
interface TvlDataPoint {
date: string;
tvl: string;
truncated?: boolean;
}
interface TvlChartProps {
poolId: string;
}
export default function TvlChart({ poolId }: TvlChartProps) {
const [data, setData] = useState<TvlDataPoint[]>([]);
const [loading, setLoading] = useState(true);
// Issue #214: a fixed 180px is tall relative to viewport width on small
// phones; shrink it below the sm breakpoint instead.
const chartHeight = useBreakpointValue({ base: 140, sm: 180 }, { fallback: "sm" }) ?? 180;
useEffect(() => {
let cancelled = false;
setLoading(true);
sorobanService
.getPoolHistory(poolId, 7)
.then((history) => {
if (!cancelled) {
setData(history);
setLoading(false);
}
})
.catch(() => {
if (!cancelled) setLoading(false);
});
return () => {
cancelled = true;
};
}, [poolId]);
if (loading) {
return <Skeleton height={`${chartHeight}px`} borderRadius="2xl" startColor="app.border" endColor="app.surfaceHover" />;
}
if (data.length === 0) {
return (
<Box
h={`${chartHeight}px`}
display="flex"
alignItems="center"
justifyContent="center"
borderRadius="2xl"
border="1px solid"
borderColor="app.border"
>
<Text color="app.muted" fontSize="sm">
No TVL history available
</Text>
</Box>
);
}
return (
<ResponsiveContainer width="100%" height={chartHeight}>
<AreaChart data={data} margin={{ top: 8, right: 8, left: 0, bottom: 0 }}>
<defs>
<linearGradient id="tvlGradient" x1="0" y1="0" x2="0" y2="1">
<stop offset="5%" stopColor={ACCENT} stopOpacity={0.3} />
<stop offset="95%" stopColor={ACCENT} stopOpacity={0} />
</linearGradient>
</defs>
<XAxis
dataKey="date"
tick={{ fontSize: 11, fill: "#A2A2A2" }}
tickFormatter={(v: string) =>
new Date(v).toLocaleDateString(undefined, {
month: "short",
day: "numeric",
})
}
axisLine={false}
tickLine={false}
/>
<YAxis
tick={{ fontSize: 11, fill: "#A2A2A2" }}
tickFormatter={(v: number) =>
v >= 1_000_000
? `$${(v / 1_000_000).toFixed(1)}M`
: v >= 1_000
? `$${(v / 1_000).toFixed(0)}K`
: `$${v}`
}
axisLine={false}
tickLine={false}
width={56}
/>
<Tooltip
contentStyle={{
background: "#171717",
border: "1px solid #333",
borderRadius: "12px",
fontSize: "12px",
}}
labelFormatter={(label) =>
new Date(String(label)).toLocaleDateString(undefined, {
weekday: "short",
month: "short",
day: "numeric",
})
}
formatter={(value) => [`$${Number(value).toLocaleString()}`, "TVL"]}
/>
<Area
type="monotone"
dataKey="tvl"
stroke={ACCENT}
strokeWidth={2}
fill="url(#tvlGradient)"
dot={false}
activeDot={{ r: 4, fill: ACCENT }}
/>
</AreaChart>
</ResponsiveContainer>
);
}