Skip to content

Commit

Permalink
fix: vibration wrapper for qr modal (#1122)
Browse files Browse the repository at this point in the history
Co-authored-by: iGroza <[email protected]>
  • Loading branch information
iGroza and iGroza authored Jun 23, 2023
1 parent e225815 commit 9d802d3
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/components/modals/qr/qr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)) {
Expand All @@ -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') {
Expand All @@ -80,35 +87,35 @@ 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,
});
} else if (slicedAddress) {
checkAddress(slicedAddress);
}
},
[checkAddress, qrWithoutFrom],
[checkAddress, qrWithoutFrom, vibrateWrapper],
);

const onSuccess = useCallback(
(e: BarCodeReadEvent) => {
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) {
Expand All @@ -119,7 +126,7 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => {
}
}
},
[code, onGetAddress, prepareAddress],
[code, onGetAddress, prepareAddress, vibrateWrapper],
);

const onClickGallery = useCallback(async () => {
Expand All @@ -146,8 +153,8 @@ export const QRModal = ({onClose = () => {}, qrWithoutFrom}: QRModalProps) => {

const onToggleFlashMode = useCallback(() => {
setFlashMode(pr => !pr);
vibrate(HapticEffects.impactLight);
}, []);
vibrateWrapper(HapticEffects.impactLight);
}, [vibrateWrapper]);

return (
<>
Expand Down

0 comments on commit 9d802d3

Please sign in to comment.