Skip to content
22 changes: 21 additions & 1 deletion src/libs/PersonalDetailsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ function getDisplayNameOrDefault(
return shouldFallbackToHidden ? hiddenTranslation : '';
}

function getPersonalDetailsByID(accountID: number | undefined, personalDetailsList: OnyxEntry<PersonalDetailsList>): PersonalDetails | undefined {
return accountID ? (personalDetailsList?.[accountID] ?? undefined) : undefined;
}

/**
* Given a list of account IDs (as number) it will return an array of personal details objects.
* @param accountIDs - Array of accountIDs
Expand Down Expand Up @@ -140,7 +144,7 @@ function getPersonalDetailsByIDs({
function newGetPersonalDetailsByIDs(accountIDs: number[], personalDetails: OnyxEntry<PersonalDetailsList>): PersonalDetails[] {
const result: PersonalDetails[] = [];
for (const accountID of accountIDs) {
const detail = personalDetails?.[accountID];
const detail = getPersonalDetailsByID(accountID, personalDetails);
if (!detail) {
continue;
}
Expand All @@ -150,6 +154,20 @@ function newGetPersonalDetailsByIDs(accountIDs: number[], personalDetails: OnyxE
return result;
}

function getPersonalDetailsListByIDs(accountIDs: Array<number | undefined>, personalDetails: OnyxEntry<PersonalDetailsList>): PersonalDetailsList {
return accountIDs.reduce((acc, accountID) => {
if (!accountID) {
return acc;
}
const detail = personalDetails?.[accountID];
if (!detail) {
return acc;
}
acc[accountID] = detail;
return acc;
}, {} as PersonalDetailsList);
}

function getDisplayNameOrYou(displayName: string, accountID: number, currentUserAccountID: number, translate: LocalizedTranslate) {
if (accountID === currentUserAccountID) {
return translate('common.you');
Expand Down Expand Up @@ -493,8 +511,10 @@ function areTravelPersonalDetailsMissing(privatePersonalDetails: OnyxEntry<Priva

export {
getDisplayNameOrDefault,
getPersonalDetailsByID,
getPersonalDetailsByIDs,
newGetPersonalDetailsByIDs,
getPersonalDetailsListByIDs,
getDisplayNameOrYou,
getPersonalDetailByEmail,
getAccountIDsByLogins,
Expand Down
12 changes: 4 additions & 8 deletions src/libs/actions/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ type CreateTaskAndNavigateParams = {
ancestors?: ReportUtils.Ancestor[];
currentUserDisplayName: string | undefined;
currentUserAvatar: AvatarSource | undefined;
taskCreatorAndAssigneeDetails: OnyxEntry<OnyxTypes.PersonalDetailsList>;
};

/**
Expand Down Expand Up @@ -122,6 +123,7 @@ function createTaskAndNavigate(params: CreateTaskAndNavigateParams) {
isCreatedUsingMarkdown = false,
quickAction = {},
ancestors = [],
taskCreatorAndAssigneeDetails,
} = params;
const parentReportID = parentReport?.reportID;
if (!parentReportID) {
Expand Down Expand Up @@ -205,13 +207,6 @@ function createTaskAndNavigate(params: CreateTaskAndNavigateParams) {
},
];

// We need the personal details of the task creator and assignee so that the "From" and "Assignee" columns wil be rendered in "Reports > Task" while offline.
const personalDetailsList = PersonalDetailsUtils.createPersonalDetailsLookupByAccountID(
PersonalDetailsUtils.getPersonalDetailsByIDs({
accountIDs: [currentUserAccountID, assigneeAccountID],
}),
);

// FOR TASK REPORT
const successData: Array<
OnyxUpdate<typeof ONYXKEYS.COLLECTION.REPORT | typeof ONYXKEYS.COLLECTION.REPORT_METADATA | typeof ONYXKEYS.PERSONAL_DETAILS_LIST | typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS>
Expand Down Expand Up @@ -300,7 +295,8 @@ function createTaskAndNavigate(params: CreateTaskAndNavigateParams) {
snapshotDataToStore[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`] = {
[optimisticAddCommentReport.reportAction.reportActionID]: optimisticAddCommentReport.reportAction as OnyxTypes.ReportAction,
};
snapshotDataToStore[ONYXKEYS.PERSONAL_DETAILS_LIST] = personalDetailsList;
// We need the personal details of the task creator and assignee so that the "From" and "Assignee" columns wil be rendered in "Reports > Task" while offline.
snapshotDataToStore[ONYXKEYS.PERSONAL_DETAILS_LIST] = taskCreatorAndAssigneeDetails;

// We push the optimistic task data into chat and task snapshot hashes so it appears immediately in "Reports > Chats" and "Reports > Task" while offline.
for (const type of searchDataTypes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ function useComposerSubmit(reportID: string) {
taskTitle = `@${mentionWithDomain} ${taskTitle}`;
}
}

const taskCreatorAndAssigneeDetails = {[currentUserPersonalDetails.accountID]: currentUserPersonalDetails};
if (assignee) {
taskCreatorAndAssigneeDetails[assignee.accountID] = assignee;
}

createTaskAndNavigate({
parentReport: report,
title: taskTitle,
Expand All @@ -124,6 +130,7 @@ function useComposerSubmit(reportID: string) {
isCreatedUsingMarkdown: true,
quickAction,
ancestors: reportAncestors,
taskCreatorAndAssigneeDetails,
});
return;
}
Expand Down
5 changes: 5 additions & 0 deletions src/pages/tasks/NewTaskDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import {personalDetailsListSelector} from '@src/selectors/PersonalDetails';
import INPUT_IDS from '@src/types/form/NewTaskForm';

type NewTaskDetailsPageProps = PlatformStackScreenProps<NewTaskNavigatorParamList, typeof SCREENS.NEW_TASK.DETAILS>;
Expand All @@ -34,6 +35,9 @@ function NewTaskDetailsPage({route}: NewTaskDetailsPageProps) {
const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${task?.parentReportID}`, undefined, [task?.parentReportID]);
const ancestors = useAncestors(parentReport);
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const [taskCreatorAndAssigneeDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {
selector: personalDetailsListSelector([currentUserPersonalDetails.accountID, task?.assigneeAccountID]),
});
const styles = useThemeStyles();
const {translate} = useLocalize();
const [localTitle, setLocalTitle] = useState<string>();
Expand Down Expand Up @@ -88,6 +92,7 @@ function NewTaskDetailsPage({route}: NewTaskDetailsPageProps) {
isCreatedUsingMarkdown: false,
quickAction,
ancestors,
taskCreatorAndAssigneeDetails,
});
} else {
Navigation.navigate(ROUTES.NEW_TASK.getRoute(backTo));
Expand Down
5 changes: 5 additions & 0 deletions src/pages/tasks/NewTaskPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import {personalDetailsListSelector} from '@src/selectors/PersonalDetails';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

type NewTaskPageProps = PlatformStackScreenProps<NewTaskNavigatorParamList, typeof SCREENS.NEW_TASK.ROOT>;
Expand All @@ -41,6 +42,9 @@ function NewTaskPage({route}: NewTaskPageProps) {
const [quickAction] = useOnyx(ONYXKEYS.NVP_QUICK_ACTION_GLOBAL_CREATE);
const reportAttributes = useReportAttributes();
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
const [taskCreatorAndAssigneeDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {
selector: personalDetailsListSelector([currentUserPersonalDetails.accountID, task?.assigneeAccountID]),
});
const styles = useThemeStyles();
const {translate, formatPhoneNumber, localeCompare} = useLocalize();
const assignee = getAssignee(task?.assigneeAccountID ?? CONST.DEFAULT_NUMBER_ID, personalDetails);
Expand Down Expand Up @@ -113,6 +117,7 @@ function NewTaskPage({route}: NewTaskPageProps) {
isCreatedUsingMarkdown: false,
quickAction,
ancestors,
taskCreatorAndAssigneeDetails,
});
};

Expand Down
4 changes: 2 additions & 2 deletions src/pages/workspace/WorkspaceMembersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {WorkspaceSplitNavigatorParamList} from '@libs/Navigation/types';
import {isPersonalDetailsReady, sortAlphabetically} from '@libs/OptionsListUtils';
import {getDisplayNameOrDefault, newGetPersonalDetailsByIDs} from '@libs/PersonalDetailsUtils';
import {getDisplayNameOrDefault, getPersonalDetailsByID} from '@libs/PersonalDetailsUtils';
import {
canEditWorkspaceSettings,
getConnectionExporters,
Expand Down Expand Up @@ -218,7 +218,7 @@ function WorkspaceMembersPage({personalDetails, route, policy}: WorkspaceMembers
const firstSelectedEmployeeAccountID = policyMemberEmailsToAccountIDs[selectedEmployees[0]];
return translate('workspace.people.removeMembersPrompt', {
count: selectedEmployees.length,
memberName: formatPhoneNumber(newGetPersonalDetailsByIDs([firstSelectedEmployeeAccountID], personalDetails).at(0)?.displayName ?? ''),
memberName: formatPhoneNumber(getPersonalDetailsByID(firstSelectedEmployeeAccountID, personalDetails)?.displayName ?? ''),
});
}, [selectedEmployees, policyMemberEmailsToAccountIDs, translate, policy, formatPhoneNumber, personalDetails]);
/**
Expand Down
8 changes: 6 additions & 2 deletions src/selectors/PersonalDetails.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type {OnyxEntry} from 'react-native-onyx';
import {getDisplayNameOrDefault, getLoginByAccountID, newGetPersonalDetailsByIDs} from '@libs/PersonalDetailsUtils';
import {getDisplayNameOrDefault, getLoginByAccountID, getPersonalDetailsByID, getPersonalDetailsListByIDs, newGetPersonalDetailsByIDs} from '@libs/PersonalDetailsUtils';
import CONST from '@src/CONST';
import type {PersonalDetailsList, Report} from '@src/types/onyx';
import type PersonalDetails from '@src/types/onyx/PersonalDetails';

const personalDetailsSelector = (accountID: number | undefined) => (personalDetailsList: OnyxEntry<PersonalDetailsList>) => (accountID ? personalDetailsList?.[accountID] : undefined);
const personalDetailsSelector = (accountID: number | undefined) => (personalDetailsList: OnyxEntry<PersonalDetailsList>) => getPersonalDetailsByID(accountID, personalDetailsList);

const multiPersonalDetailsSelector = (accountIDs: number[]) => (personalDetails: OnyxEntry<PersonalDetailsList>) => newGetPersonalDetailsByIDs(accountIDs, personalDetails);

const personalDetailsListSelector = (accountIDs: Array<number | undefined>) => (personalDetailsList: OnyxEntry<PersonalDetailsList>) =>
getPersonalDetailsListByIDs(accountIDs, personalDetailsList);

const personalDetailsLoginSelector = (accountID: number | undefined) => (personalDetailsList: OnyxEntry<PersonalDetailsList>) => getLoginByAccountID(accountID, personalDetailsList);

const personalDetailsDisplayNameSelector = (accountID: number) => (personalDetails: OnyxEntry<PersonalDetailsList>) => getDisplayNameOrDefault(personalDetails?.[accountID]);
Expand All @@ -33,6 +36,7 @@ const accountIDToLoginSelector = (reportsToArchive: Report[]) => (personalDetail
export {
personalDetailsSelector,
multiPersonalDetailsSelector,
personalDetailsListSelector,
personalDetailsDisplayNameSelector,
personalDetailsLoginSelector,
personalDetailByAccountIDSelector,
Expand Down
15 changes: 14 additions & 1 deletion tests/actions/TaskTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ describe('actions/Task', () => {
policyID: mockPolicyID,
isCreatedUsingMarkdown: false,
quickAction: {},
taskCreatorAndAssigneeDetails: undefined,
});
// Then: Verify API.write called with expected arguments
const calls = (API.write as jest.Mock).mock.calls;
Expand Down Expand Up @@ -464,6 +465,7 @@ describe('actions/Task', () => {
policyID: mockPolicyID,
isCreatedUsingMarkdown: false,
quickAction: {},
taskCreatorAndAssigneeDetails: undefined,
});

await waitForBatchedUpdatesWithAct();
Expand Down Expand Up @@ -493,6 +495,7 @@ describe('actions/Task', () => {
policyID: mockPolicyID,
isCreatedUsingMarkdown: false,
quickAction: {},
taskCreatorAndAssigneeDetails: undefined,
});

await waitForBatchedUpdatesWithAct();
Expand Down Expand Up @@ -527,6 +530,7 @@ describe('actions/Task', () => {
policyID: mockPolicyID,
isCreatedUsingMarkdown: false,
quickAction: mockQuickAction,
taskCreatorAndAssigneeDetails: undefined,
});

await waitForBatchedUpdatesWithAct();
Expand Down Expand Up @@ -589,6 +593,7 @@ describe('actions/Task', () => {
policyID: mockPolicyID,
isCreatedUsingMarkdown: true,
quickAction: {},
taskCreatorAndAssigneeDetails: undefined,
});

await waitForBatchedUpdatesWithAct();
Expand Down Expand Up @@ -629,6 +634,7 @@ describe('actions/Task', () => {
policyID: CONST.POLICY.OWNER_EMAIL_FAKE,
isCreatedUsingMarkdown: false,
quickAction: {},
taskCreatorAndAssigneeDetails: undefined,
});

await waitForBatchedUpdatesWithAct();
Expand Down Expand Up @@ -676,6 +682,7 @@ describe('actions/Task', () => {
policyID: mockPolicyID,
isCreatedUsingMarkdown: false,
quickAction: {},
taskCreatorAndAssigneeDetails: undefined,
});

await waitForBatchedUpdatesWithAct();
Expand Down Expand Up @@ -733,6 +740,7 @@ describe('actions/Task', () => {
policyID: mockPolicyID,
isCreatedUsingMarkdown: false,
quickAction: {},
taskCreatorAndAssigneeDetails: undefined,
});

await waitForBatchedUpdatesWithAct();
Expand Down Expand Up @@ -769,7 +777,8 @@ describe('actions/Task', () => {
assigneeChatReport: mockAssigneeChatReport,
policyID: mockPolicyID,
isCreatedUsingMarkdown: false,
quickAction: {}, // quickAction is empty
quickAction: {},
taskCreatorAndAssigneeDetails: undefined, // quickAction is empty
});

await waitForBatchedUpdatesWithAct();
Expand Down Expand Up @@ -810,6 +819,7 @@ describe('actions/Task', () => {
policyID: mockPolicyID,
isCreatedUsingMarkdown: false,
quickAction: {},
taskCreatorAndAssigneeDetails: undefined,
});

// Then: buildOptimisticCreatedReportAction receives the exact identity values that were passed in
Expand Down Expand Up @@ -838,6 +848,7 @@ describe('actions/Task', () => {
policyID: mockPolicyID,
isCreatedUsingMarkdown: false,
quickAction: {},
taskCreatorAndAssigneeDetails: undefined,
});

// Then: buildOptimisticCreatedReportAction is invoked with the same undefined values
Expand Down Expand Up @@ -866,6 +877,7 @@ describe('actions/Task', () => {
policyID: mockPolicyID,
isCreatedUsingMarkdown: false,
quickAction: {},
taskCreatorAndAssigneeDetails: undefined,
});

// Then: emailCreatingAction is the empty string we provided (no fallback to a session value)
Expand Down Expand Up @@ -896,6 +908,7 @@ describe('actions/Task', () => {
policyID: mockPolicyID,
isCreatedUsingMarkdown: false,
quickAction: {},
taskCreatorAndAssigneeDetails: undefined,
});

await waitForBatchedUpdatesWithAct();
Expand Down
30 changes: 29 additions & 1 deletion tests/unit/PersonalDetailsSelectorTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import {multiPersonalDetailsSelector, personalDetailsDisplayNameSelector, personalDetailsLoginSelector, personalDetailsSelector} from '@selectors/PersonalDetails';
import {
multiPersonalDetailsSelector,
personalDetailsDisplayNameSelector,
personalDetailsListSelector,
personalDetailsLoginSelector,
personalDetailsSelector,
} from '@selectors/PersonalDetails';
import {getDisplayNameOrDefault} from '@libs/PersonalDetailsUtils';
import CONST from '@src/CONST';
import type {PersonalDetailsList} from '@src/types/onyx';
Expand Down Expand Up @@ -112,4 +118,26 @@ describe('PersonalDetailsSelector', () => {
expect(result).toEqual([]);
});
});

describe('personalDetailsListSelector', () => {
it('should return the personal details list for the given accountIDs', () => {
const result = personalDetailsListSelector([accountID])(personalDetailsList);
expect(result).toEqual(personalDetailsList);
});

it('should filter out accountIDs that do not exist in the list', () => {
const result = personalDetailsListSelector([accountID, 999])(personalDetailsList);
expect(result).toEqual(personalDetailsList);
});

it('should return an empty object if accountIDs is empty', () => {
const result = personalDetailsListSelector([])(personalDetailsList);
expect(result).toEqual({});
});

it('should return an empty object if the personalDetailsList is undefined', () => {
const result = personalDetailsListSelector([accountID])(undefined);
expect(result).toEqual({});
});
});
});
Loading
Loading