Skip to content

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

Refactor RetentionPage and RetentionChart components for improved UI and tooltip functionality

Bill Yang41f1281ee394
diff --git a/client/src/app/[site]/retention/RetentionChart.tsx b/client/src/app/[site]/retention/RetentionChart.tsx
index e206bda40..bfc42381c 100644
--- a/client/src/app/[site]/retention/RetentionChart.tsx
+++ b/client/src/app/[site]/retention/RetentionChart.tsx
@@ -154,12 +154,14 @@ export function RetentionChart({ data, isLoading, mode }: RetentionChartProps) {
tickSize: 5,
tickPadding: 5,
tickRotation: 0,
+ tickValues: 5,
format: value => `${value}%`,
}}
enableGridX={true}
+ gridYValues={5}
gridXValues={Array.from({ length: data.maxPeriods + 1 }, (_, i) => i)}
colors={{ datum: "color" }}
- useMesh={true}
+ enableSlices="x"
pointSize={0}
legends={[
{
@@ -188,45 +190,37 @@ export function RetentionChart({ data, isLoading, mode }: RetentionChartProps) {
itemTextColor: resolvedTheme === "dark" ? "hsl(var(--neutral-200))" : "hsl(var(--neutral-700))",
},
]}
- tooltip={({ point }) => {
- const value = point.data.y as number | null;
- const xValue = point.data.x as number;
-
- // Find the original cohort date by matching the formatted label
- const cohortEntry = chartData.find(series => series.id === point.seriesId);
- const cohortIndex = cohortEntry ? chartData.indexOf(cohortEntry) : -1;
- const originalCohortKey = cohortIndex >= 0 && cohortKeys && cohortKeys[cohortIndex];
-
- // Format full date for tooltip
- let cohortDateDisplay = point.seriesId;
- if (originalCohortKey) {
- const startDate = DateTime.fromISO(originalCohortKey);
- if (mode === "day") {
- cohortDateDisplay = startDate.toFormat("MMM dd, yyyy");
- } else {
- const endDate = startDate.plus({ days: 6 });
-
- if (startDate.month === endDate.month) {
- cohortDateDisplay = `${startDate.toFormat("MMM dd")} - ${endDate.toFormat("dd, yyyy")}`;
- } else if (startDate.year === endDate.year) {
- cohortDateDisplay = `${startDate.toFormat("MMM dd")} - ${endDate.toFormat("MMM dd, yyyy")}`;
- } else {
- cohortDateDisplay = `${startDate.toFormat("MMM dd, yyyy")} - ${endDate.toFormat("MMM dd, yyyy")}`;
- }
- }
- }
+ sliceTooltip={({ slice }) => {
+ const xValue = slice.points[0]?.data.x as number;
return (
<ChartTooltip>
<div className="p-2 text-sm">
- <div className="font-medium mb-1" style={{ color: point.seriesColor }}>
- Cohort: {cohortDateDisplay}
+ <div className="font-medium mb-2 text-neutral-700 dark:text-neutral-200">
+ {mode === "day" ? "Day" : "Week"} {xValue}
</div>
- <div className="flex justify-between w-48 text-neutral-700 dark:text-neutral-200">
- <span>
- {mode === "day" ? "Day" : "Week"} {xValue}
- </span>
- <span className="font-medium">{value !== null ? `${value.toFixed(1)}%` : "-"}</span>
+ <div className="flex flex-col gap-1">
+ {slice.points.map((point: any) => {
+ const value = point.data.y as number | null;
+ // Point ID format is "serieId.index", extract the serie ID
+ const cohortLabel = String(point.id).split(".")[0];
+ // Get color from chartData since point.serieColor may be undefined
+ const seriesData = chartData.find(s => s.id === cohortLabel);
+ const color = point.serieColor || seriesData?.color || point.color;
+ return (
+ <div key={point.id} className="flex justify-between items-center gap-4">
+ <div className="flex items-center gap-2">
+ <div className="w-1 h-3 rounded-[3px]" style={{ backgroundColor: color }} />
+ <span className="text-neutral-600 dark:text-neutral-300 whitespace-nowrap">
+ {cohortLabel}
+ </span>
+ </div>
+ <span className="font-medium text-neutral-700 dark:text-neutral-200">
+ {value !== null ? `${value.toFixed(1)}%` : "-"}
+ </span>
+ </div>
+ );
+ })}
</div>
</div>
</ChartTooltip>
diff --git a/client/src/app/[site]/retention/page.tsx b/client/src/app/[site]/retention/page.tsx
index 2f1ff1086..21d3b6665 100644
--- a/client/src/app/[site]/retention/page.tsx
+++ b/client/src/app/[site]/retention/page.tsx
@@ -27,18 +27,21 @@ const RANGE_OPTIONS = [
];
// Dynamic color function that creates a smooth gradient based on retention percentage
-const getRetentionColor = (percentage: number | null, isDark: boolean): { backgroundColor: string; textColor: string } => {
+const getRetentionColor = (
+ percentage: number | null,
+ isDark: boolean
+): { backgroundColor: string; textColor: string } => {
if (percentage === null || isNaN(percentage)) {
return {
- backgroundColor: isDark ? "rgb(38, 38, 38)" : "rgb(245, 245, 245)", // bg-neutral-800 : bg-neutral-100
+ backgroundColor: isDark ? "rgb(38, 38, 38)" : "rgb(250, 250, 250)", // bg-neutral-800 : bg-neutral-50
textColor: "transparent",
};
}
if (percentage === 0) {
return {
- backgroundColor: isDark ? "rgb(38, 38, 38)" : "rgb(245, 245, 245)", // bg-neutral-800 : bg-neutral-100
- textColor: isDark ? "white" : "black",
+ backgroundColor: isDark ? "rgb(38, 38, 38)" : "rgb(250, 250, 250)", // bg-neutral-800 : bg-neutral-50
+ textColor: isDark ? "white" : "rgb(64, 64, 64)",
};
}
@@ -75,9 +78,9 @@ export default function RetentionPage() {
const [isDark, setIsDark] = useState(true);
// Detect theme on client side
- if (typeof window !== 'undefined') {
+ if (typeof window !== "undefined") {
const checkTheme = () => {
- const isDarkMode = document.documentElement.classList.contains('dark');
+ const isDarkMode = document.documentElement.classList.contains("dark");
if (isDarkMode !== isDark) {
setIsDark(isDarkMode);
}
@@ -221,19 +224,19 @@ export default function RetentionPage() {
) : data ? (
<div className="overflow-x-auto">
<div
- className="inline-grid gap-px bg-neutral-200 dark:bg-neutral-900 rounded-lg shadow-lg"
+ className="inline-grid gap-px bg-neutral-50 dark:bg-neutral-900 rounded-lg"
style={{
gridTemplateColumns: `minmax(120px, auto) repeat(${data.maxPeriods + 1}, minmax(80px, auto))`,
}}
>
{/* Header Row */}
- <div className="p-2 text-sm font-semibold bg-neutral-50 dark:bg-neutral-900 text-neutral-900 dark:text-neutral-100 text-center sticky left-0 z-10 border-b border-r border-neutral-200 dark:border-neutral-700">
+ <div className="p-2 text-sm font-semibold bg-white dark:bg-neutral-900 text-neutral-600 dark:text-neutral-100 text-center sticky left-0 z-10 border-b border-r border-neutral-50 dark:border-neutral-700">
Cohort
</div>
{periodHeaders.map(header => (
<div
key={header}
- className="p-2 text-sm bg-neutral-50 dark:bg-neutral-900 text-neutral-900 dark:text-neutral-100 text-center border-b border-neutral-200 dark:border-neutral-700"
+ className="p-2 text-sm bg-white dark:bg-neutral-900 text-neutral-600 dark:text-neutral-100 text-center border-b border-neutral-50 dark:border-neutral-700"
>
{header}
</div>
@@ -243,9 +246,11 @@ export default function RetentionPage() {
{cohortKeys.map(cohortPeriod => (
<Fragment key={cohortPeriod}>
{/* Cohort Info Cell */}
- <div className="py-2 px-2 bg-neutral-50 dark:bg-neutral-900 text-sm sticky left-0 z-10 border-r border-neutral-200 dark:border-neutral-800">
- <div className="whitespace-nowrap text-neutral-900 dark:text-neutral-100">{formatDate(cohortPeriod)}</div>
- <div className="text-xs text-neutral-600 dark:text-neutral-300 mt-1 whitespace-nowrap">
+ <div className="py-2 px-2 bg-white dark:bg-neutral-900 text-sm sticky left-0 z-10 border-r border-neutral-50 dark:border-neutral-800">
+ <div className="whitespace-nowrap text-neutral-700 dark:text-neutral-100">
+ {formatDate(cohortPeriod)}
+ </div>
+ <div className="text-xs text-neutral-500 dark:text-neutral-300 mt-1 whitespace-nowrap">
{data.cohorts[cohortPeriod].size.toLocaleString()} users
</div>
</div>
@@ -255,7 +260,7 @@ export default function RetentionPage() {
return (
<div
key={`${cohortPeriod}-period-${index}`}
- className="m-[2px] text-center flex items-center justify-center font-medium transition-colors duration-150 bg-neutral-100 dark:bg-neutral-900 rounded-md"
+ className="m-[2px] text-center flex items-center justify-center font-medium transition-colors duration-150 bg-white dark:bg-neutral-900 rounded-md"
style={{
backgroundColor,
color: textColor,