aboutsummaryrefslogtreecommitdiffstats
path: root/apps/mobile/lib/settings.ts
diff options
context:
space:
mode:
authorMohamedBassem <me@mbassem.com>2024-03-22 15:10:24 +0000
committerMohamedBassem <me@mbassem.com>2024-03-22 15:14:24 +0000
commit2cd2f92e9e0c82eaa5f21fe0c30e20ebea7aba24 (patch)
treeacf86f033b976a40079c3efe8ec1fb727ae2a452 /apps/mobile/lib/settings.ts
parent95cc9e6ff29cd39dc80aa09c80a6d1c9489b5d6a (diff)
downloadkarakeep-2cd2f92e9e0c82eaa5f21fe0c30e20ebea7aba24.tar.zst
fix(mobile): Fix setting propagatin
Diffstat (limited to 'apps/mobile/lib/settings.ts')
-rw-r--r--apps/mobile/lib/settings.ts51
1 files changed, 34 insertions, 17 deletions
diff --git a/apps/mobile/lib/settings.ts b/apps/mobile/lib/settings.ts
index 13de067e..3ec53231 100644
--- a/apps/mobile/lib/settings.ts
+++ b/apps/mobile/lib/settings.ts
@@ -1,6 +1,5 @@
import * as SecureStore from "expo-secure-store";
-
-import { useStorageState } from "./storage-state";
+import { create } from "zustand";
const SETTING_NAME = "settings";
@@ -9,22 +8,40 @@ export interface Settings {
address: string;
}
-export default function useAppSettings() {
- const [settingsState, setSettings] = useStorageState<Settings>(SETTING_NAME);
- const [isLoading] = settingsState;
- let [, settings] = settingsState;
+interface AppSettingsState {
+ settings: { isLoading: boolean; settings: Settings };
+ setSettings: (settings: Settings) => Promise<void>;
+ load: () => Promise<void>;
+}
- settings ||= {
- address: "https://demo.hoarder.app",
- };
+const useSettings = create<AppSettingsState>((set, get) => ({
+ settings: {
+ isLoading: true,
+ settings: { address: "" },
+ },
+ 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;
+ }
+ // TODO Wipe the state if invalid
+ const parsed = JSON.parse(strVal) as Settings;
+ set((_state) => ({ settings: { isLoading: false, settings: parsed } }));
+ },
+}));
- return { settings, setSettings, isLoading };
-}
+export default function useAppSettings() {
+ const { settings, setSettings, load } = useSettings();
-export async function getAppSettings() {
- const val = await SecureStore.getItemAsync(SETTING_NAME);
- if (!val) {
- return null;
- }
- return JSON.parse(val) as Settings;
+ return { ...settings, setSettings, load };
}