Skip to content

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

Add skeleton

Bill Yang6060df4942e7
diff --git a/client/src/app/components/MainSection/Overview.tsx b/client/src/app/components/MainSection/Overview.tsx
index f4c4ca027..29d8bc86a 100644
--- a/client/src/app/components/MainSection/Overview.tsx
+++ b/client/src/app/components/MainSection/Overview.tsx
@@ -1,8 +1,33 @@
import { Badge } from "../../../components/ui/badge";
+import { Skeleton } from "../../../components/ui/skeleton";
import { useGetOverview } from "../../../hooks/api";
-import CountUp from "react-countup";
import { formatter } from "../../../lib/utils";
+const ChangePercentage = ({
+ current,
+ previous,
+}: {
+ current: number;
+ previous: number;
+}) => {
+ const change = ((current - previous) / previous) * 100;
+
+ if (change === 0) {
+ return (
+ <Badge variant="minimal" className="text-xs">
+ 0%
+ </Badge>
+ );
+ }
+
+ return (
+ <Badge variant={change > 0 ? "green" : "red"} className="text-xs">
+ {change > 0 ? "+" : ""}
+ {change.toFixed(0)}%
+ </Badge>
+ );
+};
+
export function Overview() {
const {
data: overviewData,
@@ -17,24 +42,12 @@ export function Overview() {
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">
@@ -43,21 +56,20 @@ export function Overview() {
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>
+ {isLoading ? (
+ <>
+ <Skeleton className="w-[60px] h-7 rounded-md" />
+ <Skeleton className="w-[30px] h-5 rounded-md" />
+ </>
+ ) : (
+ <>
+ {formatter(currentUsers)}
+
+ <ChangePercentage
+ current={currentUsers}
+ previous={previousUsers}
+ />
+ </>
)}
</div>
</div>
@@ -67,22 +79,19 @@ export function Overview() {
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>
+ {isLoading ? (
+ <>
+ <Skeleton className="w-[60px] h-7 rounded-md" />
+ <Skeleton className="w-[30px] h-5 rounded-md" />
+ </>
+ ) : (
+ <>
+ {formatter(currentSessions)}
+ <ChangePercentage
+ current={currentSessions}
+ previous={previousSessions}
+ />
+ </>
)}
</div>
</div>
@@ -91,21 +100,19 @@ export function Overview() {
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>
+ {isLoading ? (
+ <>
+ <Skeleton className="w-[60px] h-7 rounded-md" />
+ <Skeleton className="w-[30px] h-5 rounded-md" />
+ </>
+ ) : (
+ <>
+ {formatter(currentPageviews)}
+ <ChangePercentage
+ current={currentPageviews}
+ previous={previousPageviews}
+ />
+ </>
)}
</div>
</div>
diff --git a/client/src/components/ui/badge.tsx b/client/src/components/ui/badge.tsx
index b78f36b0b..dfa32c0c4 100644
--- a/client/src/components/ui/badge.tsx
+++ b/client/src/components/ui/badge.tsx
@@ -18,6 +18,7 @@ const badgeVariants = cva(
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",
+ minimal: "border-transparent bg-neutral-700 text-neutral-200",
},
},
defaultVariants: {
diff --git a/client/src/components/ui/skeleton.tsx b/client/src/components/ui/skeleton.tsx
new file mode 100644
index 000000000..34eeeaf5a
--- /dev/null
+++ b/client/src/components/ui/skeleton.tsx
@@ -0,0 +1,15 @@
+import { cn } from "@/lib/utils"
+
+function Skeleton({
+ className,
+ ...props
+}: React.HTMLAttributes<HTMLDivElement>) {
+ return (
+ <div
+ className={cn("animate-pulse rounded-md bg-neutral-900/10 dark:bg-neutral-50/10", className)}
+ {...props}
+ />
+ )
+}
+
+export { Skeleton }
diff --git a/client/src/hooks/api.ts b/client/src/hooks/api.ts
index 920cef805..3ecf03eb0 100644
--- a/client/src/hooks/api.ts
+++ b/client/src/hooks/api.ts
@@ -30,6 +30,7 @@ export function useGenericQuery<T>(
).then((res) => res.json());
},
staleTime: Infinity,
+ placeholderData: keepPreviousData,
});
}
diff --git a/server/package-lock.json b/server/package-lock.json
index e19c254e4..8bc4a2056 100644
--- a/server/package-lock.json
+++ b/server/package-lock.json
@@ -12,6 +12,7 @@
"@fastify/cors": "^10.0.2",
"@fastify/one-line-logger": "^1.4.0",
"@fastify/static": "^8.0.4",
+ "dotenv": "^16.4.7",
"fastify": "^5.1.0",
"luxon": "^3.5.0",
"node-cron": "^3.0.3",
@@ -677,6 +678,17 @@
"node": ">=0.3.1"
}
},
+ "node_modules/dotenv": {
+ "version": "16.4.7",
+ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.7.tgz",
+ "integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==",
+ "engines": {
+ "node": ">=12"
+ },
+ "funding": {
+ "url": "https://dotenvx.com"
+ }
+ },
"node_modules/dynamic-dedupe": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/dynamic-dedupe/-/dynamic-dedupe-0.3.0.tgz",
diff --git a/server/package.json b/server/package.json
index 5c88a8283..6a98f7804 100644
--- a/server/package.json
+++ b/server/package.json
@@ -13,6 +13,7 @@
"@fastify/cors": "^10.0.2",
"@fastify/one-line-logger": "^1.4.0",
"@fastify/static": "^8.0.4",
+ "dotenv": "^16.4.7",
"fastify": "^5.1.0",
"luxon": "^3.5.0",
"node-cron": "^3.0.3",
diff --git a/server/src/api/getPageViews.ts b/server/src/api/getPageViews.ts
index f2a2968e6..8105cee77 100644
--- a/server/src/api/getPageViews.ts
+++ b/server/src/api/getPageViews.ts
@@ -39,8 +39,6 @@ export async function getPageViews(
time ASC
`;
- console.info(query);
-
try {
const result = await clickhouse.query({
query,
diff --git a/server/src/db/postgres/postgres.ts b/server/src/db/postgres/postgres.ts
index dab3da6a2..207baa303 100644
--- a/server/src/db/postgres/postgres.ts
+++ b/server/src/db/postgres/postgres.ts
@@ -1,5 +1,8 @@
import postgres from "postgres";
import { Session } from "./types";
+import dotenv from "dotenv";
+
+dotenv.config();
export const sql = postgres({
host: process.env.POSTGRES_HOST || "postgres",