From 7590be35ec2f2f1226d280e3531cde5ba317444a Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 2 Apr 2025 22:54:43 +0800 Subject: [PATCH] fix(mobile): OTPWindow component and modal handling - Updated OTPWindow to include an onDismiss prop for better modal management. - Improved animation handling using Animated API for smoother transitions. - Refactored state management to control UI visibility and component rendering more effectively. - Integrated FullWindowOverlay for a consistent modal presentation. These changes enhance the user experience during two-factor authentication by providing a more responsive and visually appealing modal interface. Signed-off-by: Innei --- .../modules/settings/compoents/OTPWindow.tsx | 89 ++++++++++++++----- .../settings/hooks/useTOTPModalWrapper.tsx | 3 + 2 files changed, 71 insertions(+), 21 deletions(-) diff --git a/apps/mobile/src/modules/settings/compoents/OTPWindow.tsx b/apps/mobile/src/modules/settings/compoents/OTPWindow.tsx index b7e38be34..4da73d162 100644 --- a/apps/mobile/src/modules/settings/compoents/OTPWindow.tsx +++ b/apps/mobile/src/modules/settings/compoents/OTPWindow.tsx @@ -1,12 +1,21 @@ import { useMutation } from "@tanstack/react-query" import { useEffect, useRef, useState } from "react" -import { Keyboard, Modal, StyleSheet, Text, useWindowDimensions, View } from "react-native" +import { + Animated, + Keyboard, + StyleSheet, + Text, + useAnimatedValue, + useWindowDimensions, + View, +} from "react-native" import type { OtpInputRef } from "react-native-otp-entry" import { OtpInput } from "react-native-otp-entry" -import Animated, { FadeIn, FadeOut, useSharedValue, withSpring } from "react-native-reanimated" import { useSafeAreaInsets } from "react-native-safe-area-context" import { useColor } from "react-native-uikit-colors" +import { useEventCallback } from "usehooks-ts" +import { FullWindowOverlay } from "@/src/components/common/FullWindowOverlay" import { isAuthCodeValid } from "@/src/lib/auth" import { toast } from "@/src/lib/toast" import { accentColor } from "@/src/theme/colors" @@ -14,9 +23,10 @@ import { accentColor } from "@/src/theme/colors" type OTPWindowProps = { onSuccess: (data: T) => void verifyFn: (code: string) => Promise + onDismiss: () => void } -export const OTPWindow = ({ onSuccess, verifyFn }: OTPWindowProps) => { +export const OTPWindow = ({ onSuccess, verifyFn, onDismiss }: OTPWindowProps) => { const otpInputRef = useRef(null) const label = useColor("label") const tertiaryLabel = useColor("tertiaryLabel") @@ -31,30 +41,58 @@ export const OTPWindow = ({ onSuccess, verifyFn }: OTPWindowProps) => { onSuccess(data) }, onSettled() { - setOpen(false) + setComponentRender(false) }, mutationFn: ({ code }: { code: string }) => verifyFn(code), }) - const windowScaleValue = useSharedValue(0.9) - const windowOpacityValue = useSharedValue(0) + const windowScale = useAnimatedValue(1.1) + const windowOpacity = useAnimatedValue(0) - const [open, setOpen] = useState(false) + const [uiShow, setUiShow] = useState(false) + const [componentRender, setComponentRender] = useState(true) useEffect(() => { - setOpen(true) + setUiShow(true) }, []) + const stableDismiss = useEventCallback(() => { + onDismiss() + }) useEffect(() => { - const preset = { stiffness: 100, damping: 10 } - if (open) { - windowScaleValue.value = withSpring(1, preset) - windowOpacityValue.value = withSpring(1, preset) + let timer: any + + if (uiShow) { + Animated.parallel([ + Animated.spring(windowScale, { + toValue: 1, + damping: 80, + stiffness: 500, + mass: 1, + velocity: 10, + useNativeDriver: true, + }), + Animated.timing(windowOpacity, { + toValue: 1, + duration: 200, + useNativeDriver: true, + }), + ]).start() } else { - windowOpacityValue.value = withSpring(0, preset) - windowScaleValue.value = withSpring(0.9, preset) + Animated.timing(windowOpacity, { + toValue: 0, + duration: 500, + useNativeDriver: true, + }).start() + + timer = setTimeout(() => { + setComponentRender(false) + stableDismiss() + }, 500) } - }, [open, windowOpacityValue, windowScaleValue]) + + return () => clearTimeout(timer) + }, [stableDismiss, uiShow, windowOpacity, windowScale]) const { height } = useWindowDimensions() @@ -75,9 +113,18 @@ export const OTPWindow = ({ onSuccess, verifyFn }: OTPWindowProps) => { return () => sub.forEach((listener) => listener.remove()) }, [height, insets.top]) + if (!componentRender) return null + return ( - setOpen(false)}> - + + @@ -89,8 +136,8 @@ export const OTPWindow = ({ onSuccess, verifyFn }: OTPWindowProps) => { style={[ styles.window, { - transform: [{ scale: windowScaleValue }], - opacity: windowOpacityValue, + transform: [{ scale: windowScale }], + opacity: windowOpacity, }, ]} > @@ -148,7 +195,7 @@ export const OTPWindow = ({ onSuccess, verifyFn }: OTPWindowProps) => { { - setOpen(false) + setUiShow(false) }} suppressHighlighting > @@ -159,7 +206,7 @@ export const OTPWindow = ({ onSuccess, verifyFn }: OTPWindowProps) => { - + ) } diff --git a/apps/mobile/src/modules/settings/hooks/useTOTPModalWrapper.tsx b/apps/mobile/src/modules/settings/hooks/useTOTPModalWrapper.tsx index b0a3c79b2..6af3e190f 100644 --- a/apps/mobile/src/modules/settings/hooks/useTOTPModalWrapper.tsx +++ b/apps/mobile/src/modules/settings/hooks/useTOTPModalWrapper.tsx @@ -38,6 +38,9 @@ export const useTOTPModalWrapper = ( root.destroy() }} + onDismiss={() => { + root.destroy() + }} onSuccess={async () => { root.destroy() }}