Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/components/WorkspaceMembersSelectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import useInitialSelection from '@hooks/useInitialSelection';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import {usePersonalDetailsByLogins} from '@hooks/usePersonalDetailByLogin';
import usePolicy from '@hooks/usePolicy';
import useScreenWrapperTransitionStatus from '@hooks/useScreenWrapperTransitionStatus';

Expand Down Expand Up @@ -56,11 +57,12 @@ function WorkspaceMembersSelectionList({policyID, selectedApprover, setApprover}
const initialSelectedApprovers = initialSelectedApprover ? [initialSelectedApprover] : [];
const policyOwner = policy?.owner;
const policyEmployeeList = policy?.employeeList;
const employeePersonalDetails = usePersonalDetailsByLogins(Object.keys(policyEmployeeList ?? {}));

const approvers: SelectionListApprover[] = [];

if (policyEmployeeList) {
const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(policyEmployeeList);
const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(policyEmployeeList, employeePersonalDetails);

for (const employee of Object.values(policyEmployeeList)) {
const email = employee.email;
Expand Down
14 changes: 10 additions & 4 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {LocaleContextProps, LocalizedTranslate} from '@components/LocaleContextProvider';
import type {PersonalDetailsByLogin} from '@components/PersonalDetailsByLoginProvider';
import type {SelectorType} from '@components/SelectionScreen';

import CONST from '@src/CONST';
Expand Down Expand Up @@ -816,7 +817,7 @@ function shouldFilterExpensifyTeam(policyOwner: string | undefined, currentUserL
function createFilteredMemberCountSelector(employeeList: PolicyEmployeeList | undefined, policyOwner: string | undefined, currentUserLogin: string | undefined) {
return (personalDetails: PersonalDetailsList | undefined): number => {
const shouldFilter = shouldFilterExpensifyTeam(policyOwner, currentUserLogin);
const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(employeeList, false, false);
const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(employeeList, undefined, false, false);

return Object.keys(policyMemberEmailsToAccountIDs).reduce((count, email) => {
const accountID = policyMemberEmailsToAccountIDs[email];
Expand Down Expand Up @@ -875,7 +876,12 @@ const isPolicyOwner = (policy: OnyxInputOrEntry<Policy>, currentUserAccountID: n
*
* If includeMemberWithErrors is false, We only return members without errors. Otherwise, the members with errors would immediately be removed before the user has a chance to read the error.
*/
function getMemberAccountIDsForWorkspace(employeeList: PolicyEmployeeList | undefined, includeMemberWithErrors = false, includeMemberWithPendingDelete = true): MemberEmailsToAccountIDs {
function getMemberAccountIDsForWorkspace(
employeeList: PolicyEmployeeList | undefined,
personalDetailsByLogins?: PersonalDetailsByLogin,
includeMemberWithErrors = false,
includeMemberWithPendingDelete = true,
): MemberEmailsToAccountIDs {
const members = employeeList ?? {};
const memberEmailsToAccountIDs: MemberEmailsToAccountIDs = {};
for (const email of Object.keys(members)) {
Expand All @@ -891,7 +897,7 @@ function getMemberAccountIDsForWorkspace(employeeList: PolicyEmployeeList | unde
continue;
}
}
const personalDetail = getPersonalDetailByEmail(email);
const personalDetail = personalDetailsByLogins?.[email] ?? getPersonalDetailByEmail(email);
if (!personalDetail?.login) {
continue;
}
Expand Down Expand Up @@ -921,7 +927,7 @@ function getAccountIDForSubmitManagerEmail(managerEmail: string | undefined, emp
}

const normalizedEmail = trimmed.toLowerCase();
const memberAccountIDs = getMemberAccountIDsForWorkspace(employeeList, true, false);
const memberAccountIDs = getMemberAccountIDsForWorkspace(employeeList, undefined, true, false);

for (const [email, accountID] of Object.entries(memberAccountIDs)) {
if (email.toLowerCase() === normalizedEmail) {
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/IOU/TrackExpense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2404,7 +2404,7 @@ function shareTrackedExpense(trackedExpenseParams: TrackedExpenseParams) {

const policyEmployeeList = policyParams?.policy?.employeeList;
if (policyParams.policy && !policyEmployeeList?.[accountantEmail]) {
const policyMemberAccountIDs = Object.values(getMemberAccountIDsForWorkspace(policyEmployeeList, false, false));
const policyMemberAccountIDs = Object.values(getMemberAccountIDsForWorkspace(policyEmployeeList, undefined, false, false));
const {
optimisticData: addAccountantToWorkspaceOptimisticData,
successData: addAccountantToWorkspaceSuccessData,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3611,7 +3611,7 @@ function buildDuplicatePolicyData(policy: Policy, options: DuplicatePolicyDataOp

const outputCurrency = isOverviewOptionSelected && policy?.outputCurrency ? policy?.outputCurrency : localCurrency;

const policyMemberAccountIDs = isMemberOptionSelected ? Object.values(getMemberAccountIDsForWorkspace(policy?.employeeList, false, false)) : [];
const policyMemberAccountIDs = isMemberOptionSelected ? Object.values(getMemberAccountIDsForWorkspace(policy?.employeeList, undefined, false, false)) : [];
const {customUnitID: distanceCustomUnitID, customUnitRateID} = buildOptimisticDistanceRateCustomUnits(outputCurrency);
const perDiemCustomUnitID = generateCustomUnitID();

Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/Report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8595,7 +8595,7 @@ function changeReportPolicyAndInviteSubmitter({
}

const {accountID: currentUserAccountID, email: currentUserEmail = ''} = currentUser;
const policyMemberAccountIDs = Object.values(getMemberAccountIDsForWorkspace(employeeList, false, false));
const policyMemberAccountIDs = Object.values(getMemberAccountIDsForWorkspace(employeeList, undefined, false, false));
const {
optimisticData: optimisticAddMembersData,
successData: successAddMembersData,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/VacationDelegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ function inviteVacationDelegateToWorkspaces({delegate, policies, inviter, transl
isLastInvite ? personalDetailsOnyxData : {optimisticData: personalDetailsOnyxData.optimisticData},
`${translate('workspace.common.invitedYouToWorkspace', inviter.displayName ?? '', policy.name)}\n\n${translate('workspace.common.welcomeNote')}`,
policy,
Object.values(getMemberAccountIDsForWorkspace(policy.employeeList, false, false)),
Object.values(getMemberAccountIDsForWorkspace(policy.employeeList, undefined, false, false)),
CONST.POLICY.ROLE.USER,
inviter,
policyExpenseChatReportActions,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/ReportAddApproverPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function ReportAddApproverPage({report, isLoadingReportData, policy}: ReportAddA
return [];
}

const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(employeeList, true, false);
const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(employeeList, undefined, true, false);
return Object.values(employeeList)
.map((employee): SelectionListApprover | null => {
const isAdmin = employee?.role === CONST.REPORT.ROLE.ADMIN;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/ReportSubmitToContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function ReportSubmitToContent({
return [];
}
const prepopulatedEmailLower = prepopulatedEmail?.trim().toLowerCase();
const emailsToAccountIDs = getMemberAccountIDsForWorkspace(employeeList, true, false);
const emailsToAccountIDs = getMemberAccountIDsForWorkspace(employeeList, undefined, true, false);
return Object.values(employeeList).flatMap((employee): WorkspaceMemberItem[] => {
const email = employee.email?.trim();
if (!email || employee.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Search/SearchAddApproverPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function SearchAddApproverPage() {
const employeeLists = uniquePolicyIds.map((policyID) => allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.employeeList).filter((employeeList) => !!employeeList);
const firstWorkspaceEmployees = employeeLists.at(0);
const intersectedEmployees = firstWorkspaceEmployees ? lodashPick(firstWorkspaceEmployees, lodashIntersection(...employeeLists.map(Object.keys))) : {};
const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(intersectedEmployees, true, false);
const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(intersectedEmployees, undefined, true, false);
// We get the intersection here as we only want to show members who belong to all workspaces when adding an additional approver
return Object.values(intersectedEmployees)
.map((employee): SelectionListApprover | null => {
Expand Down
4 changes: 3 additions & 1 deletion src/pages/workspace/DynamicWorkspaceInvitePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import useDynamicBackPath from '@hooks/useDynamicBackPath';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import {usePersonalDetailsByLogins} from '@hooks/usePersonalDetailByLogin';
import {useAllPersonalDetails} from '@hooks/usePersonalDetails';
import usePersonalDetailSearchSelector from '@hooks/usePersonalDetailSearchSelector';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -64,8 +65,9 @@ function DynamicWorkspaceInvitePageContent({route, policy, invitedEmailsToAccoun
const [countryCode = CONST.DEFAULT_COUNTRY_CODE] = useOnyx(ONYXKEYS.COUNTRY_CODE);
const [personalDetails] = useAllPersonalDetails();
const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const employeePersonalDetails = usePersonalDetailsByLogins(Object.keys(policy?.employeeList ?? {}));
const openWorkspaceInvitePage = () => {
const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(policy?.employeeList);
const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(policy?.employeeList, employeePersonalDetails);
policyOpenWorkspaceInvitePage(route.params.policyID, Object.keys(policyMemberEmailsToAccountIDs));
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ApproverSelectionList from '@components/ApproverSelectionList';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import {usePersonalDetailsByLogins} from '@hooks/usePersonalDetailByLogin';

import {setWorkspaceInviteApproverDraft} from '@libs/actions/Policy/Member';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -50,7 +51,8 @@ function WorkspaceInviteMessageApproverPage({policy, personalDetails, isLoadingR
Navigation.goBack(backTo);
};

const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(employeeList);
const employeePersonalDetails = usePersonalDetailsByLogins(Object.keys(employeeList ?? {}));
const policyMemberEmailsToAccountIDs = getMemberAccountIDsForWorkspace(employeeList, employeePersonalDetails);

const allApprovers: SelectionListApprover[] = (() => {
if (!employeeList) {
Expand Down
37 changes: 15 additions & 22 deletions src/pages/workspace/WorkspaceMembersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import useLocalize from '@hooks/useLocalize';
import useMobileSelectionMode from '@hooks/useMobileSelectionMode';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import {usePersonalDetailsByLogins} from '@hooks/usePersonalDetailByLogin';
import usePrevious from '@hooks/usePrevious';
import usePrivateIsArchivedMap from '@hooks/usePrivateIsArchivedMap';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
Expand Down Expand Up @@ -90,7 +91,7 @@ import type {ValueOf} from 'type-fest';

import {useIsFocused} from '@react-navigation/native';
import {createOutstandingReportsForPolicySelector} from '@selectors/Report';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import React, {useCallback, useEffect, useEffectEvent, useMemo, useRef, useState} from 'react';
import {View} from 'react-native';

import type {WithPolicyAndFullscreenLoadingProps} from './withPolicyAndFullscreenLoading';
Expand All @@ -112,13 +113,20 @@ function WorkspaceMembersPage({personalDetails, route, policy}: WorkspaceMembers
useWorkspaceDocumentTitle(policy?.name, 'common.members');
const tableRef = useRef<TableHandle<WorkspaceMemberRowData, WorkspaceMembersTableColumnKey, string>>(null);
const icons = useMemoizedLazyExpensifyIcons(['Download', 'FallbackAvatar', 'MakeAdmin', 'Plus', 'RemoveMembers', 'Sync', 'Table', 'User', 'UserEye']);
const policyMemberEmailsToAccountIDs = useMemo(() => getMemberAccountIDsForWorkspace(policy?.employeeList, true), [policy?.employeeList]);
const employeePersonalDetails = usePersonalDetailsByLogins(Object.keys(policy?.employeeList ?? {}));
const policyMemberEmailsToAccountIDs = useMemo(
() => getMemberAccountIDsForWorkspace(policy?.employeeList, employeePersonalDetails, true),
[policy?.employeeList, employeePersonalDetails],
);
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const styles = useThemeStyles();
const {showConfirmModal} = useConfirmModal();
const showRuleBotGuardModal = useRuleBotGuardModal();
const {isOffline} = useNetwork();
const prevIsOffline = usePrevious(isOffline);
const getWorkspaceMembers = () => {
const clientMemberEmails = Object.keys(getMemberAccountIDsForWorkspace(policy?.employeeList, employeePersonalDetails));
openWorkspaceMembersPage(route.params.policyID, clientMemberEmails);
};
const {isOffline} = useNetwork({onReconnect: getWorkspaceMembers});
const [isDownloadFailureModalVisible, setIsDownloadFailureModalVisible] = useState(false);
const isOfflineAndNoMemberDataAvailable = isEmptyObject(policy?.employeeList) && isOffline;
const {translate, formatPhoneNumber, localeCompare} = useLocalize();
Expand Down Expand Up @@ -201,25 +209,10 @@ function WorkspaceMembersPage({personalDetails, route, policy}: WorkspaceMembers
});
}, [selectedEmployees, policyMemberEmailsToAccountIDs, translate, policy, formatPhoneNumber, personalDetails, outstandingReportsForPolicy, privateIsArchivedMap]);

/**
* Get members for the current workspace
*/
const getWorkspaceMembers = useCallback(() => {
const clientMemberEmails = Object.keys(getMemberAccountIDsForWorkspace(policy?.employeeList));
openWorkspaceMembersPage(route.params.policyID, clientMemberEmails);
}, [route.params.policyID, policy?.employeeList]);

useEffect(() => {
getWorkspaceMembers();
}, [getWorkspaceMembers]);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added in #61169 to trigger the API once when the page opens, but it's unintentionally fired every time the employeeList changes.


const getWorkspaceMembersEvent = useEffectEvent(() => getWorkspaceMembers());
useEffect(() => {
const isReconnecting = prevIsOffline && !isOffline;
if (!isReconnecting) {
return;
}
getWorkspaceMembers();
}, [isOffline, prevIsOffline, getWorkspaceMembers]);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this to useNetwork onReconnect

getWorkspaceMembersEvent();
}, []);

/**
* Open the modal to invite a user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import TextInput from '@components/TextInput';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePersonalDetailByLogin from '@hooks/usePersonalDetailByLogin';
import usePersonalDetailByLogin, {usePersonalDetailsByLogins} from '@hooks/usePersonalDetailByLogin';
import {useAllPersonalDetails} from '@hooks/usePersonalDetails';
import useThemeStyles from '@hooks/useThemeStyles';

Expand Down Expand Up @@ -116,6 +116,7 @@ function WorkspaceInviteMessageComponent({
const [approverDraft] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_INVITE_APPROVER_DRAFT}${policyID}`);
const workspaceInviteApproverDraft = approverDraft ?? defaultApprover;
const approverDetails = usePersonalDetailByLogin(workspaceInviteApproverDraft);
const employeePersonalDetails = usePersonalDetailsByLogins(Object.keys(policy?.employeeList ?? {}));

const isControl = isControlPolicy(policy);
const shouldShowApproverRow = isControl && policy?.approvalMode === CONST.POLICY.APPROVAL_MODE.ADVANCED && policy?.areWorkflowsEnabled;
Expand Down Expand Up @@ -183,7 +184,7 @@ function WorkspaceInviteMessageComponent({
const sendInvitation = () => {
Keyboard.dismiss();
const filteredReportActions = getAllPolicyExpenseChatReportActions(allReports, allReportActions);
const policyMemberAccountIDs = Object.values(getMemberAccountIDsForWorkspace(policy?.employeeList, false, false));
const policyMemberAccountIDs = Object.values(getMemberAccountIDsForWorkspace(policy?.employeeList, employeePersonalDetails, false, false));
const {newAccountIDs, newLogins} = getNewAccountIDsAndLogins(invitedEmailsToAccountIDsDraft, allPersonalDetails);
// Please see https://github.com/Expensify/App/blob/main/README.md#Security for more details
// See https://github.com/Expensify/App/blob/main/README.md#workspace, we set conditions about who can leave the workspace
Expand Down
Loading