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
12// Limits is what most people open the page for (how much subscription quota is
13// left, and when it resets), so it is the first-visit default; the last picked
14// tab sticks after that.
15const DEFAULT_PREFERENCES: UsagePagePreferences = { metric: "limits", windowDays: 30 };
16
17export function readUsagePagePreferences(): UsagePagePreferences {
18 try {
19 return getLocalStorageItem(STORAGE_KEY, UsagePagePreferencesSchema) ?? DEFAULT_PREFERENCES;
20 } catch (error) {
21 console.error("Could not read Usage page preferences.", error);
22 return DEFAULT_PREFERENCES;
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}