Skip to content
Merged
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
20 changes: 13 additions & 7 deletions src/pages/workspace/WorkspaceInvitePage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import type {SectionListData} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
Expand Down Expand Up @@ -71,6 +71,8 @@ function WorkspaceInvitePage({
const [personalDetails, setPersonalDetails] = useState<OptionData[]>([]);
const [usersToInvite, setUsersToInvite] = useState<OptionData[]>([]);
const [didScreenTransitionEnd, setDidScreenTransitionEnd] = useState(false);
const firstRenderRef = useRef(true);

const openWorkspaceInvitePage = () => {
const policyMemberEmailsToAccountIDs = PolicyUtils.getMemberAccountIDsForWorkspace(policyMembers, personalDetailsProp);
Policy.openWorkspaceInvitePage(route.params.policyID, Object.keys(policyMemberEmailsToAccountIDs));
Expand Down Expand Up @@ -113,12 +115,16 @@ function WorkspaceInvitePage({
});

const newSelectedOptions: MemberForList[] = [];
Object.keys(invitedEmailsToAccountIDsDraft ?? {}).forEach((login) => {
if (!(login in detailsMap)) {
return;
}
newSelectedOptions.push({...detailsMap[login], isSelected: true});
});
if (firstRenderRef.current) {
// We only want to add the saved selected user on first render
firstRenderRef.current = false;
Object.keys(invitedEmailsToAccountIDsDraft ?? {}).forEach((login) => {
if (!(login in detailsMap)) {
return;
}
newSelectedOptions.push({...detailsMap[login], isSelected: true});
});
}

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.

I decided to use firstRenderRef here instead of this init in useState.

const [selectedOptions, setSelectedOptions] = useState<MemberForList[]>(() => Object.values(invitedEmailsToAccountIDsDraft ?? {})
    .filter((accountID) => personalDetailsProp?.[accountID])
    .map((accountID) => ({...OptionsListUtils.formatMemberForList(personalDetailsProp[accountID]), isSelected: true}))
);

The reason is because the personal detail data of personalDetailsProp is not the same as inviteOptions.personalDetails.

I first think of copying this logic:

const inviteOptions = OptionsListUtils.getMemberInviteOptions(personalDetailsProp, betas ?? [], searchTerm, excludedUsers, true);
// Update selectedOptions with the latest personalDetails and policyMembers information
const detailsMap: Record<string, MemberForList> = {};
inviteOptions.personalDetails.forEach((detail) => {
    if (!detail.login) {
        return;
    }
    detailsMap[detail.login] = OptionsListUtils.formatMemberForList(detail);
});

but I think it would be better to just use the ref.

selectedOptions.forEach((option) => {
newSelectedOptions.push(option.login && option.login in detailsMap ? {...detailsMap[option.login], isSelected: true} : option);
});
Expand Down