diff --git a/packages/gui/src/components/walletConnect/WalletConnectAddConnectionDialog.tsx b/packages/gui/src/components/walletConnect/WalletConnectAddConnectionDialog.tsx index 5f77ff7327..8cd7cba9df 100644 --- a/packages/gui/src/components/walletConnect/WalletConnectAddConnectionDialog.tsx +++ b/packages/gui/src/components/walletConnect/WalletConnectAddConnectionDialog.tsx @@ -1,4 +1,4 @@ -import { useGetKeysQuery, useGetLoggedInFingerprintQuery } from '@chia-network/api-react'; +import { useGetKeysQuery, useGetLoggedInFingerprintQuery, usePrefs } from '@chia-network/api-react'; import { ButtonLoading, DialogActions, @@ -8,15 +8,27 @@ import { Form, Loading, useCurrencyCode, - CardListItem, } from '@chia-network/core'; import { Trans, t } from '@lingui/macro'; import CloseIcon from '@mui/icons-material/Close'; -import { Box, Divider, Dialog, DialogContent, DialogTitle, IconButton, Typography, Checkbox } from '@mui/material'; +import { + Box, + Divider, + Dialog, + DialogContent, + DialogTitle, + IconButton, + Typography, + Select, + MenuItem, + Checkbox, + Chip, +} from '@mui/material'; import React, { useEffect, useState } from 'react'; import { useForm, useWatch } from 'react-hook-form'; import useWalletConnectContext from '../../hooks/useWalletConnectContext'; +import useWalletConnectPreferences from '../../hooks/useWalletConnectPreferences'; import HeroImage from './images/walletConnectToChia.svg'; @@ -41,6 +53,8 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd const [step, setStep] = useState(Step.CONNECT); const { pair, isLoading: isLoadingWallet } = useWalletConnectContext(); const { data: keys, isLoading: isLoadingPublicKeys } = useGetKeysQuery({}); + const [sortedWallets] = usePrefs('sortedWallets', []); + const { data: fingerprint, isLoading: isLoadingLoggedInFingerprint } = useGetLoggedInFingerprintQuery(); const mainnet = useCurrencyCode() === 'XCH'; const methods = useForm({ @@ -58,6 +72,20 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd defaultValue: [], }); + const { allowConfirmationFingerprintChange, setAllowConfirmationFingerprintChange } = useWalletConnectPreferences(); + + const sortedKeysMemo = React.useMemo(() => { + const sortedKeys: any[] = sortedWallets + .map((value: string) => (keys || []).find((f: any) => value === String(f.fingerprint))) + .filter((x: any) => !!x); /* if we added a new wallet and order was not saved yet case */ + (keys || []).forEach((f: any) => { + if (sortedKeys.map((f2: any) => f2.fingerprint).indexOf(f.fingerprint) === -1) { + sortedKeys.push(f); + } + }); + return sortedKeys; + }, [sortedWallets, keys]); + function handleClose() { onClose(); } @@ -85,6 +113,10 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd throw new Error(t`Please select at least one key`); } + if (!allowConfirmationFingerprintChange && fingerprints.length > 1) { + setAllowConfirmationFingerprintChange(true); + } + const topic = await pair(uri, selectedFingerprints, mainnet); onClose(topic); } @@ -92,6 +124,7 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd function handleToggleSelectFingerprint(fingerprintLocal: number) { const { setValue } = methods; const { fingerprints } = methods.getValues(); + if (fingerprints.length === 1 && fingerprints[0] === fingerprintLocal) return; const index = fingerprints.indexOf(fingerprintLocal); if (index === -1) { setValue('fingerprints', [...fingerprints, fingerprintLocal]); @@ -107,6 +140,78 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd const isStepValid = step === Step.CONNECT || selectedFingerprints.length > 0; const canSubmit = !isSubmitting && !isLoading && isStepValid; + function renderKeysMultiSelect() { + return ( + + ); + } + + function renderSelectedAsPills() { + const keysWithIdxs = sortedKeysMemo?.map((key, index) => ({ ...key, idx: index })); + return keysWithIdxs + ?.filter((key: any) => selectedFingerprints.indexOf(key.fingerprint) > -1) + .map((key: any) => { + if (selectedFingerprints.length < 2) { + return ; + } + return ( + { + methods.setValue( + 'fingerprints', + selectedFingerprints.filter((f) => f !== key.fingerprint) + ); + }} + /> + ); + }); + } + + function renderMultipleKeySelectedWarning() { + const { fingerprints } = methods.getValues(); + if (!allowConfirmationFingerprintChange && fingerprints.length > 1) { + return ( + + Warning! Selecting multiple keys will enable "Key Switching" inside Settings / Integration tab. + + ); + } + return null; + } + return ( @@ -148,26 +253,12 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd ) : step === Step.CONNECT ? ( Paste link} multiline required autoFocus /> ) : ( - - {keys?.map((key, index) => ( - handleToggleSelectFingerprint(key.fingerprint)} - > - - - - {key.label || Wallet {index + 1}} - - - {key.fingerprint} - - - - - - ))} + + {renderMultipleKeySelectedWarning()} + + {renderSelectedAsPills()} + + {renderKeysMultiSelect()} )}