Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vibration wrapper for qr modal #1122

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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