Skip to content

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

polish overview

Bill Yang269649ec435f
diff --git a/client/package-lock.json b/client/package-lock.json
index 1b7c0355e..fa670115d 100644
--- a/client/package-lock.json
+++ b/client/package-lock.json
@@ -42,6 +42,7 @@
"pg": "^8.13.1",
"postgres": "^3.4.5",
"react": "^19.0.0",
+ "react-countup": "^6.5.3",
"react-day-picker": "^8.10.1",
"react-dom": "^19.0.0",
"react-leaflet": "^5.0.0",
@@ -3128,6 +3129,11 @@
"resolved": "https://registry.npmjs.org/country-flag-icons/-/country-flag-icons-1.5.14.tgz",
"integrity": "sha512-GAFsVzHDu3bdAhbQ1LwBRqk/Ad8+ZzS5zU49P+lRla0KGy/V1V8ywNa1SxBOAmI/lyEOT9dfH3Q++q1lqJlvBA=="
},
+ "node_modules/countup.js": {
+ "version": "2.8.0",
+ "resolved": "https://registry.npmjs.org/countup.js/-/countup.js-2.8.0.tgz",
+ "integrity": "sha512-f7xEhX0awl4NOElHulrl4XRfKoNH3rB+qfNSZZyjSZhaAoUk6elvhH+MNxMmlmuUJ2/QNTWPSA7U4mNtIAKljQ=="
+ },
"node_modules/cross-spawn": {
"version": "7.0.6",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
@@ -4617,6 +4623,17 @@
"node": ">=0.10.0"
}
},
+ "node_modules/react-countup": {
+ "version": "6.5.3",
+ "resolved": "https://registry.npmjs.org/react-countup/-/react-countup-6.5.3.tgz",
+ "integrity": "sha512-udnqVQitxC7QWADSPDOxVWULkLvKUWrDapn5i53HE4DPRVgs+Y5rr4bo25qEl8jSh+0l2cToJgGMx+clxPM3+w==",
+ "dependencies": {
+ "countup.js": "^2.8.0"
+ },
+ "peerDependencies": {
+ "react": ">= 16.3.0"
+ }
+ },
"node_modules/react-day-picker": {
"version": "8.10.1",
"resolved": "https://registry.npmjs.org/react-day-picker/-/react-day-picker-8.10.1.tgz",
diff --git a/client/package.json b/client/package.json
index 13832dc83..ee4cf8e97 100644
--- a/client/package.json
+++ b/client/package.json
@@ -43,6 +43,7 @@
"pg": "^8.13.1",
"postgres": "^3.4.5",
"react": "^19.0.0",
+ "react-countup": "^6.5.3",
"react-day-picker": "^8.10.1",
"react-dom": "^19.0.0",
"react-leaflet": "^5.0.0",
diff --git a/client/src/app/components/MainSection/MainSection.tsx b/client/src/app/components/MainSection/MainSection.tsx
index a6f50488f..1b643cd44 100644
--- a/client/src/app/components/MainSection/MainSection.tsx
+++ b/client/src/app/components/MainSection/MainSection.tsx
@@ -3,6 +3,7 @@ import { Card, CardContent, CardLoader } from "@/components/ui/card";
import { useGetOverview, useGetPageviews } from "../../../hooks/api";
import { BucketSelection } from "./BucketSelection";
import { Chart } from "./Chart";
+import { Overview } from "./Overview";
import { PreviousChart } from "./PreviousChart";
export function MainSection() {
@@ -11,12 +12,9 @@ export function MainSection() {
data: previousData,
isFetching: isPreviousFetching,
error: previousError,
- } = useGetPageviews(true);
- const {
- data: overviewData,
- isFetching: isOverviewFetching,
- error: overviewError,
- } = useGetOverview();
+ } = useGetPageviews("previous");
+ const { isFetching: isOverviewFetching } = useGetOverview();
+ const { isFetching: isOverviewFetchingPrevious } = useGetOverview("previous");
const maxOfDataAndPreviousData = Math.max(
Math.max(...(data?.data?.map((d) => d.pageviews) ?? [])),
@@ -25,35 +23,13 @@ export function MainSection() {
return (
<Card>
- {(isFetching || isOverviewFetching) && <CardLoader />}
+ {(isFetching ||
+ isPreviousFetching ||
+ isOverviewFetching ||
+ isOverviewFetchingPrevious) && <CardLoader />}
<CardContent className="pt-4 w-full">
<div className="flex justify-between items-center">
- <div className="flex gap-8 items-center">
- <div className="flex flex-col gap-1">
- <div className="text-sm font-medium text-muted-foreground">
- Unique Users
- </div>
- <div className="text-3xl font-medium">
- {overviewData?.data?.users.toLocaleString()}
- </div>
- </div>
- <div className="flex flex-col gap-1">
- <div className="text-sm font-medium text-muted-foreground">
- Sessions
- </div>
- <div className="text-3xl font-medium">
- {overviewData?.data?.sessions.toLocaleString()}
- </div>
- </div>
- <div className="flex flex-col gap-1">
- <div className="text-sm font-medium text-muted-foreground">
- Pageviews
- </div>
- <div className="text-3xl font-medium">
- {overviewData?.data?.pageviews.toLocaleString()}
- </div>
- </div>
- </div>
+ <Overview />
<BucketSelection />
</div>
<div className="h-[350px] relative">
diff --git a/client/src/app/components/MainSection/Overview.tsx b/client/src/app/components/MainSection/Overview.tsx
new file mode 100644
index 000000000..f4c4ca027
--- /dev/null
+++ b/client/src/app/components/MainSection/Overview.tsx
@@ -0,0 +1,114 @@
+import { Badge } from "../../../components/ui/badge";
+import { useGetOverview } from "../../../hooks/api";
+import CountUp from "react-countup";
+import { formatter } from "../../../lib/utils";
+
+export function Overview() {
+ const {
+ data: overviewData,
+ isFetching: isOverviewFetching,
+ isLoading: isOverviewLoading,
+ error: overviewError,
+ } = useGetOverview();
+ const { data: overviewDataPrevious, isLoading: isOverviewLoadingPrevious } =
+ useGetOverview("previous");
+
+ const isLoading = isOverviewLoading || isOverviewLoadingPrevious;
+
+ const currentUsers = overviewData?.data?.users ?? 0;
+ const previousUsers = overviewDataPrevious?.data?.users ?? 0;
+ const usersChange = ((currentUsers - previousUsers) / previousUsers) * 100;
+ const usersChangePercentage = usersChange;
+
+ const currentSessions = overviewData?.data?.sessions ?? 0;
+ const previousSessions = overviewDataPrevious?.data?.sessions ?? 0;
+ const sessionsChange =
+ ((currentSessions - previousSessions) / previousSessions) * 100;
+ const sessionsChangePercentage = sessionsChange;
+
+ const currentPageviews = overviewData?.data?.pageviews ?? 0;
+ const previousPageviews = overviewDataPrevious?.data?.pageviews ?? 0;
+ const pageviewsChange =
+ ((currentPageviews - previousPageviews) / previousPageviews) * 100;
+ const pageviewsChangePercentage = pageviewsChange;
+
+ if (isLoading) {
+ return <div className="h-[60px]"></div>;
+ }
+
+ return (
+ <div className="flex gap-8 items-center">
+ <div className="flex flex-col gap-1">
+ <div className="text-sm font-medium text-muted-foreground">
+ Unique Users
+ </div>
+ <div className="text-3xl font-medium flex gap-2 items-center">
+ <CountUp
+ start={previousUsers}
+ end={currentUsers}
+ duration={0.5}
+ separator=","
+ formattingFn={formatter}
+ />
+ {!isLoading && (
+ <Badge
+ variant={usersChangePercentage > 0 ? "green" : "red"}
+ className="text-xs"
+ >
+ {usersChangePercentage > 0 ? "+" : ""}
+ {usersChangePercentage.toFixed(0)}%
+ </Badge>
+ )}
+ </div>
+ </div>
+
+ <div className="flex flex-col gap-1">
+ <div className="text-sm font-medium text-muted-foreground">
+ Sessions
+ </div>
+ <div className="text-3xl font-medium flex gap-2 items-center">
+ <CountUp
+ start={previousSessions}
+ end={currentSessions}
+ duration={0.5}
+ separator=","
+ formattingFn={formatter}
+ />
+
+ {!isLoading && (
+ <Badge
+ variant={sessionsChangePercentage > 0 ? "green" : "red"}
+ className="text-xs"
+ >
+ {sessionsChangePercentage > 0 ? "+" : ""}
+ {sessionsChangePercentage.toFixed(0)}%
+ </Badge>
+ )}
+ </div>
+ </div>
+ <div className="flex flex-col gap-1">
+ <div className="text-sm font-medium text-muted-foreground">
+ Pageviews
+ </div>
+ <div className="text-3xl font-medium flex gap-2 items-center">
+ <CountUp
+ start={previousPageviews}
+ end={currentPageviews}
+ duration={0.5}
+ separator=","
+ formattingFn={formatter}
+ />
+ {!isLoading && (
+ <Badge
+ variant={pageviewsChangePercentage > 0 ? "green" : "red"}
+ className="text-xs"
+ >
+ {pageviewsChangePercentage > 0 ? "+" : ""}
+ {pageviewsChangePercentage.toFixed(0)}%
+ </Badge>
+ )}
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/client/src/components/ui/badge.tsx b/client/src/components/ui/badge.tsx
new file mode 100644
index 000000000..b78f36b0b
--- /dev/null
+++ b/client/src/components/ui/badge.tsx
@@ -0,0 +1,39 @@
+import * as React from "react";
+import { cva, type VariantProps } from "class-variance-authority";
+
+import { cn } from "@/lib/utils";
+
+const badgeVariants = cva(
+ "inline-flex items-center rounded-md border border-neutral-200 px-1 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-neutral-950 focus:ring-offset-2 dark:border-neutral-800 dark:focus:ring-neutral-300",
+ {
+ variants: {
+ variant: {
+ default:
+ "border-transparent bg-neutral-900 text-neutral-50 shadow hover:bg-neutral-900/80 dark:bg-neutral-50 dark:text-neutral-900 dark:hover:bg-neutral-50/80",
+ secondary:
+ "border-transparent bg-neutral-100 text-neutral-900 hover:bg-neutral-100/80 dark:bg-neutral-800 dark:text-neutral-50 dark:hover:bg-neutral-800/80",
+ destructive:
+ "border-transparent bg-red-500 text-neutral-50 shadow hover:bg-red-500/80 dark:bg-red-900 dark:text-neutral-50 dark:hover:bg-red-900/80",
+ outline: "text-neutral-950 dark:text-neutral-50",
+ green:
+ "border-transparent bg-green-500 text-green-200 shadow hover:bg-green-500/80 dark:bg-green-900 dark:text-green-300 dark:hover:bg-green-900/80",
+ red: "border-transparent bg-red-500 text-red-200 shadow hover:bg-red-500/80 dark:bg-red-900 dark:text-red-300 dark:hover:bg-red-900/80",
+ },
+ },
+ defaultVariants: {
+ variant: "default",
+ },
+ }
+);
+
+export interface BadgeProps
+ extends React.HTMLAttributes<HTMLDivElement>,
+ VariantProps<typeof badgeVariants> {}
+
+function Badge({ className, variant, ...props }: BadgeProps) {
+ return (
+ <div className={cn(badgeVariants({ variant }), className)} {...props} />
+ );
+}
+
+export { Badge, badgeVariants };
diff --git a/client/src/hooks/api.ts b/client/src/hooks/api.ts
index a6ba69257..920cef805 100644
--- a/client/src/hooks/api.ts
+++ b/client/src/hooks/api.ts
@@ -11,14 +11,18 @@ export type APIResponse<T> = {
error?: string;
};
+type PeriodTime = "current" | "previous";
+
export function useGenericQuery<T>(
- endpoint: string
+ endpoint: string,
+ periodTime?: PeriodTime
): UseQueryResult<APIResponse<T>> {
- const { time } = useTimeSelection();
- const { startDate, endDate } = getStartAndEndDate(time);
+ const { time, previousTime } = useTimeSelection();
+ const timeToUse = periodTime === "previous" ? previousTime : time;
+ const { startDate, endDate } = getStartAndEndDate(timeToUse);
return useQuery({
- queryKey: [endpoint, time],
+ queryKey: [endpoint, timeToUse],
queryFn: () => {
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
return fetch(
@@ -95,10 +99,10 @@ export type GetPageViewsResponse = {
}[];
export function useGetPageviews(
- usePrevious?: boolean
+ periodTime?: PeriodTime
): UseQueryResult<APIResponse<GetPageViewsResponse>> {
const { time, previousTime, bucket } = useTimeSelection();
- const timeToUse = usePrevious ? previousTime : time;
+ const timeToUse = periodTime === "previous" ? previousTime : time;
const { startDate, endDate } = getStartAndEndDate(timeToUse);
@@ -121,6 +125,6 @@ export type GetOverviewResponse = {
users: number;
};
-export function useGetOverview() {
- return useGenericQuery<GetOverviewResponse>("overview");
+export function useGetOverview(periodTime?: PeriodTime) {
+ return useGenericQuery<GetOverviewResponse>("overview", periodTime);
}