Skip to content

Commit

Permalink
Merge pull request #103 from spacemeshos/fix-79-stuck-unlocking-btn
Browse files Browse the repository at this point in the history
Prevent "Unlock" button being stuck in some cases
  • Loading branch information
brusherru authored Oct 14, 2024
2 parents 331f227 + 0a5af5c commit ae2cb3d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/components/VerifyMessageModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {

import { useVerifyMessage } from '../hooks/useSigning';
import { isSignedMessage } from '../types/message';
import { SIGNED_MESSAGE_PREFIX } from '../utils/constants';

type VerifyMessageModalProps = {
isOpen: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useConfirmation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const usePassword = (): UseConfirmationReturnType => {
const res = await callback();
_onClose();
eventEmitter.emit('success', res);
}, 1);
});

const onClose = () => {
eventEmitter.emit('close', true);
Expand Down
22 changes: 13 additions & 9 deletions src/screens/UnlockScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import PasswordInput from '../components/PasswordInput';
import Logo from '../components/welcome/Logo';
import WipeOutAlert from '../components/WipeOutAlert';
import useWallet from '../store/useWallet';
import { postpone } from '../utils/promises';

type FormValues = {
password: string;
Expand All @@ -37,15 +38,18 @@ function UnlockScreen(): JSX.Element {

const submit = handleSubmit(async (data) => {
setIsLoading(true);
const success = await unlockWallet(data.password);
if (!success) {
setError('password', { type: 'value', message: 'Invalid password' });
return;
}
setValue('password', '');
reset();
setIsLoading(false);
navigate('/wallet');
await postpone(async () => {
const success = await unlockWallet(data.password);
if (!success) {
setError('password', { type: 'value', message: 'Invalid password' });
setIsLoading(false);
return;
}
setValue('password', '');
reset();
setIsLoading(false);
navigate('/wallet');
});
});

const wipeAlert = useDisclosure();
Expand Down
3 changes: 1 addition & 2 deletions src/screens/welcome/ImportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ function ImportScreen(): JSX.Element {
const success = await postpone(
// We need to postpone it for one tick
// to allow component to re-render
() => openWallet(walletFileContent, password),
1
() => openWallet(walletFileContent, password)
);
setIsLoading(false);
if (!success) {
Expand Down
2 changes: 1 addition & 1 deletion src/store/usePassword.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const usePassword = (): UsePasswordReturnType => {
} catch (err) {
setError('password', { message: 'Incorrect password' });
}
}, 1);
});
});

const onClose = () => {
Expand Down
13 changes: 9 additions & 4 deletions src/utils/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ export const delay = (ms: number) =>
setTimeout(resolve, ms);
});

export const postpone = <T>(fn: () => T, ms: number): Promise<T> =>
new Promise((resolve) => {
setTimeout(async () => {
resolve(await fn());
export const postpone = <T>(fn: () => Promise<T> | T, ms = 1): Promise<T> =>
new Promise((resolve, reject) => {
setTimeout(() => {
const r = fn();
if (r instanceof Promise) {
r.then(resolve).catch(reject);
return;
}
resolve(r);
}, ms);
});

0 comments on commit ae2cb3d

Please sign in to comment.