Skip to content

Commit

Permalink
Added "quiet mode" option when creating a WalletConnect pairing
Browse files Browse the repository at this point in the history
  • Loading branch information
rifeljm committed Aug 28, 2023
1 parent 5b56604 commit 7fe7391
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useGetKeysQuery, useGetLoggedInFingerprintQuery, usePrefs } from '@chia-network/api-react';
import { useGetKeysQuery, useGetLoggedInFingerprintQuery, usePrefs, useLocalStorage } from '@chia-network/api-react';
import {
ButtonLoading,
DialogActions,
Expand Down Expand Up @@ -51,6 +51,7 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd
const { onClose = () => {}, open = false } = props;

const [step, setStep] = useState<Step>(Step.CONNECT);
const [bypassReadonlyCommands, toggleBypassReadonlyCommands] = useLocalStorage('bypass-readonly-commands', false);
const { pair, isLoading: isLoadingWallet } = useWalletConnectContext();
const { data: keys, isLoading: isLoadingPublicKeys } = useGetKeysQuery({});
const [sortedWallets] = usePrefs('sortedWallets', []);
Expand Down Expand Up @@ -212,6 +213,23 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd
return null;
}

function renderQuietModeOption() {
return (
<Flex
sx={{ cursor: 'pointer' }}
alignItems="center"
onClick={() => {
toggleBypassReadonlyCommands(!bypassReadonlyCommands);
}}
>
<Checkbox checked={bypassReadonlyCommands} disableRipple sx={{ paddingLeft: 0 }} />
<Typography variant="body2">
<Trans>Bypass confirmation for all read-only commands</Trans>
</Typography>
</Flex>
);
}

return (
<Dialog onClose={handleClose} maxWidth="xs" open={open} fullWidth>
<DialogTitle>
Expand Down Expand Up @@ -259,6 +277,7 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd
{renderSelectedAsPills()}
</Flex>
{renderKeysMultiSelect()}
{renderQuietModeOption()}
</Flex>
)}
</Flex>
Expand Down
47 changes: 28 additions & 19 deletions packages/gui/src/hooks/useWalletConnectCommand.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import api, { store, useGetLoggedInFingerprintQuery } from '@chia-network/api-react';
import api, { store, useGetLoggedInFingerprintQuery, useLocalStorage } from '@chia-network/api-react';
import { useOpenDialog, useAuth } from '@chia-network/core';
import { Trans } from '@lingui/macro';
import debug from 'debug';
Expand Down Expand Up @@ -80,6 +80,8 @@ export default function useWalletConnectCommand(options: UseWalletConnectCommand

const isLoading = isLoadingLoggedInFingerprint;

const [bypassReadonlyCommands] = useLocalStorage('bypass-readonly-commands', false);

async function confirm(props: {
topic: string;
message: ReactNode;
Expand Down Expand Up @@ -172,24 +174,31 @@ export default function useWalletConnectCommand(options: UseWalletConnectCommand
values = newValues;
}

const confirmed = await confirm({
topic,
message:
!allFingerprints && isDifferentFingerprint ? (
<Trans>
Do you want to log in to {fingerprint} and execute command {command}?
</Trans>
) : (
<Trans>Do you want to execute command {command}?</Trans>
),
params: definitionParams,
values,
fingerprint,
isDifferentFingerprint,
command,
bypassConfirm,
onChange: handleChangeParam,
});
const isReadOnly =
['spend', 'cancel', 'create', 'transfer', 'send', 'take', 'add', 'set'].filter(
(startsWith: string) => command.indexOf(startsWith) === 0
).length === 0;

const confirmed =
(bypassReadonlyCommands && isReadOnly) ||
(await confirm({
topic,
message:
!allFingerprints && isDifferentFingerprint ? (
<Trans>
Do you want to log in to {fingerprint} and execute command {command}?
</Trans>
) : (
<Trans>Do you want to execute command {command}?</Trans>
),
params: definitionParams,
values,
fingerprint,
isDifferentFingerprint,
command,
bypassConfirm,
onChange: handleChangeParam,
}));

if (!confirmed) {
throw new Error(`User cancelled command ${requestedCommand}`);
Expand Down

0 comments on commit 7fe7391

Please sign in to comment.