Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Duplicate key error in Privacy and Settings #11663

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 211 additions & 8 deletions app/components/Views/Settings/SecuritySettings/SecuritySettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
View,
ActivityIndicator,
Keyboard,
Platform,
Linking,
} from 'react-native';
import StorageWrapper from '../../../../store/storage-wrapper';
Expand Down Expand Up @@ -53,24 +54,41 @@
selectNetworkConfigurations,
} from '../../../../selectors/networkController';
import {
selectIpfsGateway,
selectIsIpfsGatewayEnabled,
selectIsMultiAccountBalancesEnabled,
selectDisplayNftMedia,
selectShowIncomingTransactionNetworks,
selectShowTestNetworks,
selectUseSafeChainsListValidation,
selectUseTransactionSimulations,
} from '../../../../selectors/preferencesController';
import { SECURITY_PRIVACY_VIEW_ID } from '../../../../../wdio/screen-objects/testIDs/Screens/SecurityPrivacy.testIds';
import {
SECURITY_PRIVACY_MULTI_ACCOUNT_BALANCES_TOGGLE_ID,

Check failure on line 67 in app/components/Views/Settings/SecuritySettings/SecuritySettings.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Module '"../../../../../wdio/screen-objects/testIDs/Screens/SecurityPrivacy.testIds"' has no exported member 'SECURITY_PRIVACY_MULTI_ACCOUNT_BALANCES_TOGGLE_ID'.
SECURITY_PRIVACY_VIEW_ID,
} from '../../../../../wdio/screen-objects/testIDs/Screens/SecurityPrivacy.testIds';
import generateTestId from '../../../../../wdio/utils/generateTestId';
import ipfsGateways from '../../../../util/ipfs-gateways.json';
import SelectComponent from '../../../UI/SelectComponent';
import { timeoutFetch } from '../../../../util/general';
import createStyles from './SecuritySettings.styles';
import {
Gateway,

Check failure on line 76 in app/components/Views/Settings/SecuritySettings/SecuritySettings.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Module '"./SecuritySettings.types"' has no exported member 'Gateway'.
HeadingProps,
NetworksI,
SecuritySettingsParams,
} from './SecuritySettings.types';
import { useFocusEffect, useNavigation } from '@react-navigation/native';
import { useParams } from '../../../../util/navigation/navUtils';
import {
BATCH_BALANCE_REQUESTS_SECTION,

Check failure on line 84 in app/components/Views/Settings/SecuritySettings/SecuritySettings.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Module '"./SecuritySettings.constants"' has no exported member 'BATCH_BALANCE_REQUESTS_SECTION'.
BIOMETRY_CHOICE_STRING,
CLEAR_BROWSER_HISTORY_SECTION,
DISPLAY_SAFE_CHAINS_LIST_VALIDATION,
HASH_STRING,

Check failure on line 88 in app/components/Views/Settings/SecuritySettings/SecuritySettings.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Module '"./SecuritySettings.constants"' has no exported member 'HASH_STRING'.
HASH_TO_TEST,

Check failure on line 89 in app/components/Views/Settings/SecuritySettings/SecuritySettings.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Module '"./SecuritySettings.constants"' has no exported member 'HASH_TO_TEST'.
IPFS_GATEWAY_SECTION,

Check failure on line 90 in app/components/Views/Settings/SecuritySettings/SecuritySettings.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Module '"./SecuritySettings.constants"' has no exported member 'IPFS_GATEWAY_SECTION'.
NFT_DISPLAY_MEDIA_MODE_SECTION,

Check failure on line 91 in app/components/Views/Settings/SecuritySettings/SecuritySettings.tsx

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Module '"./SecuritySettings.constants"' has no exported member 'NFT_DISPLAY_MEDIA_MODE_SECTION'.
PASSCODE_CHOICE_STRING,
SDK_SECTION,
USE_SAFE_CHAINS_LIST_VALIDATION,
Expand Down Expand Up @@ -111,9 +129,6 @@
import { EtherscanSupportedHexChainId } from '@metamask/preferences-controller';
import { useDisableNotifications } from '../../../../util/notifications/hooks/useNotifications';
import { isNotificationsFeatureEnabled } from '../../../../util/notifications';
import DisplayNFTMediaSettings from '../../Settings/DisplayNFTMediaSettings';
import IPFSGatewaySettings from '../../Settings/IPFSGatewaySettings';
import BatchAccountBalanceSettings from '../BatchAccountBalanceSettings';
import AutoDetectNFTSettings from '../../Settings/AutoDetectNFTSettings';

const Heading: React.FC<HeadingProps> = ({ children, first }) => {
Expand Down Expand Up @@ -142,6 +157,8 @@
const [analyticsEnabled, setAnalyticsEnabled] = useState(false);
const [showHint, setShowHint] = useState(false);
const [hintText, setHintText] = useState('');
const [onlineIpfsGateways, setOnlineIpfsGateways] = useState<Gateway[]>([]);
const [gotAvailableGateways, setGotAvailableGateways] = useState(false);
const isProfileSyncingEnabled = useSelector(selectIsProfileSyncingEnabled);
const isBasicFunctionalityEnabled = useSelector(
(state: RootState) => state?.settings?.basicFunctionalityEnabled,
Expand Down Expand Up @@ -172,6 +189,7 @@
selectShowIncomingTransactionNetworks,
);
const networkConfigurations = useSelector(selectNetworkConfigurations);
const displayNftMedia = useSelector(selectDisplayNftMedia);
const useSafeChainsListValidation = useSelector(
selectUseSafeChainsListValidation,
);
Expand All @@ -189,6 +207,11 @@
(state: any) => state.user.seedphraseBackedUp,
);
const type = useSelector(selectProviderType);
const isMultiAccountBalancesEnabled = useSelector(
selectIsMultiAccountBalancesEnabled,
);
const ipfsGateway = useSelector(selectIpfsGateway);
const isIpfsGatewayEnabled = useSelector(selectIsIpfsGatewayEnabled);
const myNetworks = ETHERSCAN_SUPPORTED_NETWORKS;
const isMainnet = type === MAINNET;

Expand All @@ -204,6 +227,34 @@
);
}, [colors, navigation]);

