Open History to see the last commit
1import { UsageProviderKind } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3
4import { getLocalStorageItem, setLocalStorageItem } from "../../hooks/useLocalStorage";
5
6const STORAGE_KEY = "t3code:usage-page-preferences:v1";
7const UsagePagePreferencesSchema = Schema.Struct({
8 metric: Schema.Literals(["cost", "tokens", "limits"]),
9 windowDays: Schema.Literals([1, 7, 30, 90]),
10 /** Providers filtered out of the page. Stored as hidden so new providers show by default. */
11 hiddenProviders: Schema.optional(Schema.Array(UsageProviderKind)),
12});
13export type UsagePagePreferences = typeof UsagePagePreferencesSchema.Type;
14
15// Limits is what most people open the page for (how much subscription quota is
16// left, and when it resets), so it is the first-visit default; the last picked
17// tab sticks after that.
18const DEFAULT_PREFERENCES: UsagePagePreferences = { metric: "limits", windowDays: 30 };
19
20export function readUsagePagePreferences(): UsagePagePreferences {
21 try {
22 return getLocalStorageItem(STORAGE_KEY, UsagePagePreferencesSchema) ?? DEFAULT_PREFERENCES;
23 } catch (error) {
24 console.error("Could not read Usage page preferences.", error);
25 return DEFAULT_PREFERENCES;
26 }
27}
28
29export function saveUsagePagePreferences(preferences: UsagePagePreferences): void {
30 try {
31 setLocalStorageItem(STORAGE_KEY, preferences, UsagePagePreferencesSchema);
32 } catch (error) {
33 console.error("Could not save Usage page preferences.", error);
34 }
35}