Skip to content

Commit

Permalink
Fix re-sent message flicker
Browse files Browse the repository at this point in the history
  • Loading branch information
wildan-m committed Sep 5, 2024
1 parent 5294c3a commit 158274b
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ function BaseValidateCodeForm({
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing -- nullish coalescing doesn't achieve the same result in this case
const shouldDisableResendValidateCode = !!isOffline || account?.isLoading;
const focusTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const [validateCodeSentIsPressed, setValidateCodeSentIsPressed] = useState(false);
const validateCodeSentIsPressedRef = useRef(false);
const [showDotIndicator, setShowDotIndicator] = useState(false);

useImperativeHandle(innerRef, () => ({
focus() {
Expand Down Expand Up @@ -144,6 +145,18 @@ function BaseValidateCodeForm({
inputValidateCodeRef.current?.clear();
}, [hasMagicCodeBeenSent]);

useEffect(() => {
// `validateCodeSent` is updated asynchronously,
// and `validateCodeSentIsPressedRef.current` is updated faster than `hasMagicCodeBeenSent`.
// This can cause the component to hide and show the `DotIndicatorMessage` multiple times
// in quick succession, leading to a flickering effect.
if ((hasMagicCodeBeenSent ?? !!pendingContact?.validateCodeSent) && validateCodeSentIsPressedRef.current) {
setShowDotIndicator(true);
} else {
setShowDotIndicator(false);
}
}, [hasMagicCodeBeenSent, pendingContact, validateCodeSentIsPressedRef]);

/**
* Request a validate code / magic code be sent to verify this contact method
*/
Expand All @@ -155,9 +168,8 @@ function BaseValidateCodeForm({
}

inputValidateCodeRef.current?.clear();
setValidateCodeSentIsPressed(true);
validateCodeSentIsPressedRef.current = true;
};

/**
* Handle text input and clear formError upon text change
*/
Expand Down Expand Up @@ -229,7 +241,7 @@ function BaseValidateCodeForm({
>
<Text style={[StyleUtils.getDisabledLinkStyles(shouldDisableResendValidateCode)]}>{translate('validateCodeForm.magicCodeNotReceived')}</Text>
</PressableWithFeedback>
{(hasMagicCodeBeenSent ?? !!pendingContact?.validateCodeSent) && validateCodeSentIsPressed && (
{showDotIndicator && (
<DotIndicatorMessage
type="success"
style={[styles.mt6, styles.flex0]}
Expand Down

0 comments on commit 158274b

Please sign in to comment.