Skip to content

Commit 9447c20

Browse files
committed
perf(webapp): stabilize chart loading line renderer
1 parent 6696a92 commit 9447c20

1 file changed

Lines changed: 48 additions & 55 deletions

File tree

apps/webapp/app/components/primitives/charts/ChartLoading.tsx

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -187,72 +187,60 @@ function ChartBarLoadingBackground() {
187187
);
188188
}
189189

190-
function ChartLineLoadingBackground() {
191-
// Generate line points with configurable starting position and constraints
192-
const generateLinePoints = (startY: number, minY: number, maxY: number) => {
193-
const numPoints = 10;
194-
const points = [];
195-
let lastY = startY;
196-
197-
for (let i = 0; i < numPoints; i++) {
198-
// Calculate x value that spreads points across the full width
199-
const x = i * (9 / (numPoints - 1));
190+
type ChartPoint = { x: number; y: number };
200191

201-
// Create less extreme variations that move smoothly
202-
const change = Math.random() * 6 - 3; // Range from -3 to +3
203-
const y = Math.max(minY, Math.min(maxY, lastY + change)); // Apply constraints
192+
function generateLinePoints(startY: number, minY: number, maxY: number): ChartPoint[] {
193+
const numPoints = 10;
194+
const points = [];
195+
let lastY = startY;
204196

205-
points.push({ x, y });
206-
lastY = y;
207-
}
197+
for (let i = 0; i < numPoints; i++) {
198+
const x = i * (9 / (numPoints - 1));
199+
const change = Math.random() * 6 - 3;
200+
const y = Math.max(minY, Math.min(maxY, lastY + change));
208201

209-
return points;
210-
};
202+
points.push({ x, y });
203+
lastY = y;
204+
}
211205

212-
// Generate points for both lines
213-
const points = useMemo(() => generateLinePoints(30, 10, 90), []);
214-
const secondPoints = useMemo(() => generateLinePoints(40, 30, 90), []);
215-
216-
const generateSmoothPath = (points: Array<{ x: number; y: number }>) => {
217-
if (points.length < 2) return "";
206+
return points;
207+
}
218208

219-
let path = `M0,${50 - points[0].y}`;
209+
function generateSmoothPath(points: ChartPoint[]) {
210+
if (points.length < 2) return "";
220211

221-
// Use curve command for smooth lines
222-
for (let i = 0; i < points.length - 1; i++) {
223-
const x1 = points[i].x;
224-
const y1 = 50 - points[i].y;
225-
const x2 = points[i + 1].x;
226-
const y2 = 50 - points[i + 1].y;
212+
let path = `M0,${50 - points[0].y}`;
227213

228-
// Bezier control points (create smooth curve)
229-
const cx1 = (x1 + x2) / 2;
230-
const cy1 = y1;
231-
const cx2 = (x1 + x2) / 2;
232-
const cy2 = y2;
214+
for (let i = 0; i < points.length - 1; i++) {
215+
const x1 = points[i].x;
216+
const y1 = 50 - points[i].y;
217+
const x2 = points[i + 1].x;
218+
const y2 = 50 - points[i + 1].y;
219+
const cx1 = (x1 + x2) / 2;
220+
const cy1 = y1;
221+
const cx2 = (x1 + x2) / 2;
222+
const cy2 = y2;
233223

234-
path += ` C${cx1},${cy1} ${cx2},${cy2} ${x2},${y2}`;
235-
}
224+
path += ` C${cx1},${cy1} ${cx2},${cy2} ${x2},${y2}`;
225+
}
236226

237-
return path;
238-
};
227+
return path;
228+
}
239229

240-
const generateAreaPath = (points: Array<{ x: number; y: number }>) => {
241-
const curvePath = generateSmoothPath(points);
242-
const lastX = 9;
243-
return `${curvePath} L${lastX},50 L0,50 Z`;
244-
};
230+
function generateAreaPath(points: ChartPoint[]) {
231+
return `${generateSmoothPath(points)} L9,50 L0,50 Z`;
232+
}
245233

246-
// Component to render a line with area fill and animation
247-
const AnimatedLine = ({
248-
points,
249-
gradientId,
250-
delay = 0,
251-
}: {
252-
points: Array<{ x: number; y: number }>;
253-
gradientId: string;
254-
delay?: number;
255-
}) => (
234+
function AnimatedLine({
235+
points,
236+
gradientId,
237+
delay = 0,
238+
}: {
239+
points: ChartPoint[];
240+
gradientId: string;
241+
delay?: number;
242+
}) {
243+
return (
256244
<>
257245
<motion.path
258246
d={generateAreaPath(points)}
@@ -275,6 +263,11 @@ function ChartLineLoadingBackground() {
275263
/>
276264
</>
277265
);
266+
}
267+
268+
function ChartLineLoadingBackground() {
269+
const points = useMemo(() => generateLinePoints(30, 10, 90), []);
270+
const secondPoints = useMemo(() => generateLinePoints(40, 30, 90), []);
278271

279272
return (
280273
<motion.div

0 commit comments

Comments
 (0)