Skip to content

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

Fix period chart axes ending a bucket early, add past-minutes time navigation (#1130)

Goldflag64f8c4fb7f39
diff --git a/client/src/app/(home)/page.tsx b/client/src/app/(home)/page.tsx
index dc6233f15..bc45089cb 100644
--- a/client/src/app/(home)/page.tsx
+++ b/client/src/app/(home)/page.tsx
@@ -22,7 +22,7 @@ import { MultiSelect } from "../../components/ui/multi-select";
import { Pagination } from "../../components/pagination";
import { useSetPageTitle } from "../../hooks/useSetPageTitle";
import { authClient } from "../../lib/auth";
-import { canGoForward, goBack, goForward, useStore } from "../../lib/store";
+import { canGoBack, canGoForward, goBack, goForward, useStore } from "../../lib/store";
import { AddSite } from "../components/AddSite";
import { SiteCard } from "./SiteCard";
@@ -143,7 +143,7 @@ export default function Home() {
variant="secondary"
size="icon"
onClick={goBack}
- disabled={time.mode === "past-minutes"}
+ disabled={!canGoBack(time)}
className="rounded-r-none h-8 w-8"
>
<ChevronLeft />
diff --git a/client/src/app/[site]/components/SubHeader/SubHeader.tsx b/client/src/app/[site]/components/SubHeader/SubHeader.tsx
index 0ace67cf4..98fd4097e 100644
--- a/client/src/app/[site]/components/SubHeader/SubHeader.tsx
+++ b/client/src/app/[site]/components/SubHeader/SubHeader.tsx
@@ -1,6 +1,6 @@
"use client";
import { Button } from "@/components/ui/button";
-import { canGoForward, goBack, goForward, useStore } from "@/lib/store";
+import { canGoBack, canGoForward, goBack, goForward, useStore } from "@/lib/store";
import { FilterParameter } from "@rybbit/shared";
import { ChevronLeft, ChevronRight } from "lucide-react";
import { Filters } from "./Filters/Filters";
@@ -32,7 +32,7 @@ export function SubHeader({ availableFilters }: { availableFilters?: FilterParam
variant="secondary"
size="icon"
onClick={goBack}
- disabled={time.mode === "past-minutes"}
+ disabled={!canGoBack(time)}
className="rounded-r-none h-8 w-8"
>
<ChevronLeft />
diff --git a/client/src/app/[site]/dashboards/[dashboardId]/page.tsx b/client/src/app/[site]/dashboards/[dashboardId]/page.tsx
index e11080141..a88aa582b 100644
--- a/client/src/app/[site]/dashboards/[dashboardId]/page.tsx
+++ b/client/src/app/[site]/dashboards/[dashboardId]/page.tsx
@@ -26,7 +26,7 @@ import { Skeleton } from "../../../../components/ui/skeleton";
import { toast } from "../../../../components/ui/sonner";
import { cn } from "../../../../lib/utils";
import { useSetPageTitle } from "../../../../hooks/useSetPageTitle";
-import { canGoForward, goBack, goForward, useStore } from "../../../../lib/store";
+import { canGoBack, canGoForward, goBack, goForward, useStore } from "../../../../lib/store";
import { DashboardCardEditor } from "../components/DashboardCardEditor";
import { DashboardCardView } from "../components/DashboardCardView";
import { NewCardDialog } from "../components/NewCardDialog";
@@ -346,7 +346,7 @@ export default function DashboardDetailPage() {
variant="secondary"
size="icon"
onClick={goBack}
- disabled={time.mode === "past-minutes"}
+ disabled={!canGoBack(time)}
className="h-8 w-8 rounded-r-none"
aria-label="Previous date range"
>
diff --git a/client/src/app/[site]/user/[userId]/page.tsx b/client/src/app/[site]/user/[userId]/page.tsx
index 16b243f0a..b59e52092 100644
--- a/client/src/app/[site]/user/[userId]/page.tsx
+++ b/client/src/app/[site]/user/[userId]/page.tsx
@@ -10,7 +10,7 @@ import { useUserInfo } from "../../../../api/analytics/hooks/userGetInfo";
import { useGetSessions, useGetUserSessionCount } from "../../../../api/analytics/hooks/useGetUserSessions";
import { DateSelector } from "../../../../components/DateSelector/DateSelector";
import { Button } from "../../../../components/ui/button";
-import { canGoForward, goBack, goForward, useStore } from "../../../../lib/store";
+import { canGoBack, canGoForward, goBack, goForward, useStore } from "../../../../lib/store";
import { USER_DETAIL_PAGE_FILTERS } from "../../../../lib/filterGroups";
import { Filters } from "../../components/SubHeader/Filters/Filters";
import { NewFilterButton } from "../../components/SubHeader/Filters/NewFilterButton";
@@ -118,7 +118,7 @@ export default function UserPage() {
variant="secondary"
size="icon"
onClick={goBack}
- disabled={time.mode === "past-minutes"}
+ disabled={!canGoBack(time)}
className="rounded-r-none h-8 w-8"
>
<ChevronLeft />
diff --git a/client/src/app/rollup/components/RollupTopBar.tsx b/client/src/app/rollup/components/RollupTopBar.tsx
index 467442773..5c1f72864 100644
--- a/client/src/app/rollup/components/RollupTopBar.tsx
+++ b/client/src/app/rollup/components/RollupTopBar.tsx
@@ -4,7 +4,7 @@ import { Team } from "@/api/admin/endpoints/teams";
import { DateSelector } from "@/components/DateSelector/DateSelector";
import { TeamSelector } from "@/components/TeamSelector";
import { Button } from "@/components/ui/button";
-import { canGoForward, goBack, goForward, useStore } from "@/lib/store";
+import { canGoBack, canGoForward, goBack, goForward, useStore } from "@/lib/store";
export function RollupTopBar({
teams,
@@ -32,7 +32,7 @@ export function RollupTopBar({
variant="secondary"
size="icon"
onClick={goBack}
- disabled={time.mode === "past-minutes"}
+ disabled={!canGoBack(time)}
className="rounded-r-none h-8 w-8"
>
<ChevronLeft />
diff --git a/client/src/components/DateSelector/DateSelector.tsx b/client/src/components/DateSelector/DateSelector.tsx
index d202866aa..26a3abb58 100644
--- a/client/src/components/DateSelector/DateSelector.tsx
+++ b/client/src/components/DateSelector/DateSelector.tsx
@@ -137,6 +137,18 @@ export function DateSelector({
}
if (time.mode === "past-minutes") {
+ // A window stepped back no longer ends at now, so name the stretch it
+ // covers — "Last 30 minutes" would read as live when it isn't.
+ if (time.pastMinutesEnd > 0) {
+ const start = now.minus({ minutes: time.pastMinutesStart });
+ const end = now.minus({ minutes: time.pastMinutesEnd });
+ const clock = hour12 ? "h:mm a" : "HH:mm";
+ const withDate = `MMM d, ${clock}`;
+ const startFormatted = start.hasSame(now, "day") ? start.toFormat(clock) : start.toFormat(withDate);
+ const endFormatted = end.hasSame(start, "day") ? end.toFormat(clock) : end.toFormat(withDate);
+ return `${startFormatted} - ${endFormatted}`;
+ }
+
if (time.pastMinutesStart >= 60) {
const hours = Math.floor(time.pastMinutesStart / 60);
return t("Last {hours} hours", { hours: String(hours) });
diff --git a/client/src/lib/store.ts b/client/src/lib/store.ts
index 79dbab6e7..5f4db2ab6 100644
--- a/client/src/lib/store.ts
+++ b/client/src/lib/store.ts
@@ -208,3 +208,5 @@ export const getFilteredFilters = (parameters: FilterParameter[]) => {
};
export const canGoForward = (time: Time) => canGoForwardFrom(time, getTimezone());
+
+export const canGoBack = (time: Time) => shiftTimeBackward(time, getTimezone()) !== null;
diff --git a/client/src/lib/time.test.ts b/client/src/lib/time.test.ts
index 8714fe476..03b0b3163 100644
--- a/client/src/lib/time.test.ts
+++ b/client/src/lib/time.test.ts
@@ -134,13 +134,21 @@ describe("shiftTimeBackward / shiftTimeForward", () => {
endDate: "2024-03-08",
});
expect(
- shiftTimeForward({ mode: "range", startDate: "2024-03-01", endDate: "2024-03-07" }, ZONE, dt("2024-03-20T00:00:00"))
+ shiftTimeForward(
+ { mode: "range", startDate: "2024-03-01", endDate: "2024-03-07" },
+ ZONE,
+ dt("2024-03-20T00:00:00")
+ )
).toEqual({ mode: "range", startDate: "2024-03-07", endDate: "2024-03-13" });
});
it("range: forward is blocked when the whole range would be in the future", () => {
expect(
- shiftTimeForward({ mode: "range", startDate: "2024-03-10", endDate: "2024-03-16" }, ZONE, dt("2024-03-14T00:00:00"))
+ shiftTimeForward(
+ { mode: "range", startDate: "2024-03-10", endDate: "2024-03-16" },
+ ZONE,
+ dt("2024-03-14T00:00:00")
+ )
).toBeNull();
});
@@ -192,10 +200,34 @@ describe("shiftTimeBackward / shiftTimeForward", () => {
).toBeNull();
});
- it("all-time and past-minutes do not navigate", () => {
+ it("all-time does not navigate", () => {
expect(shiftTimeBackward({ mode: "all-time" }, ZONE)).toBeNull();
expect(shiftTimeForward({ mode: "all-time" }, ZONE)).toBeNull();
- expect(shiftTimeBackward({ mode: "past-minutes", pastMinutesStart: 30, pastMinutesEnd: 0 }, ZONE)).toBeNull();
+ });
+
+ it("past-minutes: steps by the window length, staying relative to now", () => {
+ const live: Time = { mode: "past-minutes", pastMinutesStart: 30, pastMinutesEnd: 0 };
+ const oneBack = shiftTimeBackward(live, ZONE);
+ expect(oneBack).toEqual({ mode: "past-minutes", pastMinutesStart: 60, pastMinutesEnd: 30 });
+ expect(shiftTimeBackward(oneBack!, ZONE)).toEqual({
+ mode: "past-minutes",
+ pastMinutesStart: 90,
+ pastMinutesEnd: 60,
+ });
+ expect(shiftTimeForward(oneBack!, ZONE)).toEqual(live);
+ });
+
+ it("past-minutes: forward stops at the live window instead of the future", () => {
+ expect(shiftTimeForward({ mode: "past-minutes", pastMinutesStart: 30, pastMinutesEnd: 0 }, ZONE)).toBeNull();
+ });
+
+ it("past-minutes: forward clamps a partial step onto the live window", () => {
+ // A 6h window whose newer edge sits 2h out: the step lands on 0, not -4h.
+ expect(shiftTimeForward({ mode: "past-minutes", pastMinutesStart: 480, pastMinutesEnd: 120 }, ZONE)).toEqual({
+ mode: "past-minutes",
+ pastMinutesStart: 360,
+ pastMinutesEnd: 0,
+ });
});
});
@@ -233,9 +265,13 @@ describe("canGoForward", () => {
expect(canGoForward({ mode: "year", year: "2025-01-01" }, ZONE, now)).toBe(false);
});
- it("all-time and past-minutes: never", () => {
+ it("all-time: never", () => {
expect(canGoForward({ mode: "all-time" }, ZONE, now)).toBe(false);
+ });
+
+ it("past-minutes: gated by the newer edge of the window", () => {
expect(canGoForward({ mode: "past-minutes", pastMinutesStart: 30, pastMinutesEnd: 0 }, ZONE, now)).toBe(false);
+ expect(canGoForward({ mode: "past-minutes", pastMinutesStart: 60, pastMinutesEnd: 30 }, ZONE, now)).toBe(true);
});
});
diff --git a/client/src/lib/time.ts b/client/src/lib/time.ts
index 00b53b663..3d88fd0d3 100644
--- a/client/src/lib/time.ts
+++ b/client/src/lib/time.ts
@@ -116,8 +116,33 @@ export const deriveTimeState = (time: Time, zone: string): { previousTime: Time;
return { previousTime, bucket };
};
-/** One step back in time. Null when the mode doesn't navigate (all-time, past-minutes). */
+/**
+ * A past-minutes window steps by its own length and stays relative to now, so
+ * the presets keep auto-refreshing: {30, 0} back is {60, 30}, and forward from
+ * there clamps the newer edge at 0 to land back on the live window.
+ */
+const shiftPastMinutes = (time: Extract<Time, { mode: "past-minutes" }>, direction: 1 | -1): Time | null => {
+ const windowLength = time.pastMinutesStart - time.pastMinutesEnd;
+
+ if (direction === -1) {
+ return {
+ mode: "past-minutes",
+ pastMinutesStart: time.pastMinutesStart + windowLength,
+ pastMinutesEnd: time.pastMinutesEnd + windowLength,
+ };
+ }
+
+ if (time.pastMinutesEnd === 0) return null;
+ const pastMinutesEnd = Math.max(0, time.pastMinutesEnd - windowLength);
+ return { mode: "past-minutes", pastMinutesStart: pastMinutesEnd + windowLength, pastMinutesEnd };
+};
+
+/** One step back in time. Null when the mode doesn't navigate (all-time). */
export const shiftTimeBackward = (time: Time, zone: string): Time | null => {
+ if (time.mode === "past-minutes") {
+ return shiftPastMinutes(time, -1);
+ }
+
if (time.mode === "day") {
return {
mode: "day",
@@ -177,6 +202,10 @@ export const shiftTimeBackward = (time: Time, zone: string): Time | null => {
* would land entirely in the future (ranges clamp against `now`).
*/
export const shiftTimeForward = (time: Time, zone: string, now: DateTime = DateTime.now()): Time | null => {
+ if (time.mode === "past-minutes") {
+ return shiftPastMinutes(time, 1);
+ }
+
if (time.mode === "day") {
return {
mode: "day",
@@ -282,6 +311,11 @@ export const canGoForward = (time: Time, zone: string, now: DateTime = DateTime.
return !(DateTime.fromISO(time.year, { zone }).startOf("year") >= currentDay);
}
+ // A past-minutes window that already ends at now has nowhere newer to go.
+ if (time.mode === "past-minutes") {
+ return time.pastMinutesEnd > 0;
+ }
+
return false;
};