BlueWallet/screen/wallets/DrawerList.tsx

162 lines
4.6 KiB
TypeScript
Raw Normal View History

import { DrawerContentScrollView } from '@react-navigation/drawer';
2024-05-20 11:54:13 +02:00
import { NavigationProp, ParamListBase, useIsFocused } from '@react-navigation/native';
2024-05-31 19:18:01 +02:00
import React, { memo, useCallback, useEffect, useMemo, useReducer, useRef } from 'react';
2024-05-20 11:54:13 +02:00
import { FlatList, InteractionManager, LayoutAnimation, StyleSheet, ViewStyle } from 'react-native';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
2024-03-15 21:05:15 +01:00
import { TWallet } from '../../class/wallets/types';
2024-05-04 00:33:23 +02:00
import { Header } from '../../components/Header';
2024-05-20 11:54:13 +02:00
import { useTheme } from '../../components/themes';
import WalletsCarousel from '../../components/WalletsCarousel';
import loc from '../../loc';
2024-05-31 19:18:01 +02:00
import { useStorage } from '../../hooks/context/useStorage';
2024-03-17 06:29:19 +01:00
enum WalletActionType {
SetWallets = 'SET_WALLETS',
SelectWallet = 'SELECT_WALLET',
SetFocus = 'SET_FOCUS',
2024-03-17 06:55:05 +01:00
Navigate = 'NAVIGATE',
2024-03-17 06:29:19 +01:00
}
interface WalletState {
2024-03-15 21:05:15 +01:00
wallets: TWallet[];
2024-03-17 06:29:19 +01:00
isFocused: boolean;
}
2024-03-17 06:55:05 +01:00
interface SelectWalletAction {
type: WalletActionType.SelectWallet;
walletID: string;
walletType: string;
2024-03-17 06:29:19 +01:00
}
interface SelectWalletAction {
type: WalletActionType.SelectWallet;
walletID: string;
}
2024-03-17 06:55:05 +01:00
interface NavigateAction {
type: WalletActionType.Navigate;
screen: string;
params: { [key: string]: any };
}
2024-03-17 06:29:19 +01:00
interface SetFocusAction {
type: WalletActionType.SetFocus;
isFocused: boolean;
}
2024-03-17 06:55:05 +01:00
interface SetWalletsAction {
type: WalletActionType.SetWallets;
2024-03-15 21:05:15 +01:00
wallets: TWallet[];
2024-03-17 06:55:05 +01:00
}
interface SelectWalletAction {
type: WalletActionType.SelectWallet;
walletID: string;
}
type WalletAction = SetWalletsAction | SelectWalletAction | SetFocusAction | NavigateAction;
2024-03-17 06:29:19 +01:00
2024-03-17 06:17:19 +01:00
interface DrawerListProps {
navigation: NavigationProp<ParamListBase>;
}
2024-03-17 06:29:19 +01:00
const walletReducer = (state: WalletState, action: WalletAction): WalletState => {
switch (action.type) {
2024-03-17 06:55:05 +01:00
case WalletActionType.SetWallets: {
return {
...state,
wallets: action.wallets,
};
}
case WalletActionType.SetFocus: {
2024-03-17 06:29:19 +01:00
return { ...state, isFocused: action.isFocused };
2024-03-17 06:55:05 +01:00
}
2024-03-17 06:29:19 +01:00
default:
return state;
}
};
const DrawerList: React.FC<DrawerListProps> = memo(({ navigation }) => {
const initialState: WalletState = {
wallets: [],
isFocused: false,
};
const [state, dispatch] = useReducer(walletReducer, initialState);
2024-03-15 21:05:15 +01:00
const walletsCarousel = useRef<FlatList<TWallet>>(null);
const { wallets, selectedWalletID } = useStorage();
const { colors } = useTheme();
const isFocused = useIsFocused();
2024-03-17 06:17:19 +01:00
const stylesHook = useMemo(
() =>
StyleSheet.create({
2024-03-17 06:29:19 +01:00
root: { backgroundColor: colors.elevated } as ViewStyle,
2024-03-17 06:17:19 +01:00
}),
[colors.elevated],
);
2021-02-08 04:54:04 +01:00
useEffect(() => {
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
2024-03-17 06:29:19 +01:00
dispatch({ type: WalletActionType.SetWallets, wallets });
dispatch({ type: WalletActionType.SetFocus, isFocused });
}, [wallets, isFocused]);
2021-02-08 04:54:04 +01:00
2024-03-17 06:17:19 +01:00
const handleClick = useCallback(
2024-03-15 21:05:15 +01:00
(item: TWallet) => {
2024-03-17 06:17:19 +01:00
if (item?.getID) {
const walletID = item.getID();
2024-03-17 06:55:05 +01:00
const walletType = item.type;
2024-03-17 06:17:19 +01:00
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
2024-03-17 06:55:05 +01:00
dispatch({ type: WalletActionType.SelectWallet, walletID, walletType });
2024-03-17 06:39:30 +01:00
InteractionManager.runAfterInteractions(() => {
navigation.navigate({
name: 'WalletTransactions',
2024-03-17 06:55:05 +01:00
params: { walletID, walletType },
2024-03-17 06:39:30 +01:00
});
2024-03-17 06:17:19 +01:00
});
} else {
2024-05-09 04:08:03 +02:00
navigation.navigate('AddWalletRoot');
2024-03-17 06:17:19 +01:00
}
},
2024-03-17 06:29:19 +01:00
[navigation],
2024-03-17 06:17:19 +01:00
);
2024-03-17 06:17:19 +01:00
const handleLongPress = useCallback(() => {
2024-03-17 06:29:19 +01:00
if (state.wallets.length > 1) {
navigation.navigate('ReorderWallets');
} else {
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
}
2024-03-17 06:29:19 +01:00
}, [state.wallets.length, navigation]);
2024-03-17 06:17:19 +01:00
const onNewWalletPress = useCallback(() => {
2024-03-17 06:55:05 +01:00
navigation.navigate('AddWalletRoot');
2024-03-17 06:29:19 +01:00
}, [navigation]);
2021-06-16 08:05:04 +02:00
return (
2023-03-27 06:07:46 +02:00
<DrawerContentScrollView
2024-03-21 00:14:53 +01:00
contentContainerStyle={stylesHook.root}
2023-03-27 06:07:46 +02:00
contentInsetAdjustmentBehavior="automatic"
2024-03-17 06:29:19 +01:00
automaticallyAdjustContentInsets={true}
2023-03-27 06:07:46 +02:00
showsHorizontalScrollIndicator={false}
showsVerticalScrollIndicator={false}
>
<Header leftText={loc.wallets.list_title} onNewWalletPress={onNewWalletPress} isDrawerList />
2023-03-27 06:07:46 +02:00
<WalletsCarousel
2024-03-17 06:55:05 +01:00
// @ts-ignore: refactor later
2024-03-17 06:29:19 +01:00
data={state.wallets.concat(false as any)}
extraData={[state.wallets]}
2023-03-27 06:07:46 +02:00
onPress={handleClick}
handleLongPress={handleLongPress}
ref={walletsCarousel}
testID="WalletsList"
selectedWallet={selectedWalletID}
2024-03-17 06:29:19 +01:00
scrollEnabled={state.isFocused}
2023-03-27 06:07:46 +02:00
/>
</DrawerContentScrollView>
);
2024-03-17 06:17:19 +01:00
});
export default DrawerList;