11import { useAtomValue } from "@effect/atom-react" ;
2- import * as Schema from "effect/Schema" ;
32import {
43 USAGE_CONTRACT_VERSION ,
54 type EnvironmentId ,
@@ -21,7 +20,6 @@ import {
2120} from "@t3tools/shared/usageMerge" ;
2221
2322import { isElectron } from "../../env" ;
24- import { useLocalStorage } from "../../hooks/useLocalStorage" ;
2523import { cn } from "../../lib/utils" ;
2624import { environmentPresentations } from "../../state/presentation" ;
2725import { serverEnvironment } from "../../state/server" ;
@@ -64,6 +62,11 @@ import { UsageLimitsSection } from "./UsageLimits";
6462import { UsagePriceOverrides } from "./UsagePriceOverrides" ;
6563import { UsageProviderChart , type UsageChartMetric } from "./UsageProviderChart" ;
6664import { PROVIDER_ORDER , PROVIDER_PRESENTATION , providersWithUsage } from "./usageProviders" ;
65+ import {
66+ readUsagePagePreferences ,
67+ saveUsagePagePreferences ,
68+ type UsagePagePreferences ,
69+ } from "./usagePagePreferences" ;
6770
6871type UsageMetric = UsageChartMetric | "limits" ;
6972const METRIC_OPTIONS = [
@@ -72,20 +75,6 @@ const METRIC_OPTIONS = [
7275 { value : "limits" , label : "Limits" } ,
7376] as const satisfies readonly { value : UsageMetric ; label : string } [ ] ;
7477
75- const USAGE_PAGE_PREFERENCES_STORAGE_KEY = "t3code:usage-page-preferences:v1" ;
76- const UsageMetricSchema = Schema . Literals ( [ "cost" , "tokens" , "limits" ] ) ;
77- const UsageWindowDaysSchema = Schema . Literals ( [ 1 , 7 , 30 , 90 ] ) ;
78- type UsageWindowDays = typeof UsageWindowDaysSchema . Type ;
79- const UsagePagePreferencesSchema = Schema . Struct ( {
80- metric : UsageMetricSchema ,
81- windowDays : UsageWindowDaysSchema ,
82- } ) ;
83- type UsagePagePreferences = typeof UsagePagePreferencesSchema . Type ;
84- const DEFAULT_USAGE_PAGE_PREFERENCES : UsagePagePreferences = {
85- metric : "cost" ,
86- windowDays : 30 ,
87- } ;
88-
8978function isUsageMetric ( value : string | null | undefined ) : value is UsageMetric {
9079 return METRIC_OPTIONS . some ( ( option ) => option . value === value ) ;
9180}
@@ -97,16 +86,12 @@ const WINDOW_OPTIONS = [
9786 { days : 90 , label : "90 days" } ,
9887] as const ;
9988
100- function isUsageWindowDays ( value : number ) : value is UsageWindowDays {
89+ function isUsageWindowDays ( value : number ) : value is UsagePagePreferences [ "windowDays" ] {
10190 return WINDOW_OPTIONS . some ( ( option ) => option . days === value ) ;
10291}
10392
10493export function UsagePage ( ) {
105- const [ preferences , setPreferences ] = useLocalStorage (
106- USAGE_PAGE_PREFERENCES_STORAGE_KEY ,
107- DEFAULT_USAGE_PAGE_PREFERENCES ,
108- UsagePagePreferencesSchema ,
109- ) ;
94+ const [ preferences , setPreferences ] = useState ( readUsagePagePreferences ) ;
11095 const [ windowSelection , setWindowSelection ] = useState ( ( ) => ( {
11196 days : preferences . windowDays ,
11297 window : makeWindow (
@@ -162,14 +147,18 @@ export function UsagePage() {
162147
163148 const selectWindow = ( days : number ) => {
164149 if ( ! isUsageWindowDays ( days ) ) return ;
165- setPreferences ( ( current ) => ( { ...current , windowDays : days } ) ) ;
150+ const nextPreferences = { metric, windowDays : days } ;
151+ setPreferences ( nextPreferences ) ;
152+ saveUsagePagePreferences ( nextPreferences ) ;
166153 setWindowSelection ( {
167154 days,
168155 window : makeWindow ( days , undefined , days === 1 ? "hour" : "day" ) ,
169156 } ) ;
170157 } ;
171158 const selectMetric = ( nextMetric : UsageMetric ) => {
172- setPreferences ( ( current ) => ( { ...current , metric : nextMetric } ) ) ;
159+ const nextPreferences = { metric : nextMetric , windowDays } ;
160+ setPreferences ( nextPreferences ) ;
161+ saveUsagePagePreferences ( nextPreferences ) ;
173162 } ;
174163 const refreshWindow = ( ) => {
175164 if ( showingLimits ) {
0 commit comments