Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Refactor Chart component and MainSection layout

Bill Yang972f9298e591
diff --git a/client/src/app/[site]/main/components/MainSection/Chart.tsx b/client/src/app/[site]/main/components/MainSection/Chart.tsx
index 3f2165bd7..3fe0b4d20 100644
--- a/client/src/app/[site]/main/components/MainSection/Chart.tsx
+++ b/client/src/app/[site]/main/components/MainSection/Chart.tsx
@@ -2,7 +2,7 @@
import { TimeBucket } from "@rybbit/shared";
import { useNivoTheme } from "@/lib/nivo";
import { StatType, useStore } from "@/lib/store";
-import { LineCustomSvgLayer, LineCustomSvgLayerProps, LineSeries, ResponsiveLine } from "@nivo/line";
+import { ResponsiveLine } from "@nivo/line";
import { useWindowSize } from "@uidotdev/usehooks";
import { DateTime } from "luxon";
import { useTheme } from "next-themes";
@@ -254,16 +254,11 @@ export function Chart({
});
});
- const StackedLines: LineCustomSvgLayer<LineSeries> = ({
- series,
- lineGenerator,
- xScale,
- yScale,
- }: LineCustomSvgLayerProps<LineSeries>) => {
- return series.map(({ id, data, color }) => {
+ const StackedLines = ({ series, lineGenerator, xScale, yScale }: any) => {
+ return series.map(({ id, data, color }: any) => {
const usableData = displayDashed && data.length >= 2 ? data.slice(0, -1) : data;
- const coords = usableData.map(d => {
- const stackedY = (d.data as any).yStacked ?? d.data.y;
+ const coords = usableData.map((d: any) => {
+ const stackedY = d.data.yStacked ?? d.data.y;
return { x: xScale(d.data.x), y: yScale(stackedY) };
});
const path = lineGenerator(coords);
@@ -272,17 +267,12 @@ export function Chart({
});
};
- const DashedOverlay: LineCustomSvgLayer<LineSeries> = ({
- series,
- lineGenerator,
- xScale,
- yScale,
- }: LineCustomSvgLayerProps<LineSeries>) => {
- return series.map(({ id, data, color }) => {
+ const DashedOverlay = ({ series, lineGenerator, xScale, yScale }: any) => {
+ return series.map(({ id, data, color }: any) => {
if (!displayDashed || data.length < 2) return null;
const lastTwo = data.slice(-2);
- const coords = lastTwo.map(d => {
- const stackedY = (d.data as any).yStacked ?? d.data.y;
+ const coords = lastTwo.map((d: any) => {
+ const stackedY = d.data.yStacked ?? d.data.y;
return { x: xScale(d.data.x), y: yScale(stackedY) };
});
const path = lineGenerator(coords);
@@ -519,7 +509,7 @@ export function Chart({
"areas",
"crosshair",
StackedLines,
- displayDashed ? DashedOverlay : null,
+ ...(displayDashed ? [DashedOverlay] : []),
"slices",
"points",
"mesh",
diff --git a/client/src/app/[site]/main/components/MainSection/MainSection.tsx b/client/src/app/[site]/main/components/MainSection/MainSection.tsx
index 588f1d964..ae8c17ee6 100644
--- a/client/src/app/[site]/main/components/MainSection/MainSection.tsx
+++ b/client/src/app/[site]/main/components/MainSection/MainSection.tsx
@@ -1,19 +1,19 @@
"use client";
import { Card, CardContent, CardLoader } from "@/components/ui/card";
+import { Filter } from "lucide-react";
import { Tilt_Warp } from "next/font/google";
-import Image from "next/image";
import Link from "next/link";
import { useGetOverview } from "../../../../../api/analytics/useGetOverview";
import { useGetOverviewBucketed } from "../../../../../api/analytics/useGetOverviewBucketed";
+import { BucketSelection } from "../../../../../components/BucketSelection";
+import { RybbitLogo } from "../../../../../components/RybbitLogo";
+import { Button } from "../../../../../components/ui/button";
import { authClient } from "../../../../../lib/auth";
import { useStore } from "../../../../../lib/store";
import { cn } from "../../../../../lib/utils";
-import { BucketSelection } from "../../../../../components/BucketSelection";
import { Chart } from "./Chart";
import { Overview } from "./Overview";
import { PreviousChart } from "./PreviousChart";
-import { RybbitLogo } from "../../../../../components/RybbitLogo";
-import { Switch } from "../../../../../components/ui/switch";
const SELECTED_STAT_MAP = {
pageviews: "Pageviews",
@@ -60,15 +60,13 @@ export function MainSection() {
const activeKeys = showUserBreakdown ? (["new_users", "returning_users"] as const) : ([selectedStat] as const);
- const getMaxValue = (dataset?: { data?: { [key: string]: number }[] }) =>
+ const getMaxValue = (dataset?: { data?: Record<string, number | string>[] }) =>
Math.max(
- ...(
- dataset?.data?.map(d =>
- showUserBreakdown
- ? (d?.["new_users"] ?? 0) + (d?.["returning_users"] ?? 0)
- : Math.max(...activeKeys.map(key => d?.[key] ?? 0))
- ) ?? [0]
- )
+ ...(dataset?.data?.map(d =>
+ showUserBreakdown
+ ? Number(d?.["new_users"] ?? 0) + Number(d?.["returning_users"] ?? 0)
+ : Math.max(...activeKeys.map(key => Number(d?.[key] ?? 0)))
+ ) ?? [0])
);
const maxOfDataAndPreviousData = Math.max(getMaxValue(data), getMaxValue(previousData));
@@ -98,39 +96,19 @@ export function MainSection() {
<span className="absolute left-1/2 -translate-x-1/2 text-sm text-neutral-700 dark:text-neutral-200">
{SELECTED_STAT_MAP[selectedStat]}
</span>
- <div className="flex items-center gap-3 md:justify-end">
+ <div className="flex items-center gap-2 md:justify-end">
{selectedStat === "users" && (
- <div className="hidden md:flex items-center gap-2 text-xs text-muted-foreground">
- <Switch id="toggle-user-breakdown" checked={showUsersSplit} onCheckedChange={setShowUsersSplit} />
- <label className="cursor-pointer" htmlFor="toggle-user-breakdown">
- New vs returning
- </label>
+ <div className="flex items-center gap-2 text-xs text-muted-foreground">
+ <Button variant="default" size="smIcon" onClick={() => setShowUsersSplit(!showUsersSplit)}>
+ <Filter />
+ </Button>
</div>
)}
<BucketSelection />
</div>
</div>
- {selectedStat === "users" && (
- <div className="flex md:hidden items-center gap-2 text-xs text-muted-foreground justify-end">
- <Switch id="toggle-user-breakdown" checked={showUsersSplit} onCheckedChange={setShowUsersSplit} />
- <label className="cursor-pointer" htmlFor="toggle-user-breakdown">
- New vs returning
- </label>
- </div>
- )}
</div>
- {showUserBreakdown && (
- <div className="flex items-center justify-center gap-4 text-xs text-muted-foreground px-2 md:px-0 mt-2">
- <div className="flex items-center gap-2">
- <span className="h-2.5 w-2.5 rounded-full" style={{ backgroundColor: "hsl(var(--dataviz))" }} />
- <span>New users</span>
- </div>
- <div className="flex items-center gap-2">
- <span className="h-2.5 w-2.5 rounded-full" style={{ backgroundColor: "hsl(var(--accent-200))" }} />
- <span>Returning users</span>
- </div>
- </div>
- )}
+
<div className="h-[200px] md:h-[290px] relative">
<div className="absolute top-0 left-0 w-full h-full">
<PreviousChart data={previousData} max={maxOfDataAndPreviousData} />