From 1646d07c8401bed8ae71a413c36a3f1002fa3f53 Mon Sep 17 00:00:00 2001 From: Rushat Gabhane Date: Wed, 28 Aug 2024 15:16:48 +0300 Subject: [PATCH] use Modal.close to handle race condition --- src/components/AccountSwitcher.tsx | 46 +++++++++++++++++------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/components/AccountSwitcher.tsx b/src/components/AccountSwitcher.tsx index a60e7135ba56..ba30ea0062b9 100644 --- a/src/components/AccountSwitcher.tsx +++ b/src/components/AccountSwitcher.tsx @@ -11,6 +11,7 @@ import useThemeStyles from '@hooks/useThemeStyles'; import {clearDelegatorErrors, connect, disconnect} from '@libs/actions/Delegate'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import variables from '@styles/variables'; +import * as Modal from '@userActions/Modal'; import CONST from '@src/CONST'; import type {TranslationPaths} from '@src/languages/types'; import ONYXKEYS from '@src/ONYXKEYS'; @@ -38,7 +39,7 @@ function AccountSwitcher() { const buttonRef = useRef(null); const [shouldShowDelegatorMenu, setShouldShowDelegatorMenu] = useState(false); - const [shouldShowOfflineError, setShouldShowOfflineError] = useState(false); + const [shouldShowOfflineModal, setShouldShowOfflineModal] = useState(false); const delegators = account?.delegatedAccess?.delegators ?? []; const isActingAsDelegate = !!account?.delegatedAccess?.delegate ?? false; @@ -73,6 +74,11 @@ function AccountSwitcher() { if (isActingAsDelegate) { const delegateEmail = account?.delegatedAccess?.delegate ?? ''; + // Avoid duplicating the current user in the list when switching accounts + if (delegateEmail === currentUserPersonalDetails.login) { + return [currentUserMenuItem]; + } + const delegatePersonalDetails = PersonalDetailsUtils.getPersonalDetailByEmail(delegateEmail); const error = account?.delegatedAccess?.error; @@ -80,10 +86,9 @@ function AccountSwitcher() { createBaseMenuItem(delegatePersonalDetails, error, { onPress: () => { if (isOffline) { - setShouldShowOfflineError(true); + Modal.close(() => setShouldShowOfflineModal(true)); return; } - setShouldShowDelegatorMenu(false); disconnect(); }, key: `${delegateEmail}-delegate`, @@ -92,21 +97,22 @@ function AccountSwitcher() { ]; } - const delegatorMenuItems: MenuItemProps[] = delegators.map(({email, role, error}, index) => { - const personalDetails = PersonalDetailsUtils.getPersonalDetailByEmail(email); - return createBaseMenuItem(personalDetails, error, { - badgeText: translate('delegate.role', role), - onPress: () => { - if (isOffline) { - setShouldShowOfflineError(true); - return; - } - setShouldShowDelegatorMenu(false); - connect(email); - }, - key: `${email}-${index}`, + const delegatorMenuItems: MenuItemProps[] = delegators + .filter(({email}) => email !== currentUserPersonalDetails.login) + .map(({email, role, error}, index) => { + const personalDetails = PersonalDetailsUtils.getPersonalDetailByEmail(email); + return createBaseMenuItem(personalDetails, error, { + badgeText: translate('delegate.role', role), + onPress: () => { + if (isOffline) { + Modal.close(() => setShouldShowOfflineModal(true)); + return; + } + connect(email); + }, + key: `${email}-${index}`, + }); }); - }); return [currentUserMenuItem, ...delegatorMenuItems]; }; @@ -180,9 +186,9 @@ function AccountSwitcher() { )} setShouldShowOfflineError(false)} - onCancel={() => setShouldShowOfflineError(false)} + isVisible={shouldShowOfflineModal} + onConfirm={() => setShouldShowOfflineModal(false)} + onCancel={() => setShouldShowOfflineModal(false)} confirmText={translate('common.buttonConfirm')} prompt={translate('common.offlinePrompt')} shouldShowCancelButton={false}