Skip to content

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

feat(retention): use local data and time formats

Andrii Andrushchyshync0c3c043163b
diff --git a/client/src/app/[site]/retention/RetentionChart.tsx b/client/src/app/[site]/retention/RetentionChart.tsx
index a76df4c88..306cb718b 100644
--- a/client/src/app/[site]/retention/RetentionChart.tsx
+++ b/client/src/app/[site]/retention/RetentionChart.tsx
@@ -16,6 +16,9 @@ interface RetentionChartProps {
mode: RetentionMode;
}
+// Detect user locale and 12h/24h preference
+const userLocale = typeof navigator !== "undefined" ? navigator.language : "en";
+
// Vibrant color palette for different cohorts using Tailwind CSS HSL variables
const cohortColors = [
"hsl(var(--accent-500))", // Primary accent color
@@ -77,7 +80,7 @@ export function RetentionChart({ data, isLoading, mode }: RetentionChartProps) {
// Format the date label based on mode
let formattedDate: string;
if (mode === "day") {
- formattedDate = DateTime.fromISO(cohortKey).toFormat("MMM dd");
+ formattedDate = DateTime.fromISO(cohortKey).setLocale(userLocale).toFormat("MMM dd");
} else {
// For weekly mode
const startDate = DateTime.fromISO(cohortKey);
@@ -85,11 +88,11 @@ export function RetentionChart({ data, isLoading, mode }: RetentionChartProps) {
// If same month, don't repeat month
if (startDate.month === endDate.month) {
- formattedDate = `${startDate.toFormat("MMM dd")}-${endDate.toFormat(
+ formattedDate = `${startDate.setLocale(userLocale).toFormat("MMM dd")}-${endDate.setLocale(userLocale).toFormat(
"dd"
)}`;
} else {
- formattedDate = `${startDate.toFormat("MMM dd")}-${endDate.toFormat(
+ formattedDate = `${startDate.setLocale(userLocale).toFormat("MMM dd")}-${endDate.setLocale(userLocale).toFormat(
"MMM dd"
)}`;
}
@@ -213,23 +216,23 @@ export function RetentionChart({ data, isLoading, mode }: RetentionChartProps) {
if (originalCohortKey) {
if (mode === "day") {
cohortDateDisplay =
- DateTime.fromISO(originalCohortKey).toFormat("MMM dd, yyyy");
+ DateTime.fromISO(originalCohortKey).setLocale(userLocale).toFormat("MMM dd, yyyy");
} else {
const startDate = DateTime.fromISO(originalCohortKey);
const endDate = startDate.plus({ days: 6 });
if (startDate.month === endDate.month) {
- cohortDateDisplay = `${startDate.toFormat(
+ cohortDateDisplay = `${startDate.setLocale(userLocale).toFormat(
"MMM dd"
- )} - ${endDate.toFormat("dd, yyyy")}`;
+ )} - ${endDate.setLocale(userLocale).toFormat("dd, yyyy")}`;
} else if (startDate.year === endDate.year) {
- cohortDateDisplay = `${startDate.toFormat(
+ cohortDateDisplay = `${startDate.setLocale(userLocale).toFormat(
"MMM dd"
- )} - ${endDate.toFormat("MMM dd, yyyy")}`;
+ )} - ${endDate.setLocale(userLocale).toFormat("MMM dd, yyyy")}`;
} else {
- cohortDateDisplay = `${startDate.toFormat(
+ cohortDateDisplay = `${startDate.setLocale(userLocale).toFormat(
"MMM dd, yyyy"
- )} - ${endDate.toFormat("MMM dd, yyyy")}`;
+ )} - ${endDate.setLocale(userLocale).toFormat("MMM dd, yyyy")}`;
}
}
}
diff --git a/client/src/app/[site]/retention/page.tsx b/client/src/app/[site]/retention/page.tsx
index 45d60fc9f..b40dc87cb 100644
--- a/client/src/app/[site]/retention/page.tsx
+++ b/client/src/app/[site]/retention/page.tsx
@@ -26,6 +26,9 @@ import { RetentionChart } from "./RetentionChart";
import { NothingFound } from "../../../components/NothingFound";
import { useSetPageTitle } from "../../../hooks/useSetPageTitle";
+// Detect user locale and 12h/24h preference
+const userLocale = typeof navigator !== "undefined" ? navigator.language : "en";
+
// Available time range options (in days)
const RANGE_OPTIONS = [
{ value: "7", label: "Last 7 days" },
@@ -99,7 +102,7 @@ export default function RetentionPage() {
// Function to format date based on mode
const formatDate = (dateStr: string) => {
if (mode === "day") {
- return DateTime.fromISO(dateStr).toFormat("MMM dd, yyyy");
+ return DateTime.fromISO(dateStr).setLocale(userLocale).toFormat("MMM dd, yyyy");
} else {
// For weekly mode, show start and end dates of the week
const startDate = DateTime.fromISO(dateStr);
@@ -107,17 +110,17 @@ export default function RetentionPage() {
// If same month, don't repeat month name
if (startDate.month === endDate.month) {
- return `${startDate.toFormat("MMM dd")} - ${endDate.toFormat(
+ return `${startDate.setLocale(userLocale).toFormat("MMM dd")} - ${endDate.setLocale(userLocale).toFormat(
"dd, yyyy"
)}`;
} else if (startDate.year === endDate.year) {
// Different months, same year
- return `${startDate.toFormat("MMM dd")} - ${endDate.toFormat(
+ return `${startDate.setLocale(userLocale).toFormat("MMM dd")} - ${endDate.setLocale(userLocale).toFormat(
"MMM dd, yyyy"
)}`;
} else {
// Different years
- return `${startDate.toFormat("MMM dd, yyyy")} - ${endDate.toFormat(
+ return `${startDate.setLocale(userLocale).toFormat("MMM dd, yyyy")} - ${endDate.setLocale(userLocale).toFormat(
"MMM dd, yyyy"
)}`;
}