Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: broken time / period selector

Blue Mousecdef92f4f13c
diff --git a/web/app/lib/constants/index.ts b/web/app/lib/constants/index.ts
index 8e13e62d5..8eb9333fa 100644
--- a/web/app/lib/constants/index.ts
+++ b/web/app/lib/constants/index.ts
@@ -74,6 +74,36 @@ export type Period = (typeof VALID_PERIODS)[number]
export type TimeBucket = (typeof VALID_TIME_BUCKETS)[number]
+// Maps each period to its valid time buckets (must stay in sync with tbPeriodPairs)
+export const PERIOD_TO_VALID_TIME_BUCKETS: Record<Period, TimeBucket[]> = {
+ '1h': ['minute'],
+ today: ['hour'],
+ yesterday: ['hour'],
+ '1d': ['hour'],
+ '7d': ['hour', 'day'],
+ '4w': ['day'],
+ '3M': ['day', 'month'],
+ '12M': ['day', 'month'],
+ '24M': ['month'],
+ all: ['month', 'year'],
+ custom: ['hour', 'day', 'month'],
+ compare: ['hour', 'day', 'month'],
+}
+
+export const getValidTimeBucket = (
+ period: Period,
+ requestedTimeBucket?: string | null,
+): TimeBucket => {
+ const validBuckets = PERIOD_TO_VALID_TIME_BUCKETS[period] || ['day']
+ if (
+ requestedTimeBucket &&
+ validBuckets.includes(requestedTimeBucket as TimeBucket)
+ ) {
+ return requestedTimeBucket as TimeBucket
+ }
+ return validBuckets[0]
+}
+
export const tbPeriodPairs = (
t: typeof i18nextT,
tbs?: TimeBucket[] | null,
diff --git a/web/app/routes/projects.$id.tsx b/web/app/routes/projects.$id.tsx
index a52c77c3e..8e67b214d 100644
--- a/web/app/routes/projects.$id.tsx
+++ b/web/app/routes/projects.$id.tsx
@@ -47,6 +47,8 @@ import {
MAIN_URL,
DEFAULT_TIMEZONE,
PROJECT_TABS,
+ getValidTimeBucket,
+ type Period,
} from '~/lib/constants'
import { Project } from '~/lib/models/Project'
import ViewProject from '~/pages/Project/View'
@@ -249,13 +251,16 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
const tab =
(url.searchParams.get('tab') as keyof typeof PROJECT_TABS) ||
PROJECT_TABS.traffic
- const period = url.searchParams.get('period') || '7d'
- const timeBucket = url.searchParams.get('timeBucket') || 'day'
+ const period = (url.searchParams.get('period') || '7d') as Period
+ const urlTimeBucket = url.searchParams.get('timeBucket')
const from = url.searchParams.get('from') || ''
const to = url.searchParams.get('to') || ''
const timezone = url.searchParams.get('timezone') || DEFAULT_TIMEZONE
const filters = parseFiltersFromUrl(url.searchParams)
+ // Ensure the time bucket is valid for the selected period
+ const timeBucket = getValidTimeBucket(period, urlTimeBucket)
+
// Performance-specific params
const measure = url.searchParams.get('measure') || 'median'
const perfMetric = url.searchParams.get('perfMetric') || 'timing' // 'timing' or 'quantiles'