🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.
Add event type filtering to SessionDetails component
Bill Yang6e77c2b2d479
diff --git a/client/src/components/EventTypeFilter.tsx b/client/src/components/EventTypeFilter.tsxnew file mode 100644index 000000000..0249cff83--- /dev/null+++ b/client/src/components/EventTypeFilter.tsx@@ -0,0 +1,98 @@+"use client";++import { Eye, ExternalLink, MousePointerClick, TriangleAlert } from "lucide-react";+import { cn } from "@/lib/utils";++export type EventType = "pageview" | "custom_event" | "error" | "outbound";++interface EventTypeOption {+ value: EventType;+ label: string;+ color: string;+ icon: React.ReactNode;+}++const EVENT_TYPE_OPTIONS: EventTypeOption[] = [+ {+ value: "pageview",+ label: "Pageviews",+ color: "rgb(96, 165, 250)", // blue-400+ icon: <Eye className="h-3 w-3" />,+ },+ {+ value: "custom_event",+ label: "Events",+ color: "rgb(251, 191, 36)", // amber-400+ icon: <MousePointerClick className="h-3 w-3" />,+ },+ {+ value: "outbound",+ label: "Outbound",+ color: "rgb(168, 85, 247)", // purple-500+ icon: <ExternalLink className="h-3 w-3" />,+ },+ {+ value: "error",+ label: "Errors",+ color: "rgb(239, 68, 68)", // red-500+ icon: <TriangleAlert className="h-3 w-3" />,+ },+];++interface EventTypeFilterProps {+ visibleTypes: Set<string>;+ onToggle: (type: string) => void;+ counts?: {+ pageview?: number;+ custom_event?: number;+ error?: number;+ outbound?: number;+ };+}++export function EventTypeFilter({ visibleTypes, onToggle, counts }: EventTypeFilterProps) {+ return (+ <div className="flex items-center space-x-2">+ {EVENT_TYPE_OPTIONS.map((option) => {+ const isSelected = visibleTypes.has(option.value);+ const count = counts?.[option.value];++ return (+ <button+ key={option.value}+ onClick={() => onToggle(option.value)}+ className={cn(+ "flex items-center space-x-1.5 px-2 py-1 rounded text-xs font-medium transition-all",+ isSelected+ ? "bg-neutral-200 dark:bg-neutral-800 text-neutral-900 dark:text-white"+ : "bg-neutral-100 dark:bg-neutral-900 text-neutral-600 dark:text-neutral-500 hover:text-neutral-700 dark:hover:text-neutral-400"+ )}+ >+ <div+ className={cn(+ "w-3 h-3 rounded-sm flex items-center justify-center transition-opacity",+ isSelected ? "opacity-100" : "opacity-30"+ )}+ style={{ color: option.color }}+ >+ {option.icon}+ </div>+ <span>{option.label}</span>+ {count !== undefined && count > 0 && (+ <span+ className={cn(+ "ml-1 px-1.5 py-0.5 rounded-full text-[10px] leading-none",+ isSelected+ ? "bg-neutral-300 dark:bg-neutral-700 text-neutral-700 dark:text-neutral-300"+ : "bg-neutral-200 dark:bg-neutral-800 text-neutral-500 dark:text-neutral-500"+ )}+ >+ {count}+ </span>+ )}+ </button>+ );+ })}+ </div>+ );+}diff --git a/client/src/components/Sessions/SessionDetails.tsx b/client/src/components/Sessions/SessionDetails.tsxindex f9f44c4f4..671f904fb 100644--- a/client/src/components/Sessions/SessionDetails.tsx+++ b/client/src/components/Sessions/SessionDetails.tsx@@ -9,7 +9,7 @@ import { ArrowRight, Clock, ExternalLink, Loader2, Monitor, Smartphone, Tablet,import { DateTime } from "luxon";import Link from "next/link";import { useParams } from "next/navigation";-import { memo, useMemo } from "react";+import { memo, useMemo, useState } from "react";import { useGetSessionDetailsInfinite } from "../../api/analytics/hooks/useGetUserSessions";import { GetSessionsResponse, SessionEvent } from "../../api/analytics/endpoints";import { Browser } from "../../app/[site]/components/shared/icons/Browser";@@ -20,6 +20,7 @@ import { useGetRegionName } from "../../lib/geo";import { cn, getCountryName, getLanguageName } from "../../lib/utils";import { Avatar, generateName } from "../Avatar";import { EventIcon, PageviewIcon } from "../EventIcons";+import { EventTypeFilter } from "../EventTypeFilter";import { IdentifiedBadge } from "../IdentifiedBadge";import { Button } from "../ui/button";@@ -303,6 +304,28 @@ export function SessionDetails({ session, userId }: SessionDetailsProps) {return allEvents.filter((p: SessionEvent) => p.type === "outbound").length;}, [allEvents]);+ // Event type filter state+ const [visibleEventTypes, setVisibleEventTypes] = useState<Set<string>>(+ new Set(["pageview", "custom_event", "outbound"])+ );++ const toggleEventType = (type: string) => {+ setVisibleEventTypes((prev) => {+ const newSet = new Set(prev);+ if (newSet.has(type)) {+ newSet.delete(type);+ } else {+ newSet.add(type);+ }+ return newSet;+ });+ };++ // Filter events based on visible types+ const filteredEvents = useMemo(() => {+ return allEvents.filter((event: SessionEvent) => visibleEventTypes.has(event.type));+ }, [allEvents, visibleEventTypes]);+const { getRegionName } = useGetRegionName();const isIdentified = !!session.identified_user_id;@@ -335,14 +358,26 @@ export function SessionDetails({ session, userId }: SessionDetailsProps) {)}</div>- <TabsContent value="timeline" className="mt-4">+ <TabsContent value="timeline">+ <div className="mb-4">+ <EventTypeFilter+ visibleTypes={visibleEventTypes}+ onToggle={toggleEventType}+ counts={{+ pageview: totalPageviews,+ custom_event: totalEvents,+ error: totalErrors,+ outbound: totalOutbound,+ }}+ />+ </div><div className="mb-4 px-1">- {allEvents.map((pageview: SessionEvent, index: number) => {+ {filteredEvents.map((pageview: SessionEvent, index: number) => {// Determine the next timestamp for duration calculation// For the last item, use the session end timelet nextTimestamp;- if (index < allEvents.length - 1) {- nextTimestamp = allEvents[index + 1].timestamp;+ if (index < filteredEvents.length - 1) {+ nextTimestamp = filteredEvents[index + 1].timestamp;} else if (sessionDetails) {nextTimestamp = sessionDetails.session_end;}@@ -352,7 +387,7 @@ export function SessionDetails({ session, userId }: SessionDetailsProps) {key={`${pageview.timestamp}-${index}`}item={pageview}index={index}- isLast={index === allEvents.length - 1 && !hasNextPage}+ isLast={index === filteredEvents.length - 1 && !hasNextPage}nextTimestamp={nextTimestamp}/>);@@ -404,8 +439,8 @@ export function SessionDetails({ session, userId }: SessionDetailsProps) {<span className="font-medium text-neutral-600 dark:text-neutral-300">{isIdentified? (session.traits?.username as string) ||- (session.traits?.name as string) ||- session.identified_user_id+ (session.traits?.name as string) ||+ session.identified_user_id: generateName(sessionDetails.user_id)}</span>{isIdentified && <IdentifiedBadge traits={session.traits} />}