🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Refactor user and session components for improved data handling and navigation
Bill Yang007ced07bab9
diff --git a/client/src/app/[site]/sessions/page.tsx b/client/src/app/[site]/sessions/page.tsxindex f2390b215..79e5c847c 100644--- a/client/src/app/[site]/sessions/page.tsx+++ b/client/src/app/[site]/sessions/page.tsx@@ -1,7 +1,7 @@"use client";import { SubHeader } from "../components/SubHeader/SubHeader";-import SessionsList from "../../../components/Sessions/SessionsList";+import SessionsList from "@/components/Sessions/SessionsList";export default function SessionsPage() {return (diff --git a/client/src/app/[site]/user/[userId]/page.tsx b/client/src/app/[site]/user/[userId]/page.tsxindex 01bece6ce..8392fdf9a 100644--- a/client/src/app/[site]/user/[userId]/page.tsx+++ b/client/src/app/[site]/user/[userId]/page.tsx@@ -1,185 +1,19 @@"use client";import { useParams } from "next/navigation";-import { Skeleton } from "@/components/ui/skeleton";-import { Browser } from "../../components/shared/icons/Browser";-import { OperatingSystem } from "../../components/shared/icons/OperatingSystem";-import { CountryFlag } from "../../components/shared/icons/CountryFlag";-import { getCountryName } from "@/lib/utils";-import { useStore } from "../../../../lib/store";-import { DateTime } from "luxon";-import {- useGetUserSessions,- UserSessionsResponse,-} from "../../../../api/analytics/userSessions";-import { useGetSiteMetadata } from "../../../../api/admin/sites";--// Helper function to format duration in seconds to a readable format-const formatDuration = (seconds: number): string => {- if (seconds < 60) return `${seconds} sec`;-- const minutes = Math.floor(seconds / 60);- const remainingSeconds = seconds % 60;-- if (remainingSeconds === 0) return `${minutes} min`;- return `${minutes} min ${remainingSeconds} sec`;-};--// Helper function to combine pathname and querystring correctly-const getFullPath = (pathname: string, querystring: string): string => {- if (!querystring) return pathname;-- // Check if querystring already starts with ? and avoid adding another one- const prefix = querystring.startsWith("?") ? "" : "?";- return `${pathname}${prefix}${querystring}`;-};--// Referrer icon component-const ReferrerIcon = ({ referrer }: { referrer: string }) => {- return (- <div- className="text-gray-400 hover:text-gray-300 cursor-help"- title={referrer}- >- <svg- xmlns="http://www.w3.org/2000/svg"- viewBox="0 0 20 20"- fill="currentColor"- className="w-5 h-5"- >- <path- fillRule="evenodd"- d="M5.22 14.78a.75.75 0 001.06 0l7.22-7.22v5.69a.75.75 0 001.5 0v-7.5a.75.75 0 00-.75-.75h-7.5a.75.75 0 000 1.5h5.69l-7.22 7.22a.75.75 0 000 1.06z"- clipRule="evenodd"- />- </svg>- </div>- );-};+import SessionsList from "@/components/Sessions/SessionsList";export default function UserPage() {const { userId } = useParams();- const { data, isLoading, error } = useGetUserSessions(userId as string);- const { site } = useStore();- const { siteMetadata, isLoading: isLoadingSiteMetadata } =- useGetSiteMetadata(site);-- // Sessions are now pre-grouped by the server- const groupedSessions = data?.data || [];-- if (error) {- return <div className="p-6">Error: {(error as Error).message}</div>;- }return (<div className="mx-auto py-8"><div className="mb-8"><h1 className="text-2xl font-bold mb-2">User Profile: {userId}</h1>- <div className="text-sm text-gray-400">- {groupedSessions.length > 0- ? `${groupedSessions.length} sessions recorded`- : isLoading- ? "Loading user data..."- : "No sessions found for this user"}- </div>+ <div className="text-sm text-gray-400 mb-6">User activity sessions</div></div>- {isLoading ? (- <div className="space-y-8">- {Array.from({ length: 3 }).map((_, i) => (- <div- key={i}- className="bg-neutral-900 border border-neutral-800 rounded-lg p-6 mb-6"- >- <Skeleton className="h-6 w-48 mb-4" />- <Skeleton className="h-5 w-full mb-2" />- <Skeleton className="h-5 w-full mb-2" />- <Skeleton className="h-5 w-2/3" />- </div>- ))}- </div>- ) : (- <div className="space-y-8">- {groupedSessions.map((session: UserSessionsResponse[number]) => (- <div- key={session.session_id}- className="bg-neutral-900 border border-neutral-800 rounded-lg p-6 mb-6"- >- <div className="mb-4">- <h2 className="text-xl font-medium mb-2">- {DateTime.fromSQL(session.firstTimestamp).toFormat(- "MMM d, yyyy h:mm a"- )}- </h2>- <div className="flex flex-wrap gap-4 text-sm text-gray-400">- <div className="flex items-center gap-2">- <Browser browser={session.browser || "Unknown"} />- {session.browser || "Unknown"}- </div>- <div className="flex items-center gap-2">- <OperatingSystem- os={session.operating_system || "Unknown"}- />- {session.operating_system || "Unknown"}- </div>- <div>{session.device_type || "Unknown Device"}</div>- <div className="flex items-center gap-2">- <CountryFlag country={session.country || "Unknown"} />- {getCountryName(session.country || "Unknown")}- </div>- <div>Duration: {formatDuration(session.duration)}</div>- </div>- </div>-- <div className="space-y-4">- {session.pageviews.map(- (- pageview: {- pathname: string;- querystring: string;- title: string;- timestamp: string;- referrer: string;- },- index: number- ) => {- const fullPath = getFullPath(- pageview.pathname,- pageview.querystring- );- return (- <div- key={index}- className="pl-4 border-l-2 border-neutral-800 grid grid-cols-[100px_1fr_auto] items-center gap-4"- >- <div className="text-gray-400">- {DateTime.fromSQL(pageview.timestamp).toFormat(- "h:mm:ss a"- )}- </div>- <div className="text-gray-200 overflow-hidden">- <a- href={`https://${siteMetadata?.domain}${fullPath}`}- target="_blank"- rel="noopener noreferrer"- className="hover:underline block truncate"- title={`https://${siteMetadata?.domain}${fullPath}`}- >- {fullPath}- </a>- </div>- {pageview.referrer ? (- <ReferrerIcon referrer={pageview.referrer} />- ) : null}- </div>- );- }- )}- </div>- </div>- ))}- </div>- )}+ <SessionsList userId={userId as string} /></div>);}diff --git a/client/src/components/Sessions/SessionCard.tsx b/client/src/components/Sessions/SessionCard.tsxindex 498e25d57..2a3100817 100644--- a/client/src/components/Sessions/SessionCard.tsx+++ b/client/src/components/Sessions/SessionCard.tsx@@ -27,6 +27,7 @@ import { Badge } from "../ui/badge";interface SessionCardProps {session: GetSessionsResponse[number];+ userId?: string;onClick?: () => void;}@@ -50,7 +51,7 @@ function truncatePath(path: string, maxLength: number = 32) {return `${path.substring(0, maxLength)}...`;}-export function SessionCard({ session, onClick }: SessionCardProps) {+export function SessionCard({ session, onClick, userId }: SessionCardProps) {const [expanded, setExpanded] = useState(false);// Calculate session duration in minutes@@ -225,7 +226,7 @@ export function SessionCard({ session, onClick }: SessionCardProps) {</div>{/* Expanded content using SessionDetails component */}- {expanded && <SessionDetails session={session} />}+ {expanded && <SessionDetails session={session} userId={userId} />}</div>);}diff --git a/client/src/components/Sessions/SessionDetails.tsx b/client/src/components/Sessions/SessionDetails.tsxindex e6607a2c6..1ca7d0866 100644--- a/client/src/components/Sessions/SessionDetails.tsx+++ b/client/src/components/Sessions/SessionDetails.tsx@@ -218,9 +218,10 @@ const SessionDetailsTimelineSkeleton = memo(interface SessionDetailsProps {session: GetSessionsResponse[number];+ userId?: string;}-export function SessionDetails({ session }: SessionDetailsProps) {+export function SessionDetails({ session, userId }: SessionDetailsProps) {const {data: sessionDetails,isLoading,@@ -241,6 +242,8 @@ export function SessionDetails({ session }: SessionDetailsProps) {return `${Math.floor(duration.minutes)}m ${Math.floor(duration.seconds)}s`;};+ console.info(userId);+return (<div className="px-4 bg-neutral-900 border-t border-neutral-800">{isLoading ? (@@ -260,11 +263,13 @@ export function SessionDetails({ session }: SessionDetailsProps) {<TabsTrigger value="timeline">Timeline</TabsTrigger><TabsTrigger value="info">Session Info</TabsTrigger></TabsList>- <Link href={`/${site}/user/${sessionDetails.data.session.user_id}`}>- <Button variant={"accent"}>- View User <ArrowRight className="w-4 h-4" />- </Button>- </Link>+ {!userId && (+ <Link href={`/${site}/user/${userId}`}>+ <Button variant={"accent"}>+ View User <ArrowRight className="w-4 h-4" />+ </Button>+ </Link>+ )}</div><TabsContent value="timeline" className="mt-4">diff --git a/client/src/components/Sessions/SessionsList.tsx b/client/src/components/Sessions/SessionsList.tsxindex a339676eb..be07cfcf3 100644--- a/client/src/components/Sessions/SessionsList.tsx+++ b/client/src/components/Sessions/SessionsList.tsx@@ -13,6 +13,8 @@ export default function SessionsList({ userId }: { userId?: string }) {isFetchingNextPage,} = useGetSessionsInfinite(userId);+ console.info(userId);+// Combine all pages of dataconst flattenedData = useMemo(() => {if (!data) return [];@@ -72,6 +74,7 @@ export default function SessionsList({ userId }: { userId?: string }) {<SessionCardkey={`${session.session_id}-${index}`}session={session}+ userId={userId}/>)))}