import React, { useEffect, useReducer } from 'react'; import { View, TouchableWithoutFeedback, ScrollView } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import { BlueCard, BlueText } from '../../BlueComponents'; import loc from '../../loc'; import { useStorage } from '../../blue_modules/storage-context'; import ListItem from '../../components/ListItem'; import useOnAppLaunch from '../../hooks/useOnAppLaunch'; import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { TWallet } from '../../class/wallets/types'; type RootStackParamList = { SelectWallet: { onWalletSelect: (wallet: TWallet) => void; onChainRequireSend: boolean }; }; type DefaultViewNavigationProp = NativeStackNavigationProp; const enum ActionType { SetDefaultWalletLabel = 'SET_DEFAULT_WALLET_LABEL', SetViewAllWalletsSwitch = 'SET_VIEW_ALL_WALLETS_SWITCH', } type State = { defaultWalletLabel: string; isViewAllWalletsSwitchEnabled: boolean; }; type Action = { type: ActionType.SetDefaultWalletLabel; payload: string } | { type: ActionType.SetViewAllWalletsSwitch; payload: boolean }; const reducer = (state: State, action: Action): State => { switch (action.type) { case ActionType.SetDefaultWalletLabel: return { ...state, defaultWalletLabel: action.payload }; case ActionType.SetViewAllWalletsSwitch: return { ...state, isViewAllWalletsSwitchEnabled: action.payload }; default: return state; } }; const DefaultView: React.FC = () => { const [state, dispatch] = useReducer(reducer, { defaultWalletLabel: '', isViewAllWalletsSwitchEnabled: true, }); const { navigate, pop } = useNavigation(); const { wallets } = useStorage(); const { isViewAllWalletsEnabled, getSelectedDefaultWallet, setSelectedDefaultWallet, setViewAllWalletsEnabled } = useOnAppLaunch(); useEffect(() => { (async () => { const newViewAllWalletsEnabled: boolean = await isViewAllWalletsEnabled(); let newDefaultWalletLabel: string = ''; const walletID = await getSelectedDefaultWallet(); if (walletID) { const w = wallets.find(wallet => wallet.getID() === walletID); if (w) newDefaultWalletLabel = w.getLabel(); } dispatch({ type: ActionType.SetDefaultWalletLabel, payload: newDefaultWalletLabel }); dispatch({ type: ActionType.SetViewAllWalletsSwitch, payload: newViewAllWalletsEnabled }); })(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const onViewAllWalletsSwitchValueChanged = async (value: boolean) => { await setViewAllWalletsEnabled(value); dispatch({ type: ActionType.SetViewAllWalletsSwitch, payload: value }); if (!value && wallets.length === 1) { // Automatically select the wallet if there is only one const selectedWallet = wallets[0]; await setSelectedDefaultWallet(selectedWallet.getID()); dispatch({ type: ActionType.SetDefaultWalletLabel, payload: selectedWallet.getLabel() }); } else if (!value) { const selectedWalletID = await getSelectedDefaultWallet(); const selectedWallet = wallets.find(wallet => wallet.getID() === selectedWalletID); if (selectedWallet) { dispatch({ type: ActionType.SetDefaultWalletLabel, payload: selectedWallet.getLabel() }); } } }; const selectWallet = () => { navigate('SelectWallet', { onWalletSelect: onWalletSelectValueChanged, onChainRequireSend: false }); }; const onWalletSelectValueChanged = async (wallet: TWallet) => { await setViewAllWalletsEnabled(false); await setSelectedDefaultWallet(wallet.getID()); dispatch({ type: ActionType.SetDefaultWalletLabel, payload: wallet.getLabel() }); dispatch({ type: ActionType.SetViewAllWalletsSwitch, payload: false }); pop(); }; return ( {loc.settings.default_desc} {!state.isViewAllWalletsSwitchEnabled && ( )} ); }; export default DefaultView;