Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(studio): remember project home chart interval per project (#51833)

Drew McLellanf5288eb1e276
diff --git a/apps/studio/components/interfaces/ProjectHome/ChartInterval.utils.test.ts b/apps/studio/components/interfaces/ProjectHome/ChartInterval.utils.test.ts
new file mode 100644
index 00000000000..626774bd3d9
--- /dev/null
+++ b/apps/studio/components/interfaces/ProjectHome/ChartInterval.utils.test.ts
@@ -0,0 +1,55 @@
+import { describe, expect, it } from 'vitest'
+
+import {
+ getDefaultChartInterval,
+ isChartIntervalKey,
+ resolveChartInterval,
+} from './ChartInterval.utils'
+
+describe('isChartIntervalKey', () => {
+ it('accepts known keys only', () => {
+ expect(isChartIntervalKey('1hr')).toBe(true)
+ expect(isChartIntervalKey('1day')).toBe(true)
+ expect(isChartIntervalKey('7day')).toBe(true)
+ expect(isChartIntervalKey('30day')).toBe(false)
+ expect(isChartIntervalKey(undefined)).toBe(false)
+ expect(isChartIntervalKey(1)).toBe(false)
+ })
+})
+
+describe('getDefaultChartInterval', () => {
+ it('uses 1hr when retention is under 7 days', () => {
+ expect(getDefaultChartInterval(1)).toBe('1hr')
+ })
+
+ it('uses 1day when retention is 7+ days or unknown', () => {
+ expect(getDefaultChartInterval(7)).toBe('1day')
+ expect(getDefaultChartInterval(undefined)).toBe('1day')
+ })
+})
+
+describe('resolveChartInterval', () => {
+ it('returns the stored interval when allowed by retention', () => {
+ expect(resolveChartInterval('7day', 7)).toBe('7day')
+ expect(resolveChartInterval('1hr', 7)).toBe('1hr')
+ })
+
+ it('returns the stored interval while retention is still loading', () => {
+ expect(resolveChartInterval('7day', undefined)).toBe('7day')
+ })
+
+ it('falls back to the default when nothing is stored', () => {
+ expect(resolveChartInterval(undefined, 7)).toBe('1day')
+ expect(resolveChartInterval(undefined, 1)).toBe('1hr')
+ })
+
+ it('falls back to the default when the stored value is invalid', () => {
+ expect(resolveChartInterval('bogus', 7)).toBe('1day')
+ expect(resolveChartInterval(42, 1)).toBe('1hr')
+ })
+
+ it('falls back to the default when the plan no longer allows the stored interval', () => {
+ expect(resolveChartInterval('7day', 1)).toBe('1hr')
+ expect(resolveChartInterval('1day', 0.5)).toBe('1hr')
+ })
+})
diff --git a/apps/studio/components/interfaces/ProjectHome/ChartInterval.utils.ts b/apps/studio/components/interfaces/ProjectHome/ChartInterval.utils.ts
new file mode 100644
index 00000000000..417895a5436
--- /dev/null
+++ b/apps/studio/components/interfaces/ProjectHome/ChartInterval.utils.ts
@@ -0,0 +1,27 @@
+import type { ChartIntervalKey } from './ProjectUsage.metrics'
+import { CHART_INTERVALS, getDaysRequired } from '@/components/ui/Logs/logs.utils'
+
+export const isChartIntervalKey = (value: unknown): value is ChartIntervalKey =>
+ value === '1hr' || value === '1day' || value === '7day'
+
+export const getDefaultChartInterval = (retentionDays: number | undefined): ChartIntervalKey =>
+ retentionDays !== undefined && retentionDays < 7 ? '1hr' : '1day'
+
+/**
+ * Returns the stored interval if it's valid and allowed by the plan's log retention,
+ * otherwise the plan default.
+ */
+export const resolveChartInterval = (
+ storedInterval: unknown,
+ retentionDays: number | undefined
+): ChartIntervalKey => {
+ if (isChartIntervalKey(storedInterval)) {
+ const interval = CHART_INTERVALS.find((i) => i.key === storedInterval)
+ const isAllowed =
+ interval !== undefined &&
+ (retentionDays === undefined ||
+ getDaysRequired(interval.startValue, interval.startUnit) <= retentionDays)
+ if (isAllowed) return storedInterval
+ }
+ return getDefaultChartInterval(retentionDays)
+}
diff --git a/apps/studio/components/interfaces/ProjectHome/ProjectUsageSection.tsx b/apps/studio/components/interfaces/ProjectHome/ProjectUsageSection.tsx
index 78106a90b9c..fb08fc43c23 100644
--- a/apps/studio/components/interfaces/ProjectHome/ProjectUsageSection.tsx
+++ b/apps/studio/components/interfaces/ProjectHome/ProjectUsageSection.tsx
@@ -1,13 +1,14 @@
-import { useParams } from 'common'
+import { LOCAL_STORAGE_KEYS, useParams } from 'common'
import dayjs from 'dayjs'
import Link from 'next/link'
import { useRouter } from 'next/router'
-import { useEffect, useMemo, useState } from 'react'
+import { useMemo } from 'react'
import { Card, CardContent, CardHeader, CardTitle, Loading, WarningIcon } from 'ui'
import { ChartEmptyState } from 'ui-patterns/Chart'
import { LogsBarChart } from 'ui-patterns/LogsBarChart'
import { Row } from 'ui-patterns/Row'
+import { isChartIntervalKey, resolveChartInterval } from './ChartInterval.utils'
import { useUnifiedLogsPreview } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext'
import {
buildUnifiedLogsUrl,
@@ -20,6 +21,7 @@ import { UsageApiCounts, useProjectLogStatsQuery } from '@/data/analytics/projec
import { fillTimeseriesSorted } from '@/hooks/analytics/useFillTimeseriesSorted'
import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
+import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
import { useTrack } from '@/lib/telemetry/track'
@@ -30,8 +32,6 @@ type LogsBarChartDatum = {
warning_count: number
}
-type ChartIntervalKey = '1hr' | '1day' | '7day'
-
type ServiceKey = 'db' | 'auth' | 'storage' | 'realtime'
type ServiceEntry = {
@@ -63,13 +63,11 @@ export const ProjectUsageSection = () => {
const retentionDays = getEntitlementMax()
const { isEnabled: isUnifiedLogsEnabled } = useUnifiedLogsPreview()
- const DEFAULT_INTERVAL: ChartIntervalKey =
- retentionDays !== undefined && retentionDays < 7 ? '1hr' : '1day'
- const [interval, setInterval] = useState<ChartIntervalKey>(DEFAULT_INTERVAL)
-
- useEffect(() => {
- setInterval(retentionDays !== undefined && retentionDays < 7 ? '1hr' : '1day')
- }, [retentionDays])
+ const [storedInterval, setStoredInterval] = useLocalStorageQuery<string | undefined>(
+ LOCAL_STORAGE_KEYS.HOME_CHART_INTERVAL(projectRef ?? ''),
+ undefined
+ )
+ const interval = resolveChartInterval(storedInterval, retentionDays)
const selectedInterval = CHART_INTERVALS.find((i) => i.key === interval) || CHART_INTERVALS[1]
@@ -227,7 +225,9 @@ export const ProjectUsageSection = () => {
</div>
<ChartIntervalDropdown
value={interval}
- onChange={(interval) => setInterval(interval as ChartIntervalKey)}
+ onChange={(next) => {
+ if (isChartIntervalKey(next)) setStoredInterval(next)
+ }}
organizationSlug={organization?.slug}
dropdownAlign="end"
tooltipSide="left"
diff --git a/apps/studio/components/interfaces/ProjectHome/ProjectUsageSectionDeltas.tsx b/apps/studio/components/interfaces/ProjectHome/ProjectUsageSectionDeltas.tsx
index 7f3385e803b..70b68b3621c 100644
--- a/apps/studio/components/interfaces/ProjectHome/ProjectUsageSectionDeltas.tsx
+++ b/apps/studio/components/interfaces/ProjectHome/ProjectUsageSectionDeltas.tsx
@@ -1,17 +1,17 @@
-import { useParams } from 'common'
+import { LOCAL_STORAGE_KEYS, useParams } from 'common'
import Link from 'next/link'
import { useRouter } from 'next/router'
-import { useMemo, useState } from 'react'
+import { useMemo } from 'react'
import { Card, CardContent, CardHeader, CardTitle, cn, Loading } from 'ui'
import { LogsBarChart } from 'ui-patterns/LogsBarChart'
import { Row } from 'ui-patterns/Row'
+import { isChartIntervalKey, resolveChartInterval } from './ChartInterval.utils'
import {
buildSortedServiceCards,
computeSuccessAndNonSuccessRates,
getBucketLogRange,
isServiceDisabled,
- type ChartIntervalKey,
type LogsBarChartDatum,
} from './ProjectUsage.metrics'
import { useUnifiedLogsPreview } from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext'
@@ -26,15 +26,13 @@ import { CHART_INTERVALS } from '@/components/ui/Logs/logs.utils'
import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
import { useIsDataApiEnabled } from '@/hooks/misc/useIsDataApiEnabled'
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
+import { useLocalStorageQuery } from '@/hooks/misc/useLocalStorage'
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
import { useTrack } from '@/lib/telemetry/track'
// Services the homepage shows; matches the telemetry event types.
type HomeServiceKey = 'db' | 'functions' | 'auth' | 'storage' | 'realtime' | 'data_api'
-const isChartIntervalKey = (value: string): value is ChartIntervalKey =>
- value === '1hr' || value === '1day' || value === '7day'
-
type ServiceEntry = {
key: HomeServiceKey
title: string
@@ -58,9 +56,11 @@ export const ProjectUsageSectionDeltas = () => {
const { getEntitlementMax } = useCheckEntitlements('log.retention_days')
const retentionDays = getEntitlementMax()
- const [userInterval, setUserInterval] = useState<ChartIntervalKey | undefined>(undefined)
- const interval: ChartIntervalKey =
- userInterval ?? (retentionDays !== undefined && retentionDays < 7 ? '1hr' : '1day')
+ const [storedInterval, setStoredInterval] = useLocalStorageQuery<string | undefined>(
+ LOCAL_STORAGE_KEYS.HOME_CHART_INTERVAL(projectRef ?? ''),
+ undefined
+ )
+ const interval = resolveChartInterval(storedInterval, retentionDays)
const selectedInterval = CHART_INTERVALS.find((i) => i.key === interval) || CHART_INTERVALS[1]
const datetimeFormat = selectedInterval.format || 'MMM D, ha'
@@ -178,7 +178,7 @@ export const ProjectUsageSectionDeltas = () => {
<ChartIntervalDropdown
value={interval}
onChange={(next) => {
- if (isChartIntervalKey(next)) setUserInterval(next)
+ if (isChartIntervalKey(next)) setStoredInterval(next)
}}
organizationSlug={organization?.slug}
dropdownAlign="end"
diff --git a/apps/studio/components/ui/Logs/ChartIntervalDropdown.tsx b/apps/studio/components/ui/Logs/ChartIntervalDropdown.tsx
index 0eabd8741ab..bc6ad74b6bb 100644
--- a/apps/studio/components/ui/Logs/ChartIntervalDropdown.tsx
+++ b/apps/studio/components/ui/Logs/ChartIntervalDropdown.tsx
@@ -11,16 +11,10 @@ import {
TooltipTrigger,
} from 'ui'
-import { CHART_INTERVALS } from './logs.utils'
+import { CHART_INTERVALS, getDaysRequired } from './logs.utils'
import { InlineLink } from '@/components/ui/InlineLink'
import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
-function getDaysRequired(startValue: number, startUnit: string): number {
- if (startUnit === 'day') return startValue
- if (startUnit === 'hour') return startValue / 24
- return 0
-}
-
interface ChartIntervalDropdownProps {
value: string
onChange: (value: string) => void
diff --git a/apps/studio/components/ui/Logs/logs.utils.ts b/apps/studio/components/ui/Logs/logs.utils.ts
index 69c56804956..5584e34b5ce 100644
--- a/apps/studio/components/ui/Logs/logs.utils.ts
+++ b/apps/studio/components/ui/Logs/logs.utils.ts
@@ -23,3 +23,9 @@ export const CHART_INTERVALS: ChartIntervals[] = [
format: 'MMM D',
},
]
+
+export function getDaysRequired(startValue: number, startUnit: string): number {
+ if (startUnit === 'day') return startValue
+ if (startUnit === 'hour') return startValue / 24
+ return 0
+}
diff --git a/packages/common/constants/local-storage.ts b/packages/common/constants/local-storage.ts
index 18f1e93c51b..e9204fee10e 100644
--- a/packages/common/constants/local-storage.ts
+++ b/packages/common/constants/local-storage.ts
@@ -17,6 +17,7 @@ export const LOCAL_STORAGE_KEYS = {
INCIDENT_BANNER_DISMISSED_IDS: 'incident-banner-dismissed-ids',
STATUS_BANNER_DISMISSED_KEYS: 'status-banner-dismissed-keys',
DASHBOARD_PREFERENCES: (ref: string) => `dashboard-preferences-${ref}`,
+ HOME_CHART_INTERVAL: (ref: string) => `home-chart-interval-${ref}`,
UNIFIED_LOGS_DOCK: 'unified-logs-dock',
UI_TIMEZONE: 'supabase-ui-timezone',