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

clientPersistenceStorage.ts

25 lines706 Bytes

1import { ClientSettingsSchema, type ClientSettings } from "@t3tools/contracts";
2​
3import { getLocalStorageItem, setLocalStorageItem } from "./hooks/useLocalStorage";
4​
5const CLIENT_SETTINGS_STORAGE_KEY = "t3code:client-settings:v1";
6​
7function hasWindow(): boolean {
8 return typeof window !== "undefined";
9}
10​
11export function readBrowserClientSettings(): ClientSettings | null {
12 if (!hasWindow()) {
13 return null;
14 }
15​
16 return getLocalStorageItem(CLIENT_SETTINGS_STORAGE_KEY, ClientSettingsSchema);
18​
19export function writeBrowserClientSettings(settings: ClientSettings): void {
20 if (!hasWindow()) {
21 return;
22 }
23​
24 setLocalStorageItem(CLIENT_SETTINGS_STORAGE_KEY, settings, ClientSettingsSchema);