Update useOnAppLaunch.ts

This commit is contained in:
Marcos Rodriguez Velez 2024-03-23 12:03:18 -04:00
parent 63ebc7b6b1
commit 44ed9a27fa
No known key found for this signature in database
GPG key ID: 6030B2F48CCE86D7

View file

@ -1,28 +1,31 @@
import { useCallback, useContext } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { AbstractWallet } from '../class';
import { BlueStorageContext } from '../blue_modules/storage-context';
import { TWallet } from '../class/wallets/types';
const useOnAppLaunch = () => {
const STORAGE_KEY = 'ONAPP_LAUNCH_SELECTED_DEFAULT_WALLET_KEY';
const { wallets } = useContext(BlueStorageContext);
const getSelectedDefaultWallet = useCallback(async (): Promise<AbstractWallet | false> => {
let selectedWallet: AbstractWallet | false = false;
const getSelectedDefaultWallet = useCallback(async (): Promise<TWallet | undefined> => {
let selectedWallet: TWallet | undefined;
try {
const selectedWalletID = JSON.parse((await AsyncStorage.getItem(STORAGE_KEY)) || 'null');
if (selectedWalletID) {
selectedWallet = wallets.find((wallet: AbstractWallet) => wallet.getID() === selectedWalletID);
if (selectedWalletID !== null) {
selectedWallet = wallets.find((wallet: TWallet) => wallet.getID() === selectedWalletID);
if (!selectedWallet) {
await AsyncStorage.removeItem(STORAGE_KEY);
return undefined;
}
} else {
return undefined;
}
} catch (_e) {
return false;
return undefined;
}
return selectedWallet;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [STORAGE_KEY]); // No external dependencies
}, [STORAGE_KEY]);
const setSelectedDefaultWallet = useCallback(
async (value: string): Promise<void> => {
@ -54,7 +57,7 @@ const useOnAppLaunch = () => {
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[STORAGE_KEY, getSelectedDefaultWallet, setSelectedDefaultWallet],
); // Include dependencies here
);
return {
isViewAllWalletsEnabled,
@ -64,4 +67,4 @@ const useOnAppLaunch = () => {
};
};
export default useOnAppLaunch;
export default useOnAppLaunch;