Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usagePagePreferences.ts

31 lines1.2 KB

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 hiddenProviders: Schema.optional(Schema.Array(UsageProviderKind)),
11});
12export type UsagePagePreferences = typeof UsagePagePreferencesSchema.Type;
13​
14const DEFAULT_PREFERENCES: UsagePagePreferences = { metric: "limits", windowDays: 30 };
15​
16export function readUsagePagePreferences(): UsagePagePreferences {
17 try {
18 return getLocalStorageItem(STORAGE_KEY, UsagePagePreferencesSchema) ?? DEFAULT_PREFERENCES;
19 } catch (error) {
20 console.error("Could not read Usage page preferences.", error);
21 return DEFAULT_PREFERENCES;
22 }
24​
25export function saveUsagePagePreferences(preferences: UsagePagePreferences): void {
26 try {
27 setLocalStorageItem(STORAGE_KEY, preferences, UsagePagePreferencesSchema);
28 } catch (error) {
29 console.error("Could not save Usage page preferences.", error);
30 }