const handleAvailableIpfsGateways = useCallback(async () => {
if (!isIpfsGatewayEnabled) return;
const ipfsGatewaysPromises = ipfsGateways.map(async (gateway: Gateway) => {
const testUrl =
gateway.value + HASH_TO_TEST + '#x-ipfs-companion-no-redirect';
try {
const res = await timeoutFetch(testUrl, 1200);
const text = await res.text();
const available = text.trim() === HASH_STRING.trim();
return { ...gateway, available };
} catch (e) {
const available = false;
return { ...gateway, available };
}
});
const ipfsGatewaysAvailability = await Promise.all(ipfsGatewaysPromises);
const onlineGateways = ipfsGatewaysAvailability.filter(
(gateway) => gateway.available,
);

const sortedOnlineIpfsGateways = [...onlineGateways].sort(
(a, b) => a.key - b.key,
);

setGotAvailableGateways(true);
setOnlineIpfsGateways(sortedOnlineIpfsGateways);
}, [isIpfsGatewayEnabled]);

const handleHintText = useCallback(async () => {
const currentSeedphraseHints = await StorageWrapper.getItem(
SEED_PHRASE_HINTS,
Expand Down Expand Up @@ -277,6 +328,10 @@
}, [waitForRenderDetectNftComponentRef]),
);

useEffect(() => {
handleAvailableIpfsGateways();
}, [handleAvailableIpfsGateways]);

const toggleHint = () => {
setShowHint(!showHint);
};
Expand Down Expand Up @@ -444,6 +499,48 @@
</View>
);

const toggleIsMultiAccountBalancesEnabled = (
multiAccountBalancesEnabled: boolean,
) => {
const { PreferencesController } = Engine.context;
PreferencesController.setIsMultiAccountBalancesEnabled(
multiAccountBalancesEnabled,
);
};

const renderMultiAccountBalancesSection = () => (
<View style={styles.halfSetting} testID={BATCH_BALANCE_REQUESTS_SECTION}>
<View style={styles.titleContainer}>
<Text variant={TextVariant.BodyLGMedium} style={styles.title}>
{strings('app_settings.batch_balance_requests_title')}
</Text>
<View style={styles.switchElement}>
<Switch
value={isMultiAccountBalancesEnabled}
onValueChange={toggleIsMultiAccountBalancesEnabled}
trackColor={{
true: colors.primary.default,
false: colors.border.muted,
}}
thumbColor={theme.brandColors.white}
style={styles.switch}
ios_backgroundColor={colors.border.muted}
{...generateTestId(
Platform,
SECURITY_PRIVACY_MULTI_ACCOUNT_BALANCES_TOGGLE_ID,
)}
/>
</View>
</View>
<Text
variant={TextVariant.BodyMD}
color={TextColor.Alternative}
style={styles.desc}
>
{strings('app_settings.batch_balance_requests_description')}
</Text>
</View>
);
const toggleEnableIncomingTransactions = (
hexChainId: EtherscanSupportedHexChainId,
value: boolean,
Expand Down Expand Up @@ -480,6 +577,46 @@
</ActionModal>
);

const toggleDisplayNftMedia = (value: boolean) => {
const { PreferencesController } = Engine.context;
PreferencesController?.setDisplayNftMedia(value);
if (!value) PreferencesController?.setUseNftDetection(value);
};

const renderDisplayNftMedia = useCallback(
() => (
<View style={styles.halfSetting} testID={NFT_DISPLAY_MEDIA_MODE_SECTION}>
<View style={styles.titleContainer}>
<Text variant={TextVariant.BodyLGMedium} style={styles.title}>
{strings('app_settings.display_nft_media')}
</Text>
<View style={styles.switchElement}>
<Switch
value={displayNftMedia}
onValueChange={toggleDisplayNftMedia}
trackColor={{
true: colors.primary.default,
false: colors.border.muted,
}}
thumbColor={theme.brandColors.white}
style={styles.switch}
ios_backgroundColor={colors.border.muted}
testID="display-nft-toggle"
/>
</View>
</View>
<Text
variant={TextVariant.BodyMD}
color={TextColor.Alternative}
style={styles.desc}
>
{strings('app_settings.display_nft_media_desc_new')}
</Text>
</View>
),
[colors, styles, displayNftMedia, theme],
);

const renderUseSafeChainsListValidation = useCallback(
() => (
<View style={styles.halfSetting} testID={USE_SAFE_CHAINS_LIST_VALIDATION}>
Expand Down Expand Up @@ -571,6 +708,72 @@
[colors, styles, useTransactionSimulations, theme.brandColors.white],
);

const setIpfsGateway = (gateway: string) => {
const { PreferencesController } = Engine.context;
PreferencesController.setIpfsGateway(gateway);
};

const setIsIpfsGatewayEnabled = (isIpfsGatewatEnabled: boolean) => {
const { PreferencesController } = Engine.context;
PreferencesController.setIsIpfsGatewayEnabled(isIpfsGatewatEnabled);
};

const renderIpfsGateway = () => (
<View style={styles.setting} testID={IPFS_GATEWAY_SECTION}>
<View style={styles.titleContainer}>
<Text variant={TextVariant.BodyLGMedium} style={styles.title}>
{strings('app_settings.ipfs_gateway')}
</Text>
<View style={styles.switchElement}>
<Switch
value={isIpfsGatewayEnabled}
onValueChange={setIsIpfsGatewayEnabled}
trackColor={{
true: colors.primary.default,
false: colors.border.muted,
}}
thumbColor={theme.brandColors.white}
style={styles.switch}
ios_backgroundColor={colors.border.muted}
/>
</View>
</View>
<Text
variant={TextVariant.BodyMD}
color={TextColor.Alternative}
style={styles.desc}
>
{strings('app_settings.ipfs_gateway_content')}
</Text>
{isIpfsGatewayEnabled && (
<View style={styles.accessory}>
<Text
variant={TextVariant.BodyMD}
color={TextColor.Alternative}
style={styles.desc}
>
{strings('app_settings.ipfs_gateway_desc')}
</Text>
<View style={styles.picker}>
{gotAvailableGateways ? (
<SelectComponent
selectedValue={ipfsGateway}
defaultValue={strings('app_settings.ipfs_gateway_down')}
onValueChange={setIpfsGateway}
label={strings('app_settings.ipfs_gateway')}
options={onlineIpfsGateways}
/>
) : (
<View>
<ActivityIndicator size="small" />
</View>
)}
</View>
</View>
)}
</View>
);

const handleChangeText = (text: string) => setHintText(text);

const renderHint = () => (
Expand Down Expand Up @@ -667,7 +870,7 @@

return (
<Cell
key={chainId}
key={`${nickname}-${chainId}`}
variant={CellVariant.Display}
title={name}
secondaryText={
Expand Down Expand Up @@ -878,7 +1081,7 @@
>
{strings('app_settings.transactions_subheading')}
</Text>
<BatchAccountBalanceSettings />
{renderMultiAccountBalancesSection()}
{renderShowIncomingTransactions()}
{renderHistoryModal()}
{renderUseTransactionSimulations()}
Expand All @@ -889,13 +1092,13 @@
>
{strings('app_settings.token_nft_ens_subheading')}
</Text>
<DisplayNFTMediaSettings />
{renderDisplayNftMedia()}
{isMainnet && (
<View ref={detectNftComponentRef}>
<AutoDetectNFTSettings />
</View>
)}
<IPFSGatewaySettings />
{renderIpfsGateway()}
<Text
variant={TextVariant.BodyLGMedium}
color={TextColor.Alternative}
Expand Down
Loading