Open History to see the last commit
1import { useCallback, useSyncExternalStore } from "react";
2import { Option, Schema } from "effect";
3
4const APP_SETTINGS_STORAGE_KEY = "t3code:app-settings:v1";
5
6const AppSettingsSchema = Schema.Struct({
7 codexBinaryPath: Schema.String.check(Schema.isMaxLength(4096)).pipe(
8 Schema.withConstructorDefault(() => Option.some("")),
9 ),
10 codexHomePath: Schema.String.check(Schema.isMaxLength(4096)).pipe(
11 Schema.withConstructorDefault(() => Option.some("")),
12 ),
13 confirmThreadDelete: Schema.Boolean.pipe(Schema.withConstructorDefault(() => Option.some(true))),
14 enableAssistantStreaming: Schema.Boolean.pipe(
15 Schema.withConstructorDefault(() => Option.some(false)),
16 ),
17});
18export type AppSettings = typeof AppSettingsSchema.Type;
19
20const DEFAULT_APP_SETTINGS = AppSettingsSchema.makeUnsafe({});
21
22let listeners: Array<() => void> = [];
23let cachedRawSettings: string | null | undefined;
24let cachedSnapshot: AppSettings = DEFAULT_APP_SETTINGS;
25
26function emitChange(): void {
27 for (const listener of listeners) {
28 listener();
29 }
30}
31
32function parsePersistedSettings(value: string | null): AppSettings {
33 if (!value) {
34 return DEFAULT_APP_SETTINGS;
35 }
36
37 try {
38 return Schema.decodeSync(Schema.fromJsonString(AppSettingsSchema))(value);
39 } catch {
40 return DEFAULT_APP_SETTINGS;
41 }
42}
43
44export function getAppSettingsSnapshot(): AppSettings {
45 if (typeof window === "undefined") {
46 return DEFAULT_APP_SETTINGS;
47 }
48
49 const raw = window.localStorage.getItem(APP_SETTINGS_STORAGE_KEY);
50 if (raw === cachedRawSettings) {
51 return cachedSnapshot;
52 }
53
54 cachedRawSettings = raw;
55 cachedSnapshot = parsePersistedSettings(raw);
56 return cachedSnapshot;
57}
58
59function persistSettings(next: AppSettings): void {
60 if (typeof window === "undefined") return;
61
62 const raw = JSON.stringify(next);
63 try {
64 if (raw !== cachedRawSettings) {
65 window.localStorage.setItem(APP_SETTINGS_STORAGE_KEY, raw);
66 }
67 } catch {
68 // Best-effort persistence only.
69 }
70
71 cachedRawSettings = raw;
72 cachedSnapshot = next;
73}
74
75function subscribe(listener: () => void): () => void {
76 listeners.push(listener);
77
78 const onStorage = (event: StorageEvent) => {
79 if (event.key === APP_SETTINGS_STORAGE_KEY) {
80 emitChange();
81 }
82 };
83
84 window.addEventListener("storage", onStorage);
85 return () => {
86 listeners = listeners.filter((entry) => entry !== listener);
87 window.removeEventListener("storage", onStorage);
88 };
89}
90
91export function useAppSettings() {
92 const settings = useSyncExternalStore(
93 subscribe,
94 getAppSettingsSnapshot,
95 () => DEFAULT_APP_SETTINGS,
96 );
97
98 const updateSettings = useCallback((patch: Partial<AppSettings>) => {
99 const next = Schema.decodeSync(AppSettingsSchema)({
100 ...getAppSettingsSnapshot(),
101 ...patch,
102 });
103 persistSettings(next);
104 emitChange();
105 }, []);
106
107 const resetSettings = useCallback(() => {
108 persistSettings(DEFAULT_APP_SETTINGS);
109 emitChange();
110 }, []);
111
112 return {
113 settings,
114 updateSettings,
115 resetSettings,
116 defaults: DEFAULT_APP_SETTINGS,
117 } as const;
118}