From 9d802d30781ca2b32fe4e8371a8e1a3dd6a95b54 Mon Sep 17 00:00:00 2001 From: Kirill Ageychenko Date: Fri, 23 Jun 2023 17:55:58 +0800 Subject: [PATCH] fix: vibration wrapper for qr modal (#1122) Co-authored-by: iGroza --- src/components/modals/qr/qr.tsx | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/components/modals/qr/qr.tsx b/src/components/modals/qr/qr.tsx index e935bef2f..95b1a72ba 100644 --- a/src/components/modals/qr/qr.tsx +++ b/src/components/modals/qr/qr.tsx @@ -29,11 +29,21 @@ export type QRModalProps = Modals['qr']; export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => { const [isOpen, setIsOpen] = useState(false); const visible = useWalletsVisible(); - + const [error, setError] = useState(false); + const [flashMode, setFlashMode] = useState(false); const closeDistance = useWindowDimensions().height / 6; const [code, setCode] = useState(''); const isProcessing = useRef(false); + const vibrateWrapper = useCallback( + (effect: HapticEffects) => { + if (!error) { + vibrate(effect); + } + }, + [error], + ); + const prepareAddress = useCallback((data: string) => { if (data.startsWith('haqq:')) { return data.slice(5); @@ -57,9 +67,6 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => { [prepareAddress, code], ); - const [error, setError] = useState(false); - const [flashMode, setFlashMode] = useState(false); - const checkAddress = useCallback( (address: string) => { if (utils.isAddress(address)) { @@ -70,7 +77,7 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => { from: visible[0].address.trim(), }); } else { - vibrate(HapticEffects.success); + vibrateWrapper(HapticEffects.success); setIsOpen(true); } } else if (parseUri(address)?.protocol === 'wc') { @@ -80,19 +87,19 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => { }, 1000); } else if (!error) { setError(true); - vibrate(HapticEffects.error); + vibrateWrapper(HapticEffects.error); setTimeout(() => { setError(false); }, 5000); } }, - [error, visible, onClose, prepareAddress], + [error, visible, onClose, prepareAddress, vibrateWrapper], ); const onGetAddress = useCallback( (slicedAddress: string) => { if (slicedAddress && qrWithoutFrom) { - vibrate(HapticEffects.success); + vibrateWrapper(HapticEffects.success); app.emit('address', { to: slicedAddress, }); @@ -100,7 +107,7 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => { checkAddress(slicedAddress); } }, - [checkAddress, qrWithoutFrom], + [checkAddress, qrWithoutFrom, vibrateWrapper], ); const onSuccess = useCallback( @@ -108,7 +115,7 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => { if (!isProcessing.current && e.data && e.data !== code) { isProcessing.current = true; try { - vibrate(HapticEffects.selection); + vibrateWrapper(HapticEffects.selection); setCode(e.data); const slicedAddress = prepareAddress(e.data); if (slicedAddress) { @@ -119,7 +126,7 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => { } } }, - [code, onGetAddress, prepareAddress], + [code, onGetAddress, prepareAddress, vibrateWrapper], ); const onClickGallery = useCallback(async () => { @@ -146,8 +153,8 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => { const onToggleFlashMode = useCallback(() => { setFlashMode(pr => !pr); - vibrate(HapticEffects.impactLight); - }, []); + vibrateWrapper(HapticEffects.impactLight); + }, [vibrateWrapper]); return ( <>