🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Update Chart and MainSection components for improved layout and functionality
Bill Yang02e59b13154a
diff --git a/client/src/app/[site]/main/components/MainSection/Chart.tsx b/client/src/app/[site]/main/components/MainSection/Chart.tsxindex 9da742463..de6735c29 100644--- a/client/src/app/[site]/main/components/MainSection/Chart.tsx+++ b/client/src/app/[site]/main/components/MainSection/Chart.tsx@@ -150,7 +150,8 @@ export function Chart({max: Math.max(max, 1),}}enableGridX={false}- enableGridY={false}+ enableGridY={true}+ gridYValues={4}yFormat=" >-.2f"axisTop={null}axisRight={null}diff --git a/client/src/app/[site]/main/components/MainSection/MainSection.tsx b/client/src/app/[site]/main/components/MainSection/MainSection.tsxindex 9f99cfd30..c08caba27 100644--- a/client/src/app/[site]/main/components/MainSection/MainSection.tsx+++ b/client/src/app/[site]/main/components/MainSection/MainSection.tsx@@ -32,7 +32,7 @@ export function MainSection() {<><Card>{(isOverviewFetching || isOverviewFetchingPrevious) && <CardLoader />}- <CardContent className="pt-4 w-full">+ <CardContent className="p-0 w-full"><Overview /></CardContent></Card>diff --git a/client/src/app/[site]/main/components/MainSection/Overview.tsx b/client/src/app/[site]/main/components/MainSection/Overview.tsxindex 5ce471e89..53d0c9852 100644--- a/client/src/app/[site]/main/components/MainSection/Overview.tsx+++ b/client/src/app/[site]/main/components/MainSection/Overview.tsx@@ -1,12 +1,12 @@"use client";-import { Badge } from "@/components/ui/badge";import { Skeleton } from "@/components/ui/skeleton";-import { cn, formatSecondsAsMinutesAndSeconds, formatter } from "@/lib/utils";-import { StatType, useStore } from "../../../../../lib/store";-import { useGetOverview } from "../../../../../api/analytics/useGetOverview";+import { cn, formatSecondsAsMinutesAndSeconds } from "@/lib/utils";import NumberFlow from "@number-flow/react";-import { round } from "lodash";+import { useGetOverview } from "../../../../../api/analytics/useGetOverview";+import { StatType, useStore } from "../../../../../lib/store";+import { useGetOverviewBucketed } from "../../../../../api/analytics/useGetOverviewBucketed";+import { SparklinesChart } from "./SparklinesChart";const ChangePercentage = ({current,@@ -57,41 +57,55 @@ const Stat = ({decimals?: number;postfix?: string;}) => {- const { selectedStat, setSelectedStat } = useStore();+ const { selectedStat, setSelectedStat, site } = useStore();++ const { data, isFetching, error } = useGetOverviewBucketed({ site });++ const sparklinesData = data?.data.map((d) => ({+ value: d[id],+ time: d.time,+ }));+return (<divclassName={cn(- "flex flex-col hover:bg-neutral-800 rounded-md px-3 py-2 cursor-pointer",+ "flex flex-col hover:bg-neutral-800 cursor-pointer border-r border-neutral-800 last:border-r-0 text-nowrap",+ isFetching && "opacity-50",selectedStat === id && "bg-neutral-850")}onClick={() => setSelectedStat(id)}>- <div className="text-sm font-medium text-muted-foreground">{title}</div>- <div className="text-2xl font-medium flex gap-2 items-center">- {isLoading ? (- <>- <Skeleton className="w-[60px] h-7 rounded-md" />- <Skeleton className="w-[30px] h-5 rounded-md" />- </>- ) : (- <>- {valueFormatter ? (- valueFormatter(value)- ) : (- <span>- {- <NumberFlow- respectMotionPreference={false}- value={decimals ? Number(value.toFixed(decimals)) : value}- format={{ notation: "compact" }}- />- }- {postfix && <span>{postfix}</span>}- </span>- )}- <ChangePercentage current={value} previous={previous} />- </>- )}+ <div className={cn("flex flex-col px-3 py-2")}>+ <div className="text-sm font-medium text-muted-foreground">{title}</div>+ <div className="text-2xl font-medium flex gap-2 items-center">+ {isLoading ? (+ <>+ <Skeleton className="w-[60px] h-7 rounded-md" />+ <Skeleton className="w-[30px] h-5 rounded-md" />+ </>+ ) : (+ <>+ {valueFormatter ? (+ valueFormatter(value)+ ) : (+ <span>+ {+ <NumberFlow+ respectMotionPreference={false}+ value={decimals ? Number(value.toFixed(decimals)) : value}+ format={{ notation: "compact" }}+ />+ }+ {postfix && <span>{postfix}</span>}+ </span>+ )}+ <ChangePercentage current={value} previous={previous} />+ </>+ )}+ </div>+ </div>+ <div className="h-[40px] mt-[-16]">+ <SparklinesChart data={sparklinesData} /></div></div>);diff --git a/client/src/app/[site]/main/components/MainSection/SparklinesChart.tsx b/client/src/app/[site]/main/components/MainSection/SparklinesChart.tsxnew file mode 100644index 000000000..7f3fcceb4--- /dev/null+++ b/client/src/app/[site]/main/components/MainSection/SparklinesChart.tsx@@ -0,0 +1,83 @@+"use client";+import { nivoTheme } from "@/lib/nivo";+import { useStore } from "@/lib/store";+import { ResponsiveLine } from "@nivo/line";+import { DateTime } from "luxon";++export const formatter = Intl.NumberFormat("en", { notation: "compact" });++export function SparklinesChart({+ data,+}: {+ data: { value: number; time: string }[] | undefined;+}) {+ const formattedData = data+ ?.map((e, i) => {+ // filter out dates from the future+ if (DateTime.fromSQL(e.time).toUTC() > DateTime.now()) {+ return null;+ }++ return {+ x: DateTime.fromSQL(e.time).toUTC().toFormat("yyyy-MM-dd HH:mm:ss"),+ y: e.value,+ currentTime: DateTime.fromSQL(e.time),+ };+ })+ .filter((e) => e !== null);++ return (+ <ResponsiveLine+ data={[+ {+ id: "1",+ data: formattedData ?? [],+ },+ ]}+ theme={nivoTheme}+ margin={{ top: 5, right: 0, bottom: 0, left: 0 }}+ xScale={{+ type: "time",+ format: "%Y-%m-%d %H:%M:%S",+ precision: "second",+ useUTC: true,+ }}+ yScale={{+ type: "linear",+ min: 0,+ stacked: false,+ reverse: false,+ }}+ enableGridX={false}+ enableGridY={false}+ gridYValues={4}+ yFormat=" >-.2f"+ axisTop={null}+ axisRight={null}+ axisBottom={null}+ axisLeft={null}+ enableTouchCrosshair={true}+ enablePoints={false}+ useMesh={true}+ animate={false}+ enableSlices={"x"}+ colors={["hsl(var(--accent-400))"]}+ enableArea={false}+ areaBaselineValue={0}+ areaOpacity={0.3}+ defs={[+ {+ id: "gradient",+ type: "linearGradient",+ colors: [+ { offset: 0, color: "hsl(var(--accent-400))", opacity: 1 },+ { offset: 100, color: "hsl(var(--accent-400))", opacity: 0 },+ ],+ },+ ]}+ fill={[{ match: (d) => d.id === "1", id: "gradient" }]}+ sliceTooltip={() => null}+ enableCrosshair={false}+ />+ );+}