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
2 changes: 2 additions & 0 deletions src/DeepLinkHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ function DeepLinkHandler({onInitialUrl}: DeepLinkHandlerProps) {
introSelected,
betas,
conciergeChat,
// The public room already exists on the server, so no optimistic report is created and the personal details are never read.
personalDetails: undefined,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why are we passing undefined here?

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.

@parasharrajat I added code comments for place that passing undefined

hasReportActions: false,
currentUserAccountID: session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
isSelfTourViewed: guidedSetupAndTourStatus?.isSelfTourViewed,
Expand Down
18 changes: 18 additions & 0 deletions src/hooks/useOptimisticPersonalDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import ONYXKEYS from '@src/ONYXKEYS';

import {optimisticPersonalDetailsSelector} from '@selectors/PersonalDetails';

import useOnyx from './useOnyx';

/**
* Subscribes to the personal details that were created optimistically only.
* Use it instead of `usePersonalDetails` when the consumer only cares about optimistic accounts:
* the optimistic set is small, so the component re-renders only when an optimistic personal detail changes
* instead of on every change of the whole personal details list.
*/
function useOptimisticPersonalDetails() {
const [optimisticPersonalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: optimisticPersonalDetailsSelector});
return optimisticPersonalDetails;
}

export default useOptimisticPersonalDetails;
2 changes: 2 additions & 0 deletions src/libs/actions/Link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ function openReportFromDeepLink(
introSelected,
// Unauthenticated public-room path: there is no signed-in user, so no Concierge chat exists to thread.
conciergeChat: undefined,
// The public room already exists on the server, so no optimistic report is created and the personal details are never read.
personalDetails: undefined,
parentReportActionID: '0',
isFromDeepLink: true,
betas,
Expand Down
27 changes: 19 additions & 8 deletions src/libs/actions/Report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,6 @@ function openReport(params: OpenReportActionParams) {
}

const participantLoginList = participants.map((p) => p.login).filter((login) => !!login);
// TODO: allPersonalDetails fallback should be removed in follow-up PRs https://github.com/Expensify/App/issues/73656
const participantAccountIDList = participants.map((p) => p.accountID).filter((id): id is number => id !== undefined);
const existingReportName = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]?.reportName;
const isCreatingNewReport = !isEmptyObject(newReportObject);
Expand Down Expand Up @@ -1991,6 +1990,7 @@ function openReport(params: OpenReportActionParams) {
const participantAccountIDs = PersonalDetailsUtils.getAccountIDsByLogins(participantLoginList);
for (const [index, login] of participantLoginList.entries()) {
const accountID = participantAccountIDs.at(index) ?? -1;
// TODO: allPersonalDetails fallback should be removed in follow-up PRs https://github.com/Expensify/App/issues/73656
const isOptimisticAccount = !(personalDetails ?? allPersonalDetails)?.[accountID];

if (!isOptimisticAccount) {
Expand Down Expand Up @@ -2629,7 +2629,17 @@ function navigateToAndOpenReport({

if (!shouldRevalidateExistingChat) {
if (isOnboardingPending) {
openReport({reportID: chat.reportID, introSelected, isSelfTourViewed, hasCompletedGuidedSetupFlow, betas, hasReportActions, currentUserAccountID, conciergeChat});
openReport({
reportID: chat.reportID,
introSelected,
isSelfTourViewed,
hasCompletedGuidedSetupFlow,
betas,
personalDetails,
hasReportActions,
currentUserAccountID,
conciergeChat,
});
}
navigateToReport(chat.reportID, {shouldDismissModal, ...linkToOptions});
return;
Expand Down Expand Up @@ -2657,7 +2667,7 @@ function navigateToAndOpenReport({

// Re-open existing chats to re-validate server-side access and refresh stale local state. Pass hasCompletedGuidedSetupFlow
// so a pending onboarding OpenReport is enqueued here too (see the create-path assumption note above).
openReport({reportID: chat.reportID, introSelected, isSelfTourViewed, hasCompletedGuidedSetupFlow, betas, hasReportActions, currentUserAccountID, conciergeChat});
openReport({reportID: chat.reportID, introSelected, isSelfTourViewed, hasCompletedGuidedSetupFlow, betas, personalDetails, hasReportActions, currentUserAccountID, conciergeChat});
navigateToReport(chat.reportID, {shouldDismissModal, ...linkToOptions});
}

Expand Down Expand Up @@ -2795,7 +2805,7 @@ function navigateToAndOpenReportWithAccountIDs(
});

// Re-open existing chats to re-validate server-side access and refresh stale local state.
openReport({reportID: chat.reportID, introSelected, isSelfTourViewed, hasCompletedGuidedSetupFlow, betas, hasReportActions, currentUserAccountID, conciergeChat});
openReport({reportID: chat.reportID, introSelected, isSelfTourViewed, hasCompletedGuidedSetupFlow, betas, personalDetails, hasReportActions, currentUserAccountID, conciergeChat});
navigateToReport(chat.reportID, {shouldDismissModal: false});
}

Expand Down Expand Up @@ -3894,7 +3904,7 @@ function toggleSubscribeToChildReport({
hasReportActions,
}: ToggleSubscribeToChildReportParams) {
if (childReportID) {
openReport({reportID: childReportID, introSelected, betas, isSelfTourViewed, hasCompletedGuidedSetupFlow, hasReportActions, currentUserAccountID, conciergeChat});
openReport({reportID: childReportID, introSelected, betas, personalDetails, isSelfTourViewed, hasCompletedGuidedSetupFlow, hasReportActions, currentUserAccountID, conciergeChat});
const parentReportActionID = parentReportAction.reportActionID;
if (!prevNotificationPreference || isHiddenForCurrentUser(prevNotificationPreference)) {
updateNotificationPreference(
Expand Down Expand Up @@ -4838,11 +4848,11 @@ function navigateToConciergeChatAndDeleteReport(
);
}

function cleanUpOptimisticPersonalDetailsForFailedChat(report: OnyxEntry<Report>, currentUserAccountID: number) {
function cleanUpOptimisticPersonalDetailsForFailedChat(report: OnyxEntry<Report>, currentUserAccountID: number, optimisticPersonalDetails: OnyxEntry<PersonalDetailsList>) {
const personalDetailsToRemove: PersonalDetailsList = {};

for (const accountID of Object.keys(report?.participants ?? {}).map(Number)) {
if (accountID === currentUserAccountID || !allPersonalDetails?.[accountID]?.isOptimisticPersonalDetail) {
if (accountID === currentUserAccountID || !optimisticPersonalDetails?.[accountID]?.isOptimisticPersonalDetail) {
continue;
}
personalDetailsToRemove[accountID] = null;
Expand All @@ -4865,6 +4875,7 @@ function clearCreateChatError(
reportOwnerPersonalDetail: OnyxEntry<PersonalDetails>,
currentUserPersonalDetail: OnyxEntry<PersonalDetails>,
conciergePersonalDetail: OnyxEntry<PersonalDetails>,
optimisticPersonalDetails: OnyxEntry<PersonalDetailsList>,
) {
const metaData = getReportMetadata(report?.reportID);
const isOptimisticReport = metaData?.isOptimisticReport;
Expand All @@ -4874,7 +4885,7 @@ function clearCreateChatError(
}

if (report?.errorFields?.createChat && isOptimisticReport) {
cleanUpOptimisticPersonalDetailsForFailedChat(report, currentUserAccountID);
cleanUpOptimisticPersonalDetailsForFailedChat(report, currentUserAccountID, optimisticPersonalDetails);
}

navigateToConciergeChatAndDeleteReport(
Expand Down
22 changes: 20 additions & 2 deletions src/libs/actions/replaceOptimisticReportWithActualReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,16 @@ function replaceOptimisticReportWithActualReport(report: Report, draftReportComm
// betas and conciergeChat are safe to pass as undefined because introSelected is undefined, so the
// guided-setup code path that uses them is never reached. Passing them explicitly so the compiler
// flags this when they become required. Refactor issues: https://github.com/Expensify/App/issues/66424
openReport({reportID: parentReportID, introSelected: undefined, betas: undefined, conciergeChat: undefined, hasReportActions, currentUserAccountID});
// personalDetails is undefined because the parent report already exists, so no optimistic report is created and they are never read.
openReport({
reportID: parentReportID,
introSelected: undefined,
betas: undefined,
conciergeChat: undefined,
personalDetails: undefined,
hasReportActions,
currentUserAccountID,
});
});
} else {
callback();
Expand All @@ -235,7 +244,16 @@ function replaceOptimisticReportWithActualReport(report: Report, draftReportComm
// betas and conciergeChat are safe to pass as undefined because introSelected is undefined, so the
// guided-setup code path that uses them is never reached. Passing them explicitly so the compiler
// flags this when they become required. Refactor issues: https://github.com/Expensify/App/issues/66424
openReport({reportID: parentReportID, introSelected: undefined, betas: undefined, conciergeChat: undefined, hasReportActions, currentUserAccountID});
// personalDetails is undefined because the parent report already exists, so no optimistic report is created and they are never read.
openReport({
reportID: parentReportID,
introSelected: undefined,
betas: undefined,
conciergeChat: undefined,
personalDetails: undefined,
hasReportActions,
currentUserAccountID,
});
}
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/pages/Search/SearchMoneyRequestReportPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ function SearchMoneyRequestReportPage({route}: SearchMoneyRequestPageProps) {
introSelected,
conciergeChat,
betas,
personalDetails,
hasReportActions,
currentUserAccountID,
isSelfTourViewed: guidedSetupAndTourStatus?.isSelfTourViewed,
Expand Down
4 changes: 3 additions & 1 deletion src/pages/inbox/ReportFetchHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ function ReportFetchHandler() {
reportActionID: reportActionIDFromRoute,
participants: dmParticipants,
betas,
personalDetails,
hasReportActions,
currentUserAccountID,
isSelfTourViewed,
Expand Down Expand Up @@ -256,7 +257,7 @@ function ReportFetchHandler() {
if (!shouldUseNarrowLayout || !isChatThread(report) || !isHiddenForCurrentUser(report) || isTransactionThreadView) {
return;
}
openReport({reportID, introSelected, conciergeChat, betas, hasReportActions, currentUserAccountID, isSelfTourViewed, hasCompletedGuidedSetupFlow});
openReport({reportID, introSelected, conciergeChat, betas, personalDetails, hasReportActions, currentUserAccountID, isSelfTourViewed, hasCompletedGuidedSetupFlow});
});

const joinPublicRoomIfNeeded = useEffectEvent(() => {
Expand All @@ -269,6 +270,7 @@ function ReportFetchHandler() {
introSelected,
conciergeChat,
betas,
personalDetails,
hasReportActions: hasViewingPublicRoomReportActions,
currentUserAccountID,
isSelfTourViewed,
Expand Down
3 changes: 2 additions & 1 deletion src/pages/inbox/report/ContextMenu/ContextMenuActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,13 @@ const ContextMenuActions: ContextMenuAction[] = [
childReportActions,
currentUserAccountID,
conciergeChat,
personalDetails,
},
) => {
if (isMoneyRequestAction(reportAction) || isMoneyRequestAction(moneyRequestAction)) {
const editExpense = () => {
const childReportID = reportAction?.childReportID;
openReport({reportID: childReportID, introSelected, betas, hasReportActions: !!childReportActions, currentUserAccountID, conciergeChat});
openReport({reportID: childReportID, introSelected, betas, personalDetails, hasReportActions: !!childReportActions, currentUserAccountID, conciergeChat});
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(childReportID));
};
if (closePopover) {
Expand Down
3 changes: 3 additions & 0 deletions src/pages/inbox/report/ReportActionItemCreated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'
import useIsInSidePanel from '@hooks/useIsInSidePanel';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import useOptimisticPersonalDetails from '@hooks/useOptimisticPersonalDetails';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';

Expand Down Expand Up @@ -49,6 +50,7 @@ function ReportActionItemCreated({reportID, policyID}: ReportActionItemCreatedPr
const {accountID: currentUserAccountID} = currentUserPersonalDetail;
const [conciergePersonalDetail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: conciergePersonalDetailSelector});
const [reportOwnerPersonalDetail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: personalDetailsSelector(report?.ownerAccountID)});
const optimisticPersonalDetails = useOptimisticPersonalDetails();

const otherParticipantAccountID =
Object.keys(report?.participants ?? {})
Expand Down Expand Up @@ -78,6 +80,7 @@ function ReportActionItemCreated({reportID, policyID}: ReportActionItemCreatedPr
reportOwnerPersonalDetail,
currentUserPersonalDetail,
conciergePersonalDetail,
optimisticPersonalDetails,
)
}
>
Expand Down
1 change: 1 addition & 0 deletions src/pages/workspace/rooms/WorkspaceRoomsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function WorkspaceRoomsPage({route}: WorkspaceRoomsPageProps) {
introSelected,
conciergeChat,
betas,
personalDetails,
shouldMarkAsRead: false,
hasReportActions: !!hasReportActions?.[report.reportID],
currentUserAccountID,
Expand Down
16 changes: 16 additions & 0 deletions src/selectors/PersonalDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ const isOptimisticPersonalDetailSelector =
return isPersonalDetailOptimistic(personalDetailsList[accountID]);
};

/**
* Returns only the personal details that were created optimistically. The optimistic set is tiny compared to the whole
* personal details list, so subscribers using it don't re-render every time an unrelated (server-backed) detail changes.
*/
const optimisticPersonalDetailsSelector = (personalDetailsList: OnyxEntry<PersonalDetailsList>): PersonalDetailsList => {
const optimisticPersonalDetails: PersonalDetailsList = {};
for (const [accountID, personalDetail] of Object.entries(personalDetailsList ?? {})) {
if (!personalDetail?.isOptimisticPersonalDetail) {
continue;
}
optimisticPersonalDetails[accountID] = personalDetail;
}
return optimisticPersonalDetails;
};

const newAccountIDsAndLoginsSelector = (invitedEmailsToAccountIDs: InvitedEmailsToAccountIDs | undefined) => (personalDetailsList: OnyxEntry<PersonalDetailsList>) =>
getNewAccountIDsAndLogins(invitedEmailsToAccountIDs, personalDetailsList);

Expand All @@ -113,6 +128,7 @@ export {
doesPersonalDetailExistSelector,
accountIDToLoginSelector,
isOptimisticPersonalDetailSelector,
optimisticPersonalDetailsSelector,
createDisplayDetailsByAccountIDsSelector,
newAccountIDsAndLoginsSelector,
displayNameSelector,
Expand Down
Loading
Loading