Skip to content

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

Enhance DateSelector component with well-known time presets and improve URL parameter handling

Bill Yang3dde4e2bb4d3
diff --git a/.claude/settings.local.json b/.claude/settings.local.json
index 8bc639f8a..ce0e01c10 100644
--- a/.claude/settings.local.json
+++ b/.claude/settings.local.json
@@ -4,7 +4,9 @@
"Bash(do sed -i '' 's|from \"\"../../../../../|from \"\"../../../../../../|g' \"$file\")",
"Bash(do sed -i '' 's|from \"\"../../utils/|from \"\"../../../utils/|g' \"$file\")",
"Bash([ -d hooks ])",
- "WebFetch(domain:carto.com)"
+ "WebFetch(domain:carto.com)",
+ "Bash(npx tsc:*)",
+ "Bash(npm run lint)"
],
"deny": [],
"ask": []
diff --git a/client/src/components/DateSelector/DateSelector.tsx b/client/src/components/DateSelector/DateSelector.tsx
index a15b894e7..8bacf507d 100644
--- a/client/src/components/DateSelector/DateSelector.tsx
+++ b/client/src/components/DateSelector/DateSelector.tsx
@@ -12,11 +12,34 @@ import { DateTime } from "luxon";
import { CustomDateRangePicker } from "./CustomDateRangePicker";
import { Time } from "./types";
+// Convert wellKnown kebab-case to display labels
+const wellKnownLabels: Record<string, string> = {
+ today: "Today",
+ yesterday: "Yesterday",
+ "last-3-days": "Last 3 Days",
+ "last-7-days": "Last 7 Days",
+ "last-14-days": "Last 14 Days",
+ "last-30-days": "Last 30 Days",
+ "last-60-days": "Last 60 Days",
+ "last-30-minutes": "Last 30 Minutes",
+ "last-1-hour": "Last 1 Hour",
+ "last-6-hours": "Last 6 Hours",
+ "last-24-hours": "Last 24 Hours",
+ "this-week": "This Week",
+ "last-week": "Last Week",
+ "this-month": "This Month",
+ "last-month": "Last Month",
+ "this-year": "This Year",
+ "all-time": "All Time",
+};
+
const getLabel = (time: Time) => {
+ // Check for wellKnown preset first
+ if (time.wellKnown && wellKnownLabels[time.wellKnown]) {
+ return wellKnownLabels[time.wellKnown];
+ }
+
if (time.mode === "range") {
- if (time.wellKnown) {
- return `${time.wellKnown}`;
- }
const startFormatted = DateTime.fromISO(time.startDate).toFormat("EEEE, MMM d");
const endFormatted = DateTime.fromISO(time.endDate).toFormat("EEEE, MMM d");
return `${startFormatted} - ${endFormatted}`;
@@ -91,6 +114,7 @@ export function DateSelector({
setTime({
mode: "day",
day: DateTime.now().toISODate(),
+ wellKnown: "today",
})
}
>
@@ -102,7 +126,7 @@ export function DateSelector({
mode: "range",
startDate: DateTime.now().minus({ days: 2 }).toISODate(),
endDate: DateTime.now().toISODate(),
- wellKnown: "Last 3 days",
+ wellKnown: "last-3-days",
})
}
>
@@ -114,7 +138,7 @@ export function DateSelector({
mode: "range",
startDate: DateTime.now().minus({ days: 6 }).toISODate(),
endDate: DateTime.now().toISODate(),
- wellKnown: "Last 7 days",
+ wellKnown: "last-7-days",
})
}
>
@@ -126,7 +150,7 @@ export function DateSelector({
mode: "range",
startDate: DateTime.now().minus({ days: 13 }).toISODate(),
endDate: DateTime.now().toISODate(),
- wellKnown: "Last 14 days",
+ wellKnown: "last-14-days",
})
}
>
@@ -138,7 +162,7 @@ export function DateSelector({
mode: "range",
startDate: DateTime.now().minus({ days: 29 }).toISODate(),
endDate: DateTime.now().toISODate(),
- wellKnown: "Last 30 days",
+ wellKnown: "last-30-days",
})
}
>
@@ -150,7 +174,7 @@ export function DateSelector({
mode: "range",
startDate: DateTime.now().minus({ days: 59 }).toISODate(),
endDate: DateTime.now().toISODate(),
- wellKnown: "Last 60 days",
+ wellKnown: "last-60-days",
})
}
>
@@ -165,6 +189,7 @@ export function DateSelector({
mode: "past-minutes",
pastMinutesStart: 30,
pastMinutesEnd: 0,
+ wellKnown: "last-30-minutes",
})
}
>
@@ -176,6 +201,7 @@ export function DateSelector({
mode: "past-minutes",
pastMinutesStart: 60,
pastMinutesEnd: 0,
+ wellKnown: "last-1-hour",
})
}
>
@@ -187,6 +213,7 @@ export function DateSelector({
mode: "past-minutes",
pastMinutesStart: 360,
pastMinutesEnd: 0,
+ wellKnown: "last-6-hours",
})
}
>
@@ -198,6 +225,7 @@ export function DateSelector({
mode: "past-minutes",
pastMinutesStart: 1440,
pastMinutesEnd: 0,
+ wellKnown: "last-24-hours",
})
}
>
@@ -211,6 +239,7 @@ export function DateSelector({
setTime({
mode: "week",
week: DateTime.now().startOf("week").toISODate(),
+ wellKnown: "this-week",
})
}
>
@@ -221,6 +250,7 @@ export function DateSelector({
setTime({
mode: "month",
month: DateTime.now().startOf("month").toISODate(),
+ wellKnown: "this-month",
})
}
>
@@ -231,6 +261,7 @@ export function DateSelector({
setTime({
mode: "year",
year: DateTime.now().startOf("year").toISODate(),
+ wellKnown: "this-year",
})
}
>
@@ -240,6 +271,7 @@ export function DateSelector({
onClick={() =>
setTime({
mode: "all-time",
+ wellKnown: "all-time",
})
}
>
diff --git a/client/src/components/DateSelector/types.ts b/client/src/components/DateSelector/types.ts
index f60325441..75e9d77c0 100644
--- a/client/src/components/DateSelector/types.ts
+++ b/client/src/components/DateSelector/types.ts
@@ -1,38 +1,44 @@
export type DateMode = {
mode: "day";
day: string;
+ wellKnown?: "today" | "yesterday";
};
export type DateRangeMode = {
mode: "range";
startDate: string;
endDate: string;
- wellKnown?: "Last 3 days" | "Last 7 days" | "Last 14 days" | "Last 30 days" | "Last 60 days";
+ wellKnown?: "last-3-days" | "last-7-days" | "last-14-days" | "last-30-days" | "last-60-days";
};
export type WeekMode = {
mode: "week";
week: string;
+ wellKnown?: "this-week" | "last-week";
};
export type MonthMode = {
mode: "month";
month: string;
+ wellKnown?: "this-month" | "last-month";
};
export type YearMode = {
mode: "year";
year: string;
+ wellKnown?: "this-year";
};
export type AllTimeMode = {
mode: "all-time";
+ wellKnown?: "all-time";
};
export type PastMinutesMode = {
mode: "past-minutes";
pastMinutesStart: number;
pastMinutesEnd: number;
+ wellKnown?: "last-30-minutes" | "last-1-hour" | "last-6-hours" | "last-24-hours";
};
export type Time = DateMode | DateRangeMode | WeekMode | MonthMode | YearMode | AllTimeMode | PastMinutesMode;
diff --git a/client/src/lib/urlParams.ts b/client/src/lib/urlParams.ts
index 66ea3be74..30cd8cc71 100644
--- a/client/src/lib/urlParams.ts
+++ b/client/src/lib/urlParams.ts
@@ -1,9 +1,68 @@
import { Filter, TimeBucket } from "@rybbit/shared";
+import { DateTime } from "luxon";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import React, { useEffect } from "react";
import { Time } from "../components/DateSelector/types";
import { StatType, useStore } from "./store";
+// Map of wellKnown presets to their dynamic time calculations
+const wellKnownPresets: Record<string, () => Time> = {
+ today: () => ({ mode: "day", day: DateTime.now().toISODate(), wellKnown: "today" }),
+ yesterday: () => ({ mode: "day", day: DateTime.now().minus({ days: 1 }).toISODate(), wellKnown: "yesterday" }),
+ "last-3-days": () => ({
+ mode: "range",
+ startDate: DateTime.now().minus({ days: 2 }).toISODate(),
+ endDate: DateTime.now().toISODate(),
+ wellKnown: "last-3-days",
+ }),
+ "last-7-days": () => ({
+ mode: "range",
+ startDate: DateTime.now().minus({ days: 6 }).toISODate(),
+ endDate: DateTime.now().toISODate(),
+ wellKnown: "last-7-days",
+ }),
+ "last-14-days": () => ({
+ mode: "range",
+ startDate: DateTime.now().minus({ days: 13 }).toISODate(),
+ endDate: DateTime.now().toISODate(),
+ wellKnown: "last-14-days",
+ }),
+ "last-30-days": () => ({
+ mode: "range",
+ startDate: DateTime.now().minus({ days: 29 }).toISODate(),
+ endDate: DateTime.now().toISODate(),
+ wellKnown: "last-30-days",
+ }),
+ "last-60-days": () => ({
+ mode: "range",
+ startDate: DateTime.now().minus({ days: 59 }).toISODate(),
+ endDate: DateTime.now().toISODate(),
+ wellKnown: "last-60-days",
+ }),
+ "this-week": () => ({ mode: "week", week: DateTime.now().startOf("week").toISODate(), wellKnown: "this-week" }),
+ "last-week": () => ({
+ mode: "week",
+ week: DateTime.now().minus({ weeks: 1 }).startOf("week").toISODate(),
+ wellKnown: "last-week",
+ }),
+ "this-month": () => ({
+ mode: "month",
+ month: DateTime.now().startOf("month").toISODate(),
+ wellKnown: "this-month",
+ }),
+ "last-month": () => ({
+ mode: "month",
+ month: DateTime.now().minus({ months: 1 }).startOf("month").toISODate(),
+ wellKnown: "last-month",
+ }),
+ "this-year": () => ({ mode: "year", year: DateTime.now().startOf("year").toISODate(), wellKnown: "this-year" }),
+ "last-30-minutes": () => ({ mode: "past-minutes", pastMinutesStart: 30, pastMinutesEnd: 0, wellKnown: "last-30-minutes" }),
+ "last-1-hour": () => ({ mode: "past-minutes", pastMinutesStart: 60, pastMinutesEnd: 0, wellKnown: "last-1-hour" }),
+ "last-6-hours": () => ({ mode: "past-minutes", pastMinutesStart: 360, pastMinutesEnd: 0, wellKnown: "last-6-hours" }),
+ "last-24-hours": () => ({ mode: "past-minutes", pastMinutesStart: 1440, pastMinutesEnd: 0, wellKnown: "last-24-hours" }),
+ "all-time": () => ({ mode: "all-time", wellKnown: "all-time" }),
+};
+
// Serialize store state to URL parameters
const serializeStateToUrl = (
time: Time,
@@ -15,23 +74,27 @@ const serializeStateToUrl = (
// Serialize time
params.set("timeMode", time.mode);
- if (time.mode === "day") {
- params.set("day", time.day);
- } else if (time.mode === "range") {
- params.set("startDate", time.startDate);
- params.set("endDate", time.endDate);
- if (time.wellKnown) {
- params.set("wellKnown", time.wellKnown);
+
+ // If wellKnown preset, only store that
+ if (time.wellKnown) {
+ params.set("wellKnown", time.wellKnown);
+ } else {
+ // Otherwise store explicit date fields
+ if (time.mode === "day") {
+ params.set("day", time.day);
+ } else if (time.mode === "range") {
+ params.set("startDate", time.startDate);
+ params.set("endDate", time.endDate);
+ } else if (time.mode === "week") {
+ params.set("week", time.week);
+ } else if (time.mode === "month") {
+ params.set("month", time.month);
+ } else if (time.mode === "year") {
+ params.set("year", time.year);
+ } else if (time.mode === "past-minutes") {
+ params.set("pastMinutesStart", time.pastMinutesStart.toString());
+ params.set("pastMinutesEnd", time.pastMinutesEnd.toString());
}
- } else if (time.mode === "week") {
- params.set("week", time.week);
- } else if (time.mode === "month") {
- params.set("month", time.month);
- } else if (time.mode === "year") {
- params.set("year", time.year);
- } else if (time.mode === "past-minutes") {
- params.set("pastMinutesStart", time.pastMinutesStart.toString());
- params.set("pastMinutesEnd", time.pastMinutesEnd.toString());
}
// Serialize bucket
@@ -66,54 +129,34 @@ const deserializeUrlToState = (
// Deserialize time
const timeMode = searchParams.get("timeMode") as Time["mode"] | null;
- if (timeMode) {
- if (timeMode === "past-minutes") {
- const pastMinutesStart = searchParams.get("pastMinutesStart");
- const pastMinutesEnd = searchParams.get("pastMinutesEnd");
- if (pastMinutesStart && pastMinutesEnd) {
- result.time = {
- mode: "past-minutes",
- pastMinutesStart: Number(pastMinutesStart),
- pastMinutesEnd: Number(pastMinutesEnd),
- };
- }
- } else if (timeMode === "day") {
+ const wellKnown = searchParams.get("wellKnown");
+
+ // Try to resolve wellKnown preset first
+ if (wellKnown && wellKnownPresets[wellKnown]) {
+ result.time = wellKnownPresets[wellKnown]();
+ } else if (timeMode) {
+ // Fallback to explicit date parameters
+ if (timeMode === "day") {
const day = searchParams.get("day");
- if (day) {
- result.time = { mode: "day", day };
- }
+ if (day) result.time = { mode: "day", day };
} else if (timeMode === "range") {
const startDate = searchParams.get("startDate");
const endDate = searchParams.get("endDate");
- if (startDate && endDate) {
- const wellKnown = searchParams.get("wellKnown") as
- | "Last 3 days"
- | "Last 7 days"
- | "Last 14 days"
- | "Last 30 days"
- | "Last 60 days"
- | undefined;
- result.time = {
- mode: "range",
- startDate,
- endDate,
- wellKnown,
- };
- }
+ if (startDate && endDate) result.time = { mode: "range", startDate, endDate };
} else if (timeMode === "week") {
const week = searchParams.get("week");
- if (week) {
- result.time = { mode: "week", week };
- }
+ if (week) result.time = { mode: "week", week };
} else if (timeMode === "month") {
const month = searchParams.get("month");
- if (month) {
- result.time = { mode: "month", month };
- }
+ if (month) result.time = { mode: "month", month };
} else if (timeMode === "year") {
const year = searchParams.get("year");
- if (year) {
- result.time = { mode: "year", year };
+ if (year) result.time = { mode: "year", year };
+ } else if (timeMode === "past-minutes") {
+ const pastMinutesStart = searchParams.get("pastMinutesStart");
+ const pastMinutesEnd = searchParams.get("pastMinutesEnd");
+ if (pastMinutesStart && pastMinutesEnd) {
+ result.time = { mode: "past-minutes", pastMinutesStart: Number(pastMinutesStart), pastMinutesEnd: Number(pastMinutesEnd) };
}
} else if (timeMode === "all-time") {
result.time = { mode: "all-time" };
@@ -176,7 +219,19 @@ export const useSyncStateWithUrl = () => {
if (!pathname) return false;
const pathParts = pathname.split("/");
if (pathParts.length < 3) return false;
- return ["main", "sessions", "users", "performance"].includes(pathParts[2]);
+ return [
+ "main",
+ "sessions",
+ "users",
+ "performance",
+ "globe",
+ "goals",
+ "events",
+ "funnels",
+ "journeys",
+ "errors",
+ "pages",
+ ].includes(pathParts[2]);
};
// Initialize from URL params after site is set