Open History to see the last commit
1import * as Schema from "effect/Schema";
2
3import { getLocalStorageItem, setLocalStorageItem } from "../../hooks/useLocalStorage";
4
5const STORAGE_KEY = "t3code:usage-page-preferences:v1";
6const UsagePagePreferencesSchema = Schema.Struct({
7 metric: Schema.Literals(["cost", "tokens", "limits"]),
8 windowDays: Schema.Literals([1, 7, 30, 90]),
9});
10export type UsagePagePreferences = typeof UsagePagePreferencesSchema.Type;
11
12export function readUsagePagePreferences(): UsagePagePreferences {
13 try {
14 return (
15 getLocalStorageItem(STORAGE_KEY, UsagePagePreferencesSchema) ?? {
16 metric: "cost",
17 windowDays: 30,
18 }
19 );
20 } catch (error) {
21 console.error("Could not read Usage page preferences.", error);
22 return { metric: "cost", windowDays: 30 };
23 }
24}
25
26export function saveUsagePagePreferences(preferences: UsagePagePreferences): void {
27 try {
28 setLocalStorageItem(STORAGE_KEY, preferences, UsagePagePreferencesSchema);
29 } catch (error) {
30 console.error("Could not save Usage page preferences.", error);
31 }
32}