1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
import * as SecureStore from "expo-secure-store";
import { z } from "zod";
import { create } from "zustand";
const SETTING_NAME = "settings";
const zSettingsSchema = z.object({
apiKey: z.string().optional(),
apiKeyId: z.string().optional(),
address: z.string().optional().default("https://cloud.karakeep.app"),
imageQuality: z.number().optional().default(0.2),
theme: z.enum(["light", "dark", "system"]).optional().default("system"),
defaultBookmarkView: z
.enum(["reader", "browser"])
.optional()
.default("reader"),
showNotes: z.boolean().optional().default(false),
customHeaders: z.record(z.string(), z.string()).optional().default({}),
});
export type Settings = z.infer<typeof zSettingsSchema>;
interface AppSettingsState {
settings: { isLoading: boolean; settings: Settings };
setSettings: (settings: Settings) => Promise<void>;
load: () => Promise<void>;
}
const useSettings = create<AppSettingsState>((set, get) => ({
settings: {
isLoading: true,
settings: {
address: "https://cloud.karakeep.app",
imageQuality: 0.2,
theme: "system",
defaultBookmarkView: "reader",
showNotes: false,
customHeaders: {},
},
},
setSettings: async (settings) => {
await SecureStore.setItemAsync(SETTING_NAME, JSON.stringify(settings));
set((_state) => ({ settings: { isLoading: false, settings } }));
},
load: async () => {
if (!get().settings.isLoading) {
return;
}
const strVal = await SecureStore.getItemAsync(SETTING_NAME);
if (!strVal) {
set((state) => ({
settings: { isLoading: false, settings: state.settings.settings },
}));
return;
}
const parsed = zSettingsSchema.safeParse(JSON.parse(strVal));
if (!parsed.success) {
// Wipe the state if invalid
set((state) => ({
settings: { isLoading: false, settings: state.settings.settings },
}));
return;
}
set((_state) => ({
settings: { isLoading: false, settings: parsed.data },
}));
},
}));
export default function useAppSettings() {
const { settings, setSettings, load } = useSettings();
return { ...settings, setSettings, load };
}
|