Skip to content

Commit

Permalink
use Modal.close to handle race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
rushatgabhane committed Aug 28, 2024
1 parent b40f16d commit 1646d07
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/components/AccountSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -38,7 +39,7 @@ function AccountSwitcher() {
const buttonRef = useRef<HTMLDivElement>(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;
Expand Down Expand Up @@ -73,17 +74,21 @@ 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;

return [
createBaseMenuItem(delegatePersonalDetails, error, {
onPress: () => {
if (isOffline) {
setShouldShowOfflineError(true);
Modal.close(() => setShouldShowOfflineModal(true));
return;
}
setShouldShowDelegatorMenu(false);
disconnect();
},
key: `${delegateEmail}-delegate`,
Expand All @@ -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];
};
Expand Down Expand Up @@ -180,9 +186,9 @@ function AccountSwitcher() {
)}
<ConfirmModal
title={translate('common.youAppearToBeOffline')}
isVisible={shouldShowOfflineError}
onConfirm={() => setShouldShowOfflineError(false)}
onCancel={() => setShouldShowOfflineError(false)}
isVisible={shouldShowOfflineModal}
onConfirm={() => setShouldShowOfflineModal(false)}
onCancel={() => setShouldShowOfflineModal(false)}
confirmText={translate('common.buttonConfirm')}
prompt={translate('common.offlinePrompt')}
shouldShowCancelButton={false}
Expand Down

0 comments on commit 1646d07

Please sign in to comment.