BlueWallet/components/DeviceQuickActions.tsx

184 lines
6.0 KiB
TypeScript
Raw Normal View History

2020-12-16 04:15:57 +01:00
import AsyncStorage from '@react-native-async-storage/async-storage';
2024-03-15 21:05:15 +01:00
import { CommonActions } from '@react-navigation/native';
2024-05-31 19:18:01 +02:00
import { useEffect } from 'react';
2024-03-15 21:05:15 +01:00
import { DeviceEventEmitter, Linking, Platform } from 'react-native';
import QuickActions from 'react-native-quick-actions';
2024-04-30 00:54:19 +02:00
import DeeplinkSchemaMatch from '../class/deeplink-schema-match';
import { TWallet } from '../class/wallets/types';
2024-05-20 11:54:13 +02:00
import useOnAppLaunch from '../hooks/useOnAppLaunch';
import { formatBalance } from '../loc';
import * as NavigationService from '../NavigationService';
2024-05-31 19:18:01 +02:00
import { useSettings } from '../hooks/context/useSettings';
import { useStorage } from '../hooks/context/useStorage';
2023-11-11 18:17:20 +01:00
const DeviceQuickActionsStorageKey = 'DeviceQuickActionsEnabled';
2024-04-30 00:54:19 +02:00
export async function setEnabled(enabled: boolean = true): Promise<void> {
await AsyncStorage.setItem(DeviceQuickActionsStorageKey, JSON.stringify(enabled));
}
export async function getEnabled(): Promise<boolean> {
try {
const isEnabled = await AsyncStorage.getItem(DeviceQuickActionsStorageKey);
if (isEnabled === null) {
await setEnabled(true);
return true;
}
return !!JSON.parse(isEnabled);
} catch {
return true;
}
}
function DeviceQuickActions() {
const { wallets, walletsInitialized, isStorageEncrypted, addWallet, saveToDisk, setSharedCosigner } = useStorage();
2024-04-30 00:54:19 +02:00
const { preferredFiatCurrency, isQuickActionsEnabled } = useSettings();
2024-04-18 03:05:48 +02:00
2024-03-23 00:11:31 +01:00
const { isViewAllWalletsEnabled, getSelectedDefaultWallet } = useOnAppLaunch();
useEffect(() => {
if (walletsInitialized) {
isStorageEncrypted()
2023-11-11 18:17:20 +01:00
.then((value: boolean | undefined | null) => {
if (value) {
removeShortcuts();
} else {
setQuickActions();
}
})
.catch(() => removeShortcuts());
}
// eslint-disable-next-line react-hooks/exhaustive-deps
2023-11-11 18:17:20 +01:00
}, [wallets, walletsInitialized, preferredFiatCurrency, isStorageEncrypted]);
useEffect(() => {
if (walletsInitialized) {
DeviceEventEmitter.addListener('quickActionShortcut', walletQuickActions);
2023-11-11 18:17:20 +01:00
popInitialShortcutAction().then(popInitialAction);
return () => DeviceEventEmitter.removeAllListeners('quickActionShortcut');
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [walletsInitialized]);
2024-04-30 00:54:19 +02:00
useEffect(() => {
if (walletsInitialized) {
if (isQuickActionsEnabled) {
setQuickActions();
} else {
removeShortcuts();
}
2023-11-11 18:17:20 +01:00
}
2024-04-30 00:54:19 +02:00
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isQuickActionsEnabled, walletsInitialized]);
2023-11-11 18:17:20 +01:00
const popInitialShortcutAction = async (): Promise<any> => {
const data = await QuickActions.popInitialAction();
return data;
};
2023-11-11 18:17:20 +01:00
const popInitialAction = async (data: any): Promise<void> => {
if (data) {
2023-11-11 18:17:20 +01:00
const wallet = wallets.find((w: { getID: () => any }) => w.getID() === data.userInfo.url.split('wallet/')[1]);
if (wallet) {
NavigationService.dispatch(
CommonActions.navigate({
name: 'WalletTransactions',
params: {
walletID: wallet.getID(),
walletType: wallet.type,
},
}),
);
}
} else {
const url = await Linking.getInitialURL();
if (url) {
if (DeeplinkSchemaMatch.hasSchema(url)) {
handleOpenURL({ url });
}
} else {
2024-03-23 00:11:31 +01:00
if (!(await isViewAllWalletsEnabled())) {
2024-03-23 17:33:00 +01:00
const selectedDefaultWalletID = (await getSelectedDefaultWallet()) as string;
const selectedDefaultWallet = wallets.find((w: TWallet) => w.getID() === selectedDefaultWalletID);
2023-11-11 18:17:20 +01:00
if (selectedDefaultWallet) {
2024-03-23 21:04:44 +01:00
NavigationService.dispatch(
CommonActions.navigate({
name: 'WalletTransactions',
params: {
walletID: selectedDefaultWalletID,
walletType: selectedDefaultWallet.type,
},
}),
);
}
}
}
}
};
2023-11-11 18:17:20 +01:00
const handleOpenURL = (event: { url: string }): void => {
2024-01-06 01:59:58 +01:00
DeeplinkSchemaMatch.navigationRouteFor(event, (value: [string, any]) => NavigationService.navigate(...value), {
wallets,
addWallet,
saveToDisk,
setSharedCosigner,
});
};
2023-11-11 18:17:20 +01:00
const walletQuickActions = (data: any): void => {
const wallet = wallets.find((w: { getID: () => any }) => w.getID() === data.userInfo.url.split('wallet/')[1]);
if (wallet) {
NavigationService.dispatch(
CommonActions.navigate({
name: 'WalletTransactions',
params: {
walletID: wallet.getID(),
walletType: wallet.type,
},
}),
);
}
};
2023-11-11 18:17:20 +01:00
const removeShortcuts = async (): Promise<void> => {
if (Platform.OS === 'android') {
QuickActions.clearShortcutItems();
} else {
2023-11-11 18:17:20 +01:00
// @ts-ignore: Fix later
QuickActions.setShortcutItems([{ type: 'EmptyWallets', title: '' }]);
}
};
2023-11-11 18:17:20 +01:00
const setQuickActions = async (): Promise<void> => {
2024-04-30 00:54:19 +02:00
if (await getEnabled()) {
2024-01-06 01:59:58 +01:00
QuickActions.isSupported((error: null, _supported: any) => {
if (error === null) {
const shortcutItems = [];
for (const wallet of wallets.slice(0, 4)) {
shortcutItems.push({
type: 'Wallets', // Required
title: wallet.getLabel(), // Optional, if empty, `type` will be used instead
subtitle:
wallet.hideBalance || wallet.getBalance() <= 0
? ''
: formatBalance(Number(wallet.getBalance()), wallet.getPreferredBalanceUnit(), true),
userInfo: {
url: `bluewallet://wallet/${wallet.getID()}`, // Provide any custom data like deep linking URL
},
icon: Platform.select({ android: 'quickactions', ios: 'bookmark' }),
});
}
2023-11-11 18:17:20 +01:00
// @ts-ignore: Fix later
QuickActions.setShortcutItems(shortcutItems);
}
});
} else {
removeShortcuts();
}
};
2024-04-30 00:54:19 +02:00
return null;
}
export default DeviceQuickActions;