Skip to content

Commit

Permalink
"Skip Confirmation For All Readonly Commands" into Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
rifeljm committed Aug 31, 2023
1 parent 7fe7391 commit 7e8243e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
32 changes: 32 additions & 0 deletions packages/gui/src/components/settings/SettingsIntegration.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useLocalStorage, useGetLoggedInFingerprintQuery } from '@chia-network/api-react';
import { Flex, MenuItem, SettingsHR, SettingsSection, SettingsTitle, SettingsText } from '@chia-network/core';
import { t, Trans } from '@lingui/macro';
import {
Expand Down Expand Up @@ -45,6 +46,9 @@ export default function SettingsIntegration() {
useWalletConnectPairs();

const pairs = get();
const { data: fingerprint } = useGetLoggedInFingerprintQuery();

const [bypassReadonlyCommands, setBypassReadonlyCommands] = useLocalStorage<any>('bypass-readonly-commands', {});

const refreshBypassCommands = React.useCallback(() => {
if (selectedPair.current) {
Expand Down Expand Up @@ -131,6 +135,9 @@ export default function SettingsIntegration() {
}
}, [topic, pairs, refreshBypassCommands]);

const readonlySkipValue =
bypassReadonlyCommands && topic && fingerprint ? bypassReadonlyCommands[topic][fingerprint] : false;

return (
<Grid container style={{ maxWidth: '624px' }} gap={3}>
<Grid item>
Expand Down Expand Up @@ -334,6 +341,31 @@ export default function SettingsIntegration() {
</Box>
)}
</Flex>
<Box {...borderStyle}>
<Typography variant="h6">
<Trans>Skip Confirmation For All Readonly Commands</Trans>
<Grid item container xs marginTop="20px" marginRight="8px">
<FormControl size="small">
<Select
value={readonlySkipValue}
onChange={(evt: PointerEvent) => {
setBypassReadonlyCommands((localBypassReadonlyCommands) => ({
...localBypassReadonlyCommands,
[topic]: { [fingerprint]: evt.target.value },
}));
}}
>
<MenuItem value>
<Trans>Always Skip</Trans>
</MenuItem>
<MenuItem value={false} onClick={() => {}}>
<Trans>Require Confirmation</Trans>
</MenuItem>
</Select>
</FormControl>
</Grid>
</Typography>
</Box>

{topic && commands.length > 0 && (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ 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 [bypassReadonlyCommands, setBypassReadonlyCommands] = useLocalStorage<any>('bypass-readonly-commands', {});
const [bypassCheckbox, toggleBypassCheckbox] = useState(false);
const { pair, isLoading: isLoadingWallet } = useWalletConnectContext();
const { data: keys, isLoading: isLoadingPublicKeys } = useGetKeysQuery({});
const [sortedWallets] = usePrefs('sortedWallets', []);
Expand Down Expand Up @@ -119,6 +120,13 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd
}

const topic = await pair(uri, selectedFingerprints, mainnet);
if (bypassCheckbox) {
let skipReadOnlyObject = {};
selectedFingerprints.forEach((f: number) => {
skipReadOnlyObject = { ...skipReadOnlyObject, [f]: true };
});
setBypassReadonlyCommands({ ...bypassReadonlyCommands, [topic.toString()]: skipReadOnlyObject });
}
onClose(topic);
}

Expand Down Expand Up @@ -219,10 +227,10 @@ export default function WalletConnectAddConnectionDialog(props: WalletConnectAdd
sx={{ cursor: 'pointer' }}
alignItems="center"
onClick={() => {
toggleBypassReadonlyCommands(!bypassReadonlyCommands);
toggleBypassCheckbox(!bypassCheckbox);
}}
>
<Checkbox checked={bypassReadonlyCommands} disableRipple sx={{ paddingLeft: 0 }} />
<Checkbox checked={bypassCheckbox} disableRipple sx={{ paddingLeft: 0 }} />
<Typography variant="body2">
<Trans>Bypass confirmation for all read-only commands</Trans>
</Typography>
Expand Down
12 changes: 5 additions & 7 deletions packages/gui/src/hooks/useWalletConnectCommand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default function useWalletConnectCommand(options: UseWalletConnectCommand

const isLoading = isLoadingLoggedInFingerprint;

const [bypassReadonlyCommands] = useLocalStorage('bypass-readonly-commands', false);
const [bypassReadonlyCommands] = useLocalStorage<any>('bypass-readonly-commands', {});

async function confirm(props: {
topic: string;
Expand Down Expand Up @@ -141,8 +141,9 @@ export default function useWalletConnectCommand(options: UseWalletConnectCommand

const { fingerprint } = requestedParams;

const pair = getPairBySession(topic);
const pairedTopic = pair?.topic!;
if (command === 'showNotification') {
const pair = getPairBySession(topic);
if (!pair) {
throw new Error('Invalid session topic');
}
Expand Down Expand Up @@ -174,13 +175,10 @@ export default function useWalletConnectCommand(options: UseWalletConnectCommand
values = newValues;
}

const isReadOnly =
['spend', 'cancel', 'create', 'transfer', 'send', 'take', 'add', 'set'].filter(
(startsWith: string) => command.indexOf(startsWith) === 0
).length === 0;
const isReadOnlyEnabled = bypassReadonlyCommands?.[pairedTopic]?.[fingerprint];

const confirmed =
(bypassReadonlyCommands && isReadOnly) ||
(isReadOnlyEnabled && definition.bypassConfirm) ||
(await confirm({
topic,
message:
Expand Down

0 comments on commit 7e8243e

Please sign in to comment.