Fix SignArea and ToastProvider timeout (#1444)
* fix: correctly set and clean up signingArea alarms * fix: revert ToastProvider to use setTimeout * revert SignArea to use setTimeout
This commit is contained in:
parent
80744b94d6
commit
737e8b3e78
@ -1,8 +1,6 @@
|
|||||||
// Copyright 2019-2024 @polkadot/extension-ui authors & contributors
|
// Copyright 2019-2024 @polkadot/extension-ui authors & contributors
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
/* global chrome */
|
|
||||||
|
|
||||||
import React, { useCallback, useContext, useEffect, useState } from 'react';
|
import React, { useCallback, useContext, useEffect, useState } from 'react';
|
||||||
|
|
||||||
import { PASSWORD_EXPIRY_MIN } from '@polkadot/extension-base/defaults';
|
import { PASSWORD_EXPIRY_MIN } from '@polkadot/extension-base/defaults';
|
||||||
@ -32,46 +30,23 @@ function SignArea ({ buttonText, className, error, isExternal, isFirst, setError
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const lockSigner = async () => {
|
setIsLocked(null);
|
||||||
setIsLocked(null);
|
let timeout: ReturnType<typeof setTimeout>;
|
||||||
|
|
||||||
try {
|
|
||||||
const { isLocked, remainingTime } = await isSignLocked(signId);
|
|
||||||
|
|
||||||
|
!isExternal && isSignLocked(signId)
|
||||||
|
.then(({ isLocked, remainingTime }) => {
|
||||||
setIsLocked(isLocked);
|
setIsLocked(isLocked);
|
||||||
|
timeout = setTimeout(() => {
|
||||||
|
setIsLocked(true);
|
||||||
|
}, remainingTime);
|
||||||
|
|
||||||
await chrome.alarms.create('SIGNER_TIMEOUT', { delayInMinutes: remainingTime / 60000 });
|
!isLocked && setSavePass(true);
|
||||||
|
})
|
||||||
|
.catch((error: Error) => console.error(error));
|
||||||
|
|
||||||
if (!isLocked) {
|
return () => {
|
||||||
setSavePass(true);
|
!!timeout && clearTimeout(timeout);
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error locking signer:', error);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const resetAlarm = async () => {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
chrome.alarms.clear('SIGNER_TIMEOUT', (cleared) => {
|
|
||||||
if (chrome.runtime.lastError) {
|
|
||||||
reject(chrome.runtime.lastError);
|
|
||||||
} else {
|
|
||||||
resolve(cleared);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const executeLockSigner = async () => {
|
|
||||||
if (!isExternal) {
|
|
||||||
await lockSigner();
|
|
||||||
await resetAlarm();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
executeLockSigner().then(async () => {
|
|
||||||
return await executeLockSigner();
|
|
||||||
}).catch((error) => console.error('Error clearing the alarm: ', error));
|
|
||||||
}, [isExternal, signId]);
|
}, [isExternal, signId]);
|
||||||
|
|
||||||
const _onSign = useCallback(
|
const _onSign = useCallback(
|
||||||
|
|||||||
@ -1,9 +1,7 @@
|
|||||||
// Copyright 2019-2024 @polkadot/extension-ui authors & contributors
|
// Copyright 2019-2024 @polkadot/extension-ui authors & contributors
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
/* global chrome */
|
import React, { useCallback, useState } from 'react';
|
||||||
|
|
||||||
import React, { useCallback, useEffect, useState } from 'react';
|
|
||||||
|
|
||||||
import { ToastContext } from '../index.js';
|
import { ToastContext } from '../index.js';
|
||||||
import Toast from './Toast.js';
|
import Toast from './Toast.js';
|
||||||
@ -13,37 +11,18 @@ interface ToastProviderProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const TOAST_TIMEOUT = 1500;
|
const TOAST_TIMEOUT = 1500;
|
||||||
const ALARM_NAME = 'toastAlarm';
|
|
||||||
|
|
||||||
const ToastProvider = ({ children }: ToastProviderProps): React.ReactElement<ToastProviderProps> => {
|
const ToastProvider = ({ children }: ToastProviderProps): React.ReactElement<ToastProviderProps> => {
|
||||||
const [content, setContent] = useState('');
|
const [content, setContent] = useState('');
|
||||||
const [visible, setVisible] = useState(false);
|
const [visible, setVisible] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
const show = useCallback((message: string): () => void => {
|
||||||
const createAlarm = async () => {
|
const timerId = setTimeout(() => setVisible(false), TOAST_TIMEOUT);
|
||||||
try {
|
|
||||||
await chrome.alarms.create(ALARM_NAME, { delayInMinutes: TOAST_TIMEOUT / 60000 });
|
|
||||||
chrome.alarms.onAlarm.addListener((alarm) => {
|
|
||||||
if (alarm.name === ALARM_NAME) {
|
|
||||||
setVisible(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error creating the alarm:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
createAlarm().then(async () => {
|
|
||||||
return await chrome.alarms.clear(ALARM_NAME);
|
|
||||||
}).catch((error) => console.error('Error clearing the alarm: ', error));
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const show = useCallback((message: string): (() => void) => {
|
|
||||||
setContent(message);
|
setContent(message);
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
|
|
||||||
return (): void => setVisible(false);
|
return (): void => clearTimeout(timerId);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user