Skip to content

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

improve performance of site cards list (#251)

Bill Yang04a0dce4f0a2
diff --git a/client/src/api/analytics/useGetOverviewBucketedWithInView.ts b/client/src/api/analytics/useGetOverviewBucketedWithInView.ts
new file mode 100644
index 000000000..79a41a580
--- /dev/null
+++ b/client/src/api/analytics/useGetOverviewBucketedWithInView.ts
@@ -0,0 +1,49 @@
+import { useQuery } from "@tanstack/react-query";
+import { TimeBucket } from "../../lib/store";
+import { BACKEND_URL } from "../../lib/const";
+import { authedFetch } from "../utils";
+import { APIResponse } from "../types";
+import { GetOverviewBucketedResponse } from "./useGetOverviewBucketed";
+
+/**
+ * A wrapper around useGetOverviewBucketedPastMinutes that adds support for
+ * conditional fetching based on viewport visibility
+ */
+export function useGetOverviewBucketedWithInView({
+ pastMinutes = 24 * 60,
+ site,
+ bucket = "hour",
+ isInView = true,
+ refetchInterval,
+}: {
+ pastMinutes?: number;
+ site?: number | string;
+ bucket?: TimeBucket;
+ isInView?: boolean;
+ refetchInterval?: number;
+}) {
+ return useQuery<APIResponse<GetOverviewBucketedResponse>>({
+ queryKey: ["overview-bucketed-past-minutes", pastMinutes, site, bucket],
+ queryFn: () => {
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
+ return authedFetch(`${BACKEND_URL}/overview-bucketed/${site}`, {
+ timezone,
+ bucket,
+ pastMinutes,
+ }).then((res) => res.json());
+ },
+ refetchInterval: isInView ? refetchInterval : 0,
+ enabled: isInView,
+ staleTime: Infinity,
+ placeholderData: (_, query: any) => {
+ if (!query?.queryKey) return undefined;
+ const prevQueryKey = query.queryKey as [string, string, string];
+ const [, , prevSite] = prevQueryKey;
+
+ if (prevSite === site) {
+ return query.state.data;
+ }
+ return undefined;
+ },
+ });
+}
diff --git a/client/src/api/analytics/useGetOverviewWithInView.ts b/client/src/api/analytics/useGetOverviewWithInView.ts
new file mode 100644
index 000000000..99ed2736e
--- /dev/null
+++ b/client/src/api/analytics/useGetOverviewWithInView.ts
@@ -0,0 +1,42 @@
+import { useGetOverviewPastMinutes } from "./useGetOverview";
+import { useQuery } from "@tanstack/react-query";
+import { BACKEND_URL } from "../../lib/const";
+import { authedFetch } from "../utils";
+
+/**
+ * A wrapper around useGetOverviewPastMinutes that adds support for
+ * conditional fetching based on viewport visibility
+ */
+export function useGetOverviewWithInView({
+ pastMinutes = 24 * 60,
+ site,
+ isInView = true,
+}: {
+ pastMinutes?: number;
+ site?: number | string;
+ isInView?: boolean;
+}) {
+ // Always call the useQuery hook, but conditionally enable the fetch
+ return useQuery({
+ queryKey: ["overview-past-minutes", pastMinutes, site],
+ queryFn: () => {
+ const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
+ return authedFetch(`${BACKEND_URL}/overview/${site}`, {
+ pastMinutes,
+ timezone,
+ }).then((res) => res.json());
+ },
+ enabled: isInView, // Only fetch when in view
+ staleTime: Infinity,
+ placeholderData: (_, query: any) => {
+ if (!query?.queryKey) return undefined;
+ const prevQueryKey = query.queryKey as [string, string, string];
+ const [, , prevSite] = prevQueryKey;
+
+ if (prevSite === site) {
+ return query.state.data;
+ }
+ return undefined;
+ },
+ });
+}
diff --git a/client/src/app/page.tsx b/client/src/app/page.tsx
index f2f9964b8..d530aa0b7 100644
--- a/client/src/app/page.tsx
+++ b/client/src/app/page.tsx
@@ -49,7 +49,7 @@ export default function Home() {
<StandardPage>
<FreeTrialBanner />
<div className="flex justify-between items-center mb-4">
- <div className="text-2xl font-bold">Websites</div>
+ <div className="text-2xl font-bold">{sites?.length} Websites</div>
<AddSite disabled={hasNoOrganizations || disabled} />
</div>
{/* Organization required message */}
diff --git a/client/src/components/SiteCard.tsx b/client/src/components/SiteCard.tsx
index faee7dce6..b8b6c9c03 100644
--- a/client/src/components/SiteCard.tsx
+++ b/client/src/components/SiteCard.tsx
@@ -1,12 +1,14 @@
import { ArrowRight } from "lucide-react";
import Link from "next/link";
-import { useGetOverviewPastMinutes } from "../api/analytics/useGetOverview";
-import { useGetOverviewBucketedPastMinutes } from "../api/analytics/useGetOverviewBucketed";
+import { useGetOverviewWithInView } from "../api/analytics/useGetOverviewWithInView";
+import { useGetOverviewBucketedWithInView } from "../api/analytics/useGetOverviewBucketedWithInView";
+import { useInView } from "../hooks/useInView";
+import { Favicon } from "./Favicon";
import { SiteSessionChart } from "./SiteSessionChart";
-import { SiteSettings } from "./SiteSettings/SiteSettings";
import { Button } from "./ui/button";
import { Skeleton } from "./ui/skeleton";
-import { Favicon } from "./Favicon";
+import { SiteSettings } from "./SiteSettings/SiteSettings";
+import { useRef } from "react";
interface SiteCardProps {
siteId: number;
@@ -14,110 +16,137 @@ interface SiteCardProps {
}
export function SiteCard({ siteId, domain }: SiteCardProps) {
- const { data, isLoading } = useGetOverviewBucketedPastMinutes({
+ const { ref, isInView } = useInView({
+ // Start loading slightly before the card comes into view
+ rootMargin: "200px",
+ // Once data is loaded, keep it loaded even when scrolling away
+ persistVisibility: true,
+ });
+
+ // Track if we've ever loaded data successfully
+ const hasLoadedData = useRef(false);
+
+ const { data, isLoading, isSuccess } = useGetOverviewBucketedWithInView({
pastMinutes: 24 * 60,
site: siteId,
bucket: "hour",
+ isInView,
});
- const { data: overviewData, isLoading: isOverviewLoading } =
- useGetOverviewPastMinutes({
- site: siteId,
- pastMinutes: 24 * 60,
- });
+ const {
+ data: overviewData,
+ isLoading: isOverviewLoading,
+ isSuccess: isOverviewSuccess,
+ } = useGetOverviewWithInView({
+ pastMinutes: 24 * 60,
+ site: siteId,
+ isInView,
+ });
- const hasData = (overviewData?.data?.sessions || 0) > 0;
+ // Update the hasLoadedData ref when data loads successfully
+ if (isSuccess && isOverviewSuccess && !hasLoadedData.current) {
+ hasLoadedData.current = true;
+ }
- if (isLoading || isOverviewLoading) {
- return (
- <div
- className="flex flex-col rounded-lg bg-neutral-900/70 p-4 border border-neutral-800 shadow-lg transition-all duration-300"
- key={siteId}
- >
- <div className="flex justify-between items-center">
- <div className="flex gap-3 items-center">
- <Skeleton className="w-6 h-6 rounded" />
- <Skeleton className="h-6 w-40" />
- </div>
- <Skeleton className="h-8 w-8 rounded" />
- </div>
+ const hasData = (overviewData?.data?.sessions || 0) > 0;
- <div className="relative mt-1 mb-1 rounded-md p-2 overflow-hidden">
- <Skeleton className="h-[110px] w-full rounded-md" />
- </div>
+ // Show skeleton when loading or not yet in view, but not if we've already loaded data previously
+ const showSkeleton =
+ (isLoading || isOverviewLoading || !isInView) && !hasLoadedData.current;
- <div className="grid grid-cols-3 gap-2 mt-2">
- <div className="flex flex-col gap-1 items-center bg-neutral-800/50 rounded-md p-2">
- <Skeleton className="h-3 w-16 rounded" />
- <Skeleton className="h-6 w-10 rounded mt-1" />
+ return (
+ <div
+ ref={ref}
+ className="flex flex-col rounded-lg bg-neutral-900/70 p-4 border border-neutral-800 shadow-lg hover:shadow-xl hover:border-neutral-700 transition-all duration-300 hover:translate-y-[-2px]"
+ >
+ {showSkeleton ? (
+ <>
+ <div className="flex justify-between items-center">
+ <div className="flex gap-3 items-center">
+ <Skeleton className="w-6 h-6 rounded" />
+ <Skeleton className="h-6 w-40" />
+ </div>
</div>
- <div className="flex flex-col gap-1 items-center bg-neutral-800/50 rounded-md p-2">
- <Skeleton className="h-3 w-16 rounded" />
- <Skeleton className="h-6 w-10 rounded mt-1" />
+ <div className="relative mt-1 mb-1 rounded-md p-2 overflow-hidden">
+ <Skeleton className="h-[110px] w-full rounded-md" />
</div>
- <div className="flex items-center justify-center">
- <Skeleton className="h-[38px] w-full rounded" />
- </div>
- </div>
- </div>
- );
- }
+ <div className="grid grid-cols-3 gap-2 mt-2">
+ <div className="flex flex-col gap-1 items-center bg-neutral-800/50 rounded-md p-2">
+ <Skeleton className="h-3 w-16 rounded" />
+ <Skeleton className="h-6 w-10 rounded mt-1" />
+ </div>
- return (
- <div className="flex flex-col rounded-lg bg-neutral-900/70 p-4 border border-neutral-800 shadow-lg hover:shadow-xl hover:border-neutral-700 transition-all duration-300 hover:translate-y-[-2px]">
- <div className="flex justify-between items-center">
- <Link
- href={`/${siteId}`}
- className="group flex gap-3 items-center duration-200"
- >
- <Favicon domain={domain} className="w-6 h-6" />
- <span className="text-lg font-medium truncate group-hover:underline transition-all">
- {domain}
- </span>
- </Link>
- <SiteSettings siteId={siteId} />
- </div>
-
- <div className="relative mt-1 mb-1 rounded-md p-2 overflow-hidden">
- <SiteSessionChart data={data?.data ?? []} height={110} />
- {!hasData && (
- <div className="absolute inset-0 flex items-center justify-center bg-neutral-900/70 backdrop-blur-sm">
- <span className="text-sm text-neutral-400">No data available</span>
+ <div className="flex flex-col gap-1 items-center bg-neutral-800/50 rounded-md p-2">
+ <Skeleton className="h-3 w-16 rounded" />
+ <Skeleton className="h-6 w-10 rounded mt-1" />
+ </div>
+
+ <div className="flex items-center justify-center">
+ <Skeleton className="h-[38px] w-full rounded" />
+ </div>
+ </div>
+ </>
+ ) : (
+ <>
+ <div className="flex justify-between items-center">
+ <Link
+ href={`/${siteId}`}
+ className="group flex gap-3 items-center duration-200"
+ >
+ <Favicon domain={domain} className="w-6 h-6" />
+ <span className="text-lg font-medium truncate group-hover:underline transition-all">
+ {domain}
+ </span>
+ </Link>
</div>
- )}
- </div>
-
- <div className="grid grid-cols-3 gap-2 mt-2">
- <div className="flex flex-col gap-1 items-center bg-neutral-800/50 rounded-md p-2 hover:bg-neutral-800 transition-colors">
- <div className="text-xs text-neutral-400">Sessions</div>
- <div className="font-semibold">
- {overviewData?.data?.sessions?.toLocaleString() || "0"}
+
+ <div className="relative mt-1 mb-1 rounded-md p-2 overflow-hidden">
+ <SiteSessionChart data={data?.data ?? []} height={110} />
+ {!hasData && (
+ <div className="absolute inset-0 flex items-center justify-center bg-neutral-900/70 backdrop-blur-sm">
+ <span className="text-sm text-neutral-400">
+ No data available
+ </span>
+ </div>
+ )}
</div>
- </div>
- <div className="flex flex-col gap-1 items-center bg-neutral-800/50 rounded-md p-2 hover:bg-neutral-800 transition-colors">
- <div className="text-xs text-neutral-400">Users</div>
- <div className="font-semibold">
- {overviewData?.data?.users?.toLocaleString() || "0"}
+ <div className="grid grid-cols-3 gap-2 mt-2">
+ <div className="flex flex-col gap-1 items-center bg-neutral-800/50 rounded-md p-2 hover:bg-neutral-800 transition-colors">
+ <div className="text-xs text-neutral-400">Sessions</div>
+ <div className="font-semibold">
+ {overviewData?.data?.sessions?.toLocaleString() || "0"}
+ </div>
+ </div>
+
+ <div className="flex flex-col gap-1 items-center bg-neutral-800/50 rounded-md p-2 hover:bg-neutral-800 transition-colors">
+ <div className="text-xs text-neutral-400">Users</div>
+ <div className="font-semibold">
+ {overviewData?.data?.users?.toLocaleString() || "0"}
+ </div>
+ </div>
+
+ <Link
+ href={`/${siteId}`}
+ className="flex items-center justify-center"
+ >
+ <Button
+ variant="outline"
+ size="sm"
+ className="w-full h-full border-neutral-700 bg-neutral-800/50 hover:bg-neutral-800 hover:text-blue-400 transition-all"
+ >
+ <span className="mr-1">View</span>
+ <ArrowRight
+ size={14}
+ className="transition-transform group-hover:translate-x-0.5"
+ />
+ </Button>
+ </Link>
</div>
- </div>
-
- <Link href={`/${siteId}`} className="flex items-center justify-center">
- <Button
- variant="outline"
- size="sm"
- className="w-full h-full border-neutral-700 bg-neutral-800/50 hover:bg-neutral-800 hover:text-blue-400 transition-all"
- >
- <span className="mr-1">View</span>
- <ArrowRight
- size={14}
- className="transition-transform group-hover:translate-x-0.5"
- />
- </Button>
- </Link>
- </div>
+ </>
+ )}
</div>
);
}
diff --git a/client/src/hooks/useInView.ts b/client/src/hooks/useInView.ts
new file mode 100644
index 000000000..79863a6e4
--- /dev/null
+++ b/client/src/hooks/useInView.ts
@@ -0,0 +1,43 @@
+import { useEffect, useRef, useState } from "react";
+
+interface UseInViewOptions extends IntersectionObserverInit {
+ /**
+ * Once an element has been seen, keep it as "in view" to prevent
+ * flickering when scrolling in and out quickly
+ */
+ persistVisibility?: boolean;
+}
+
+export function useInView<T extends HTMLElement = HTMLDivElement>(
+ options?: UseInViewOptions
+) {
+ const { persistVisibility = true, ...observerOptions } = options || {};
+ const ref = useRef<T>(null);
+ const [isInView, setIsInView] = useState(false);
+ const wasPreviouslyVisible = useRef(false);
+
+ useEffect(() => {
+ const element = ref.current;
+ if (!element) return;
+
+ const observer = new IntersectionObserver(([entry]) => {
+ if (entry.isIntersecting) {
+ // Always update state when entering viewport
+ setIsInView(true);
+ wasPreviouslyVisible.current = true;
+ } else if (!persistVisibility || !wasPreviouslyVisible.current) {
+ // Only update state when leaving viewport if we're not persisting visibility
+ // or if we haven't been visible before
+ setIsInView(false);
+ }
+ }, observerOptions);
+
+ observer.observe(element);
+
+ return () => {
+ observer.disconnect();
+ };
+ }, [observerOptions, persistVisibility]);
+
+ return { ref, isInView };
+}
diff --git a/server/src/api/sites/getSites.ts b/server/src/api/sites/getSites.ts
index af9180de4..141c23021 100644
--- a/server/src/api/sites/getSites.ts
+++ b/server/src/api/sites/getSites.ts
@@ -1,28 +1,72 @@
import { and, eq } from "drizzle-orm";
import { FastifyReply, FastifyRequest } from "fastify";
+import clickhouse from "../../db/clickhouse/clickhouse.js";
import { db } from "../../db/postgres/postgres.js";
import { member } from "../../db/postgres/schema.js";
-import { getSitesUserHasAccessTo } from "../../lib/auth-utils.js";
-import { getSubscriptionInner } from "../stripe/getSubscription.js";
+import {
+ getSitesUserHasAccessTo,
+ getUserGodMode,
+} from "../../lib/auth-utils.js";
import { IS_CLOUD, TRIAL_EVENT_LIMIT } from "../../lib/const.js";
+import { processResults } from "../analytics/utils.js";
+import { getSubscriptionInner } from "../stripe/getSubscription.js";
export async function getSites(req: FastifyRequest, reply: FastifyReply) {
try {
// Get sites the user has access to
const sitesData = await getSitesUserHasAccessTo(req);
+ const godMode = await getUserGodMode(req);
+
+ // Only query for session counts if there are sites
+ const sessionCountMap = new Map<number, number>();
+
+ if (sitesData.length > 0) {
+ // Extract site IDs that the user has access to
+ const siteIds = sitesData.map((site) => site.siteId);
+
+ // Query session counts only for the user's sites
+ const sessionCountsResult = await clickhouse.query({
+ query: `
+ SELECT
+ site_id,
+ uniqExact(session_id) AS total_sessions
+ FROM events
+ WHERE timestamp >= now() - INTERVAL 1 DAY
+ AND site_id IN (${siteIds.join(",")})
+ GROUP BY site_id
+ `,
+ format: "JSONEachRow",
+ });
+
+ const sessionCounts = await processResults(sessionCountsResult);
+
+ // Add session counts to map
+ if (Array.isArray(sessionCounts)) {
+ sessionCounts.forEach((row: any) => {
+ if (
+ row &&
+ typeof row.site_id === "number" &&
+ typeof row.total_sessions === "number"
+ ) {
+ sessionCountMap.set(Number(row.site_id), row.total_sessions);
+ }
+ });
+ }
+ }
const enhancedSitesData = await Promise.all(
sitesData.map(async (site) => {
let isOwner = false;
let ownerId = "";
- if (!IS_CLOUD) {
+ if (!IS_CLOUD || godMode) {
return {
...site,
monthlyEventCount: 0,
eventLimit: Infinity,
overMonthlyLimit: false,
isOwner: true,
+ sessionsLast24Hours: sessionCountMap.get(Number(site.siteId)) || 0,
};
}
// Determine ownership if organization ID exists
@@ -55,11 +99,18 @@ export async function getSites(req: FastifyRequest, reply: FastifyReply) {
eventLimit,
overMonthlyLimit: monthlyEventCount > eventLimit,
isOwner,
+ sessionsLast24Hours: sessionCountMap.get(site.siteId) || 0,
};
})
);
- return reply.status(200).send(enhancedSitesData);
+ return reply
+ .status(200)
+ .send(
+ enhancedSitesData.sort(
+ (a, b) => b.sessionsLast24Hours - a.sessionsLast24Hours
+ )
+ );
} catch (err) {
console.error("Error in getSites:", err);
return reply.status(500).send(String(err));
diff --git a/server/src/lib/auth-utils.ts b/server/src/lib/auth-utils.ts
index 8c78e4704..8c58035ee 100644
--- a/server/src/lib/auth-utils.ts
+++ b/server/src/lib/auth-utils.ts
@@ -22,6 +22,23 @@ export async function getSession(req: FastifyRequest) {
return session;
}
+export async function getUserGodMode(req: FastifyRequest) {
+ const headers = new Headers(req.headers as any);
+ const session = await auth!.api.getSession({ headers });
+ const userId = session?.user.id;
+
+ if (!userId) {
+ return false;
+ }
+
+ const userRecord = await db
+ .select({ godMode: user.godMode })
+ .from(user)
+ .where(eq(user.id, userId))
+ .limit(1);
+ return userRecord.length > 0 && userRecord[0].godMode;
+}
+
export async function getSitesUserHasAccessTo(
req: FastifyRequest,
adminOnly = false