diff options
| author | MohamedBassem <me@mbassem.com> | 2024-03-22 15:10:24 +0000 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-03-22 15:14:24 +0000 |
| commit | 2cd2f92e9e0c82eaa5f21fe0c30e20ebea7aba24 (patch) | |
| tree | acf86f033b976a40079c3efe8ec1fb727ae2a452 /apps/mobile/lib/storage-state.ts | |
| parent | 95cc9e6ff29cd39dc80aa09c80a6d1c9489b5d6a (diff) | |
| download | karakeep-2cd2f92e9e0c82eaa5f21fe0c30e20ebea7aba24.tar.zst | |
fix(mobile): Fix setting propagatin
Diffstat (limited to 'apps/mobile/lib/storage-state.ts')
| -rw-r--r-- | apps/mobile/lib/storage-state.ts | 51 |
1 files changed, 0 insertions, 51 deletions
diff --git a/apps/mobile/lib/storage-state.ts b/apps/mobile/lib/storage-state.ts deleted file mode 100644 index f45ddfe5..00000000 --- a/apps/mobile/lib/storage-state.ts +++ /dev/null @@ -1,51 +0,0 @@ -import * as React from "react"; -import * as SecureStore from "expo-secure-store"; - -type UseStateHook<T> = [[boolean, T | null], (value: T | null) => void]; - -function useAsyncState<T>( - initialValue: [boolean, T | null] = [true, null], -): UseStateHook<T> { - return React.useReducer( - ( - _state: [boolean, T | null], - action: T | null = null, - ): [boolean, T | null] => [false, action], - initialValue, - ) as UseStateHook<T>; -} - -export async function setStorageItemAsync(key: string, value: string | null) { - if (value == null) { - await SecureStore.deleteItemAsync(key); - } else { - await SecureStore.setItemAsync(key, value); - } -} - -export function useStorageState<T>(key: string): UseStateHook<T> { - // Public - const [state, setState] = useAsyncState<T>(); - - // Get - React.useEffect(() => { - SecureStore.getItemAsync(key).then((value) => { - if (!value) { - setState(null); - return null; - } - setState(JSON.parse(value) as T); - }); - }, [key]); - - // Set - const setValue = React.useCallback( - (value: T | null) => { - setState(value); - setStorageItemAsync(key, JSON.stringify(value)); - }, - [key], - ); - - return [state, setValue]; -} |
