🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor SessionCard and SessionDetails components for improved performance and user experience
Bill Yang3d689fe087c3
diff --git a/client/src/app/[site]/sessions/components/SessionCard.tsx b/client/src/app/[site]/sessions/components/SessionCard.tsxindex cccd40f46..a19124ddb 100644--- a/client/src/app/[site]/sessions/components/SessionCard.tsx+++ b/client/src/app/[site]/sessions/components/SessionCard.tsx@@ -1,4 +1,4 @@-import { useState } from "react";+import { memo, useState } from "react";import { DateTime } from "luxon";import { Browser } from "../../components/shared/icons/Browser";import { CountryFlag } from "../../components/shared/icons/CountryFlag";@@ -225,48 +225,77 @@ export function SessionCard({ session, onClick }: SessionCardProps) {</div>{/* Expanded content using SessionDetails component */}- {expanded && <SessionDetails sessionId={session.session_id} />}+ {expanded && <SessionDetails session={session} />}</div>);}-export function SessionCardSkeleton() {- return (- <div className="mb-3 rounded-lg bg-neutral-900 border border-neutral-800 p-3">- <div className="flex items-center">- {/* Avatar and User ID */}- <div className="flex items-center gap-2 min-w-[110px]">- <Skeleton className="h-6 w-6 rounded-full" />- <Skeleton className="h-3 w-16" />- </div>+export const SessionCardSkeleton = memo(() => {+ // Function to get a random width class for skeletons+ const getRandomWidth = () => {+ const widths = [+ "w-16",+ "w-20",+ "w-24",+ "w-28",+ "w-32",+ "w-36",+ "w-40",+ "w-44",+ "w-48",+ ];+ return widths[Math.floor(Math.random() * widths.length)];+ };- {/* Icons */}- <div className="flex space-x-2 items-center ml-3">- <Skeleton className="h-4 w-4" />- <Skeleton className="h-4 w-4" />- <Skeleton className="h-4 w-4" />- <Skeleton className="h-4 w-4" />- </div>+ // Create multiple skeletons for a realistic loading state+ const skeletons = Array.from({ length: 5 }).map((_, index) => (+ <div+ className="mb-3 rounded-lg bg-neutral-900 border border-neutral-800 overflow-hidden"+ key={index}+ >+ <div className="p-3">+ <div className="flex items-center gap-2">+ {/* Avatar and User ID */}+ <div className="flex items-center gap-2">+ <Skeleton className="h-6 w-6 rounded-full" />+ <Skeleton className="h-3 w-14" />+ </div>- {/* Entry/Exit paths */}- <div className="flex items-center ml-3 flex-1 min-w-0">- <Skeleton className="h-3 w-20" />- <div className="mx-2">- <Skeleton className="h-3 w-3" />+ {/* Icons */}+ <div className="flex space-x-2 items-center ml-3">+ <Skeleton className="h-4 w-4 rounded-sm" />+ <Skeleton className="h-4 w-4 rounded-sm" />+ <Skeleton className="h-4 w-4 rounded-sm" />+ <Skeleton className="h-4 w-4 rounded-sm" />+ {/* Badge skeleton for pageviews */}+ <Skeleton className="h-4 w-8 rounded-sm" />+ {/* Badge skeleton for events */}+ <Skeleton className="h-4 w-8 rounded-sm" /></div>- <Skeleton className="h-3 w-20" />- </div>- {/* Time */}- <div className="ml-3">- <Skeleton className="h-3 w-16" />- </div>+ {/* Entry/Exit paths with randomized widths */}+ <div className="flex items-center ml-3 flex-1 min-w-0">+ <Skeleton className={`h-3 max-w-[200px] ${getRandomWidth()}`} />+ <div className="mx-2 flex-shrink-0">+ <Skeleton className="h-3 w-3" />+ </div>+ <Skeleton className={`h-3 max-w-[200px] ${getRandomWidth()}`} />+ </div>++ {/* Time */}+ <div className="flex items-center gap-1 ml-3">+ <Skeleton className="h-3 w-3 rounded-full" />+ <Skeleton className="h-3 w-16" />+ </div>- {/* Expand icon */}- <div className="ml-3 flex-shrink-0">- <Skeleton className="h-4 w-4" />+ {/* Expand icon */}+ <div className="ml-3 flex-shrink-0">+ <Skeleton className="h-4 w-4" />+ </div></div></div></div>- );-}+ ));++ return <>{skeletons}</>;+});diff --git a/client/src/app/[site]/sessions/components/SessionDetails.tsx b/client/src/app/[site]/sessions/components/SessionDetails.tsxindex e9b152be1..a6111d106 100644--- a/client/src/app/[site]/sessions/components/SessionDetails.tsx+++ b/client/src/app/[site]/sessions/components/SessionDetails.tsx@@ -13,6 +13,7 @@ import Avatar from "boring-avatars";import {useGetSessionDetails,PageviewEvent,+ GetSessionsResponse,} from "../../../../api/analytics/userSessions";import { Skeleton } from "@/components/ui/skeleton";import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";@@ -20,6 +21,7 @@ import { Badge } from "@/components/ui/badge";import { Alert, AlertDescription } from "@/components/ui/alert";import Link from "next/link";import CopyText from "@/components/CopyText";+import { memo } from "react";// Component to display a single pageview or eventfunction PageviewItem({@@ -149,16 +151,78 @@ function PageviewItem({);}+// Memoized skeleton component for the session details timeline+const SessionDetailsTimelineSkeleton = memo(+ ({ itemCount }: { itemCount: number }) => {+ // Function to get a random width class for skeletons+ const getRandomWidth = () => {+ const widths = [+ "w-28",+ "w-36",+ "w-44",+ "w-52",+ "w-60",+ "w-72",+ "w-80",+ "w-96",+ "w-full",+ ];+ return widths[Math.floor(Math.random() * widths.length)];+ };++ return (+ <div className="py-4">+ {/* Tabs skeleton */}+ <div className="flex gap-2 mb-6">+ <Skeleton className="h-8 w-24 rounded-md" />+ <Skeleton className="h-8 w-24 rounded-md" />+ </div>++ {/* Timeline tab skeleton */}+ <div className="mb-4">+ <div className="flex gap-2 mb-3">+ <Skeleton className="h-6 w-32 rounded-full" />+ <Skeleton className="h-6 w-32 rounded-full" />+ </div>++ {/* Timeline items skeleton */}+ {Array.from({ length: itemCount }).map((_, i) => (+ <div key={i} className="flex mb-3">+ <Skeleton className="h-8 w-8 rounded-full" />+ <div className="ml-3 flex-1">+ <div className="flex items-center">+ <Skeleton className="h-4 w-4 mr-3" />+ <Skeleton+ className={`h-4 ${getRandomWidth()} max-w-md mr-4`}+ />+ <Skeleton className="h-3 w-16 flex-shrink-0 ml-auto" />+ </div>+ <div className="mt-1 pl-7">+ {Math.random() > 0.5 && (+ <Skeleton+ className={`h-3 ${Math.random() > 0.7 ? "w-48" : "w-32"}`}+ />+ )}+ </div>+ </div>+ </div>+ ))}+ </div>+ </div>+ );+ }+);+interface SessionDetailsProps {- sessionId: string;+ session: GetSessionsResponse[number];}-export function SessionDetails({ sessionId }: SessionDetailsProps) {+export function SessionDetails({ session }: SessionDetailsProps) {const {data: sessionDetails,isLoading,error,- } = useGetSessionDetails(sessionId);+ } = useGetSessionDetails(session.session_id);// Calculate session duration for the details sectionconst getDurationFormatted = () => {@@ -176,38 +240,9 @@ export function SessionDetails({ sessionId }: SessionDetailsProps) {return (<div className="px-4 bg-neutral-900 border-t border-neutral-800">{isLoading ? (- <div className="py-4">- {/* Tabs skeleton */}- <div className="flex gap-2 mb-6">- <Skeleton className="h-8 w-24 rounded-md" />- <Skeleton className="h-8 w-24 rounded-md" />- </div>-- {/* Timeline tab skeleton */}- <div className="mb-4">- <div className="flex gap-2 mb-3">- <Skeleton className="h-6 w-32 rounded-full" />- <Skeleton className="h-6 w-32 rounded-full" />- </div>-- {/* Timeline items skeleton */}- {[1, 2, 3].map((i) => (- <div key={i} className="flex mb-3">- <Skeleton className="h-8 w-8 rounded-full" />- <div className="ml-3 flex-1">- <div className="flex items-center">- <Skeleton className="h-4 w-4 mr-3" />- <Skeleton className="h-4 w-full max-w-md mr-4" />- <Skeleton className="h-3 w-16 flex-shrink-0 ml-auto" />- </div>- <div className="mt-1 pl-7">- <Skeleton className="h-3 w-32" />- </div>- </div>- </div>- ))}- </div>- </div>+ <SessionDetailsTimelineSkeleton+ itemCount={session.pageviews + session.events}+ />) : error ? (<Alert variant="destructive" className="mt-4"><AlertDescription>