Files
Drew McLellan f5288eb1e2 feat(studio): remember project home chart interval per project (#51833)
## Problem

The time-range dropdown above the request charts on the project home
page resets on every visit, so users who prefer a different range have
to re-select it each time.

Resolves FE-2828.

<img width="819" height="353" alt="Screenshot 2026-10-16 at 10 42 05"
src="https://github.com/user-attachments/assets/cb8d467b-e709-43b0-9ae4-b02ebccf5faf"
/>


## Solution

Persist the selection in localStorage, keyed per project
(`home-chart-interval-${ref}`), in both usage section variants
(`ProjectUsageSection` and `ProjectUsageSectionDeltas`, chosen by the
`newHomepageUsageDeltas` flag).

- New `resolveChartInterval` helper validates the stored value against
the known keys and the plan's log retention, falling back to the plan
default for corrupt values or after a plan downgrade. Unit tested.
- Removed the `useEffect` in `ProjectUsageSection` that reset the
interval on retention change, since it would overwrite the saved value.
- Moved `getDaysRequired` into `logs.utils.ts` so the dropdown and the
helper share the retention check.

Manually verified with the flag on and off: reload keeps the selection,
and each project keeps its own.
2026-10-16 13:52:23 +01:00

28 lines
1.1 KiB
TypeScript

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)
}