Skip to content

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

refactor(duration): simplify codebase and always use total seconds

Andrii Andrushchyshyn5d3f7b6e1026
diff --git a/client/src/app/[site]/user/[userId]/page.tsx b/client/src/app/[site]/user/[userId]/page.tsx
index de65d14d2..7a41ab12b 100644
--- a/client/src/app/[site]/user/[userId]/page.tsx
+++ b/client/src/app/[site]/user/[userId]/page.tsx
@@ -23,10 +23,10 @@ import { Button } from "../../../../components/ui/button";
import { useSetPageTitle } from "../../../../hooks/useSetPageTitle";
import { useGetRegionName } from "../../../../lib/geo";
import {
- formatDuration,
getCountryName,
getLanguageName,
} from "../../../../lib/utils";
+import { formatDuration } from "../../../../lib/dateTimeUtils";
import { Browser } from "../../components/shared/icons/Browser";
import { CountryFlag } from "../../components/shared/icons/CountryFlag";
import { OperatingSystem } from "../../components/shared/icons/OperatingSystem";
diff --git a/client/src/components/Sessions/SessionCard.tsx b/client/src/components/Sessions/SessionCard.tsx
index fa478251b..d9daac9a7 100644
--- a/client/src/components/Sessions/SessionCard.tsx
+++ b/client/src/components/Sessions/SessionCard.tsx
@@ -13,7 +13,7 @@ import {
MousePointerClick,
Smartphone
} from "lucide-react";
-import { DateTime } from "luxon";
+import { DateTime, Duration } from "luxon";
import { memo, useState } from "react";
import { GetSessionsResponse } from "../../api/analytics/userSessions";
import { Browser } from "../../app/[site]/components/shared/icons/Browser";
@@ -57,8 +57,8 @@ export function SessionCard({ session, onClick, userId }: SessionCardProps) {
// Calculate session duration in minutes
const start = DateTime.fromSQL(session.session_start);
const end = DateTime.fromSQL(session.session_end);
- const duration = end.diff(start, ["minutes", "seconds"]);
- const durationFormatted = formatDuration(duration.minutes, duration.seconds);
+ const totalSeconds = Math.floor(end.diff(start).milliseconds / 1000);
+ const duration = formatDuration(totalSeconds);
// Truncate user ID to first 8 characters
const truncatedUserId = session.user_id.substring(0, 8);
@@ -197,7 +197,7 @@ export function SessionCard({ session, onClick, userId }: SessionCardProps) {
.toFormat(hour12 ? "MMM d, h:mm a" : "dd MMM, HH:mm")
}
</span>
- <span className="hidden md:block">{durationFormatted}</span>
+ <span className="hidden md:block">{duration}</span>
</div>
{/* Expand/Collapse icon */}
diff --git a/client/src/components/Sessions/SessionDetails.tsx b/client/src/components/Sessions/SessionDetails.tsx
index 26bc76f70..c68c359f9 100644
--- a/client/src/components/Sessions/SessionDetails.tsx
+++ b/client/src/components/Sessions/SessionDetails.tsx
@@ -51,13 +51,8 @@ function PageviewItem({
let duration = null;
if (!isEvent && nextTimestamp) {
const nextTime = DateTime.fromSQL(nextTimestamp, { zone: "utc" }).toLocal();
- const diff = nextTime.diff(timestamp, ["minutes", "seconds"]);
- const minutes = Math.floor(diff.minutes);
- const seconds = Math.floor(diff.seconds);
-
- if (minutes > 0 || seconds > 0) {
- duration = formatDuration(minutes, seconds);
- }
+ const totalSeconds = Math.floor(nextTime.diff(timestamp).milliseconds / 1000);
+ duration = formatDuration(totalSeconds);
}
return (
diff --git a/client/src/lib/dateTimeUtils.ts b/client/src/lib/dateTimeUtils.ts
index 825679484..7347083ff 100644
--- a/client/src/lib/dateTimeUtils.ts
+++ b/client/src/lib/dateTimeUtils.ts
@@ -70,22 +70,30 @@ export function getLocalizedTimeLabels(
}
/**
- * Formats a duration from minutes and seconds into a human-readable string.
+ * Formats a duration in seconds to a human-readable string.
*
- * @param minutes - Number of minutes.
- * @param seconds - Number of seconds.
- * @returns A compact, readable string like "1 min 30 sec".
+ * @param duration - Duration in seconds.
+ * @returns Human-readable duration string (e.g., "1 min, 30 sec").
*/
-export function formatDuration(minutes: number, seconds: number): string {
- const units: Record<string, number> = {};
- if (minutes > 0) units.minutes = minutes;
- if (seconds > 0 || minutes === 0) units.seconds = seconds;
+export function formatDuration(duration: number): string {
+ // Prepare duration parts in seconds and optionally minutes.
+ const units: Record<string, number> = {
+ seconds: duration % 60,
+ };
+
+ if (duration > 59) {
+ units.minutes = Math.floor(duration / 60);
+ }
- const keys: (keyof DurationLikeObject)[] = minutes > 0 ? ["minutes", "seconds"] : ["seconds"];
+ // Choose which units to show: only "seconds" or both "minutes" and "seconds".
+ const keys: (keyof DurationLikeObject)[] = duration > 59 ? ["minutes", "seconds"] : ["seconds"];
- const duration = Duration.fromObject(units).shiftTo(...keys).normalize();
+ // Convert to Luxon duration object and format to human-readable string.
+ const luxonDuration = Duration.fromObject(units)
+ .shiftTo(...keys)
+ .normalize();
- return duration.toHuman({
+ return luxonDuration.toHuman({
listStyle: "narrow",
unitDisplay: "short",
});
diff --git a/client/src/lib/utils.ts b/client/src/lib/utils.ts
index a00dea859..7a9f26da7 100644
--- a/client/src/lib/utils.ts
+++ b/client/src/lib/utils.ts
@@ -33,20 +33,6 @@ export const getCountryName = (countryCode: string) => {
return countries[countryCode as keyof typeof countries]?.name;
};
-export function formatDuration(seconds: number): string {
- if (!seconds) return "0s";
-
- const duration = Duration.fromObject({ seconds });
-
- if (seconds < 60) {
- return duration.toFormat("s'S'");
- } else if (seconds < 3600) {
- return duration.toFormat("m'm' s's'");
- } else {
- return duration.toFormat("h'h' m'm'");
- }
-}
-
export function truncateString(str: string, n = 50) {
return str.length > n ? str.substring(0, n) + "..." : str;
}