From ca12bc8810f87297a0fb650a0cd8d6889e145fac Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 06:58:32 -1000 Subject: [PATCH 01/14] Pass the chatType --- src/libs/API/parameters/StartSplitBillParams.ts | 1 + src/libs/actions/IOU.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/src/libs/API/parameters/StartSplitBillParams.ts b/src/libs/API/parameters/StartSplitBillParams.ts index 30d21697ac67..623f291eb691 100644 --- a/src/libs/API/parameters/StartSplitBillParams.ts +++ b/src/libs/API/parameters/StartSplitBillParams.ts @@ -12,6 +12,7 @@ type StartSplitBillParams = { isFromGroupDM: boolean; createdReportActionID?: string; billable: boolean; + chatType?: string; }; export default StartSplitBillParams; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 33cd660b65f9..2d494c763503 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -3047,6 +3047,7 @@ function startSplitBill( isFromGroupDM: !existingSplitChatReport, billable, ...(existingSplitChatReport ? {} : {createdReportActionID: splitChatCreatedReportAction.reportActionID}), + chatType: splitChatReport?.chatType, }; API.write(WRITE_COMMANDS.START_SPLIT_BILL, parameters, {optimisticData, successData, failureData}); From 744a59fc88c183a2b20153fb29004d453a198b05 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 07:52:20 -1000 Subject: [PATCH 02/14] Refactor duplicate logic to get or create split chat report --- src/libs/actions/IOU.ts | 61 +++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 2d494c763503..6ba3500a27ae 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2280,31 +2280,7 @@ function createSplitsAndOnyxData( const currentUserEmailForIOUSplit = PhoneNumber.addSMSDomainIfPhoneNumber(currentUserLogin); const participantAccountIDs = participants.map((participant) => Number(participant.accountID)); - const existingChatReportID = existingSplitChatReportID || participants[0].reportID; - let existingSplitChatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingChatReportID}`]; - if (!existingSplitChatReport) { - existingSplitChatReport = participants.length < 2 ? ReportUtils.getChatByParticipants(participantAccountIDs) : null; - } - let newChat: ReportUtils.OptimisticChatReport | EmptyObject = {}; - const allParticipantsAccountIDs = [...participantAccountIDs, currentUserAccountID]; - if (!existingSplitChatReport && participants.length > 1) { - newChat = ReportUtils.buildOptimisticChatReport( - allParticipantsAccountIDs, - '', - CONST.REPORT.CHAT_TYPE.GROUP, - undefined, - undefined, - undefined, - undefined, - undefined, - undefined, - CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, - ); - } - if (isEmptyObject(newChat)) { - newChat = ReportUtils.buildOptimisticChatReport(allParticipantsAccountIDs); - } - const splitChatReport = existingSplitChatReport ?? newChat; + const {splitChatReport, existingSplitChatReport} = getOrCreateSplitChatReport(existingSplitChatReportID, participants, participantAccountIDs, currentUserAccountID); const isOwnPolicyExpenseChat = !!splitChatReport.isOwnPolicyExpenseChat; const splitTransaction = TransactionUtils.buildOptimisticTransaction( @@ -2647,6 +2623,35 @@ function createSplitsAndOnyxData( }; } +function getOrCreateSplitChatReport(existingSplitChatReportID: string, participants: Participant[], participantAccountIDs: number[], currentUserAccountID: number) { + const existingChatReportID = existingSplitChatReportID || participants[0].reportID; + let existingSplitChatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingChatReportID}`]; + if (!existingSplitChatReport) { + existingSplitChatReport = participants.length < 2 ? ReportUtils.getChatByParticipants(participantAccountIDs) : null; + } + let newChat: ReportUtils.OptimisticChatReport | EmptyObject = {}; + const allParticipantsAccountIDs = [...participantAccountIDs, currentUserAccountID]; + if (!existingSplitChatReport && participants.length > 1) { + newChat = ReportUtils.buildOptimisticChatReport( + allParticipantsAccountIDs, + '', + CONST.REPORT.CHAT_TYPE.GROUP, + undefined, + undefined, + undefined, + undefined, + undefined, + undefined, + CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN + ); + } + if (isEmptyObject(newChat)) { + newChat = ReportUtils.buildOptimisticChatReport(allParticipantsAccountIDs); + } + const splitChatReport = existingSplitChatReport ?? newChat; + return { splitChatReport, existingSplitChatReport }; +} + /** * @param amount - always in smallest currency unit * @param existingSplitChatReportID - Either a group DM or a workspace chat @@ -2785,11 +2790,7 @@ function startSplitBill( ) { const currentUserEmailForIOUSplit = PhoneNumber.addSMSDomainIfPhoneNumber(currentUserLogin); const participantAccountIDs = participants.map((participant) => Number(participant.accountID)); - const existingSplitChatReport = - existingSplitChatReportID || participants[0].reportID - ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingSplitChatReportID || participants[0].reportID}`] - : ReportUtils.getChatByParticipants(participantAccountIDs); - const splitChatReport = existingSplitChatReport ?? ReportUtils.buildOptimisticChatReport(participantAccountIDs); + const {splitChatReport, existingSplitChatReport} = getOrCreateSplitChatReport(existingSplitChatReportID, participants, participantAccountIDs, currentUserAccountID); const isOwnPolicyExpenseChat = !!splitChatReport.isOwnPolicyExpenseChat; const {name: filename, source, state = CONST.IOU.RECEIPT_STATE.SCANREADY} = receipt; From 2ff25d1c86cd77008ed384ae76d696b43115f358 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 07:54:24 -1000 Subject: [PATCH 03/14] run prettier --- src/libs/actions/IOU.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 6ba3500a27ae..0f325d001fcc 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2642,14 +2642,14 @@ function getOrCreateSplitChatReport(existingSplitChatReportID: string, participa undefined, undefined, undefined, - CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN + CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, ); } if (isEmptyObject(newChat)) { newChat = ReportUtils.buildOptimisticChatReport(allParticipantsAccountIDs); } const splitChatReport = existingSplitChatReport ?? newChat; - return { splitChatReport, existingSplitChatReport }; + return {splitChatReport, existingSplitChatReport}; } /** From db186982229cd670360c41ef4e24fde8c42956a6 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 07:56:49 -1000 Subject: [PATCH 04/14] give better name since we dont actually create any report just optimistic one --- src/libs/actions/IOU.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 0f325d001fcc..9ee90a5090f0 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2280,7 +2280,7 @@ function createSplitsAndOnyxData( const currentUserEmailForIOUSplit = PhoneNumber.addSMSDomainIfPhoneNumber(currentUserLogin); const participantAccountIDs = participants.map((participant) => Number(participant.accountID)); - const {splitChatReport, existingSplitChatReport} = getOrCreateSplitChatReport(existingSplitChatReportID, participants, participantAccountIDs, currentUserAccountID); + const {splitChatReport, existingSplitChatReport} = getOrCreateOptimisticSplitChatReport(existingSplitChatReportID, participants, participantAccountIDs, currentUserAccountID); const isOwnPolicyExpenseChat = !!splitChatReport.isOwnPolicyExpenseChat; const splitTransaction = TransactionUtils.buildOptimisticTransaction( @@ -2623,7 +2623,7 @@ function createSplitsAndOnyxData( }; } -function getOrCreateSplitChatReport(existingSplitChatReportID: string, participants: Participant[], participantAccountIDs: number[], currentUserAccountID: number) { +function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, participants: Participant[], participantAccountIDs: number[], currentUserAccountID: number) { const existingChatReportID = existingSplitChatReportID || participants[0].reportID; let existingSplitChatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingChatReportID}`]; if (!existingSplitChatReport) { @@ -2790,7 +2790,7 @@ function startSplitBill( ) { const currentUserEmailForIOUSplit = PhoneNumber.addSMSDomainIfPhoneNumber(currentUserLogin); const participantAccountIDs = participants.map((participant) => Number(participant.accountID)); - const {splitChatReport, existingSplitChatReport} = getOrCreateSplitChatReport(existingSplitChatReportID, participants, participantAccountIDs, currentUserAccountID); + const {splitChatReport, existingSplitChatReport} = getOrCreateOptimisticSplitChatReport(existingSplitChatReportID, participants, participantAccountIDs, currentUserAccountID); const isOwnPolicyExpenseChat = !!splitChatReport.isOwnPolicyExpenseChat; const {name: filename, source, state = CONST.IOU.RECEIPT_STATE.SCANREADY} = receipt; From aec400275cb82f264843eb1cacd0ad111dd4ba3c Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 08:04:17 -1000 Subject: [PATCH 05/14] Fix avatar leading to not found page --- src/components/RoomHeaderAvatars.tsx | 7 ++++++- src/pages/ReportDetailsPage.tsx | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/components/RoomHeaderAvatars.tsx b/src/components/RoomHeaderAvatars.tsx index 9298062aa6f9..c23108adc0ea 100644 --- a/src/components/RoomHeaderAvatars.tsx +++ b/src/components/RoomHeaderAvatars.tsx @@ -13,10 +13,15 @@ import Text from './Text'; type RoomHeaderAvatarsProps = { icons: Icon[]; reportID: string; + isGroupChat?: boolean; }; -function RoomHeaderAvatars({icons, reportID}: RoomHeaderAvatarsProps) { +function RoomHeaderAvatars({icons, reportID, isGroupChat}: RoomHeaderAvatarsProps) { const navigateToAvatarPage = (icon: Icon) => { + if (isGroupChat) { + return; + } + if (icon.type === CONST.ICON_TYPE_WORKSPACE) { Navigation.navigate(ROUTES.REPORT_AVATAR.getRoute(reportID)); return; diff --git a/src/pages/ReportDetailsPage.tsx b/src/pages/ReportDetailsPage.tsx index 80563fcf7b1b..ccfe329419e3 100644 --- a/src/pages/ReportDetailsPage.tsx +++ b/src/pages/ReportDetailsPage.tsx @@ -218,6 +218,7 @@ function ReportDetailsPage({policies, report, session, personalDetails}: ReportD )} From c3892b1f003fd6a8e14a5cf0274bf0cc078f0363 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 08:27:10 -1000 Subject: [PATCH 06/14] Fix lint --- src/libs/actions/IOU.ts | 58 ++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 9ee90a5090f0..30892bef5760 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2248,6 +2248,35 @@ function trackExpense( Report.notifyNewAction(activeReportID, payeeAccountID); } +function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, participants: Participant[], participantAccountIDs: number[], currentUserAccountID: number) { + const existingChatReportID = existingSplitChatReportID || participants[0].reportID; + let existingSplitChatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingChatReportID}`]; + if (!existingSplitChatReport) { + existingSplitChatReport = participants.length < 2 ? ReportUtils.getChatByParticipants(participantAccountIDs) : null; + } + let newChat: ReportUtils.OptimisticChatReport | EmptyObject = {}; + const allParticipantsAccountIDs = [...participantAccountIDs, currentUserAccountID]; + if (!existingSplitChatReport && participants.length > 1) { + newChat = ReportUtils.buildOptimisticChatReport( + allParticipantsAccountIDs, + '', + CONST.REPORT.CHAT_TYPE.GROUP, + undefined, + undefined, + undefined, + undefined, + undefined, + undefined, + CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, + ); + } + if (isEmptyObject(newChat)) { + newChat = ReportUtils.buildOptimisticChatReport(allParticipantsAccountIDs); + } + const splitChatReport = existingSplitChatReport ?? newChat; + return {splitChatReport, existingSplitChatReport}; +} + /** * Build the Onyx data and IOU split necessary for splitting a bill with 3+ users. * 1. Build the optimistic Onyx data for the group chat, i.e. chatReport and iouReportAction creating the former if it doesn't yet exist. @@ -2623,35 +2652,6 @@ function createSplitsAndOnyxData( }; } -function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, participants: Participant[], participantAccountIDs: number[], currentUserAccountID: number) { - const existingChatReportID = existingSplitChatReportID || participants[0].reportID; - let existingSplitChatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingChatReportID}`]; - if (!existingSplitChatReport) { - existingSplitChatReport = participants.length < 2 ? ReportUtils.getChatByParticipants(participantAccountIDs) : null; - } - let newChat: ReportUtils.OptimisticChatReport | EmptyObject = {}; - const allParticipantsAccountIDs = [...participantAccountIDs, currentUserAccountID]; - if (!existingSplitChatReport && participants.length > 1) { - newChat = ReportUtils.buildOptimisticChatReport( - allParticipantsAccountIDs, - '', - CONST.REPORT.CHAT_TYPE.GROUP, - undefined, - undefined, - undefined, - undefined, - undefined, - undefined, - CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, - ); - } - if (isEmptyObject(newChat)) { - newChat = ReportUtils.buildOptimisticChatReport(allParticipantsAccountIDs); - } - const splitChatReport = existingSplitChatReport ?? newChat; - return {splitChatReport, existingSplitChatReport}; -} - /** * @param amount - always in smallest currency unit * @param existingSplitChatReportID - Either a group DM or a workspace chat From d94613008dec9177cf32679aab88482a4c038a2e Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 12:19:01 -1000 Subject: [PATCH 07/14] Fix workspace avatar issue --- src/libs/ReportUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index b7c835d692ca..82ece08ef09c 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1193,7 +1193,7 @@ function hasOnlyTransactionsWithPendingRoutes(iouReportID: string | undefined): * If the report is a thread and has a chat type set, it is a workspace chat. */ function isWorkspaceThread(report: OnyxEntry): boolean { - return isThread(report) && isChatReport(report) && !!getChatType(report); + return isThread(report) && isChatReport(report) && !isGroupChat(report) && !!getChatType(report); } /** From 2de1bb225e1dfbe8ddca5af662bad4395aed5a1a Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 12:57:42 -1000 Subject: [PATCH 08/14] Fix issue for future threads as well --- src/CONST.ts | 3 +++ src/libs/ReportUtils.ts | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/CONST.ts b/src/CONST.ts index 0fa1c64be44d..f8a15351ee32 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -59,6 +59,9 @@ const cardActiveStates: number[] = [2, 3, 4, 7]; const CONST = { MERGED_ACCOUNT_PREFIX: 'MERGED_', DEFAULT_POLICY_ROOM_CHAT_TYPES: [chatTypes.POLICY_ADMINS, chatTypes.POLICY_ANNOUNCE, chatTypes.DOMAIN_ALL], + + // Note: Group excluded as it is not tied to any Workspace + WORKSPACE_ROOM_TYPES: [chatTypes.POLICY_ADMINS, chatTypes.POLICY_ANNOUNCE, chatTypes.DOMAIN_ALL, chatTypes.POLICY_ROOM, chatTypes.POLICY_EXPENSE_CHAT, chatTypes.SELF_DM], ANDROID_PACKAGE_NAME, ANIMATED_TRANSITION: 300, ANIMATED_TRANSITION_FROM_VALUE: 100, diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 82ece08ef09c..72eea831350c 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1193,7 +1193,7 @@ function hasOnlyTransactionsWithPendingRoutes(iouReportID: string | undefined): * If the report is a thread and has a chat type set, it is a workspace chat. */ function isWorkspaceThread(report: OnyxEntry): boolean { - return isThread(report) && isChatReport(report) && !isGroupChat(report) && !!getChatType(report); + return isThread(report) && isChatReport(report) && CONST.WORKSPACE_ROOM_TYPES.includes(getChatType(report)); } /** From a4225b8acd157bdb3548f9ea8afa79177b53011c Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 12:59:28 -1000 Subject: [PATCH 09/14] also self DM --- src/CONST.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index f8a15351ee32..f4c7ecb5215a 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -60,8 +60,8 @@ const CONST = { MERGED_ACCOUNT_PREFIX: 'MERGED_', DEFAULT_POLICY_ROOM_CHAT_TYPES: [chatTypes.POLICY_ADMINS, chatTypes.POLICY_ANNOUNCE, chatTypes.DOMAIN_ALL], - // Note: Group excluded as it is not tied to any Workspace - WORKSPACE_ROOM_TYPES: [chatTypes.POLICY_ADMINS, chatTypes.POLICY_ANNOUNCE, chatTypes.DOMAIN_ALL, chatTypes.POLICY_ROOM, chatTypes.POLICY_EXPENSE_CHAT, chatTypes.SELF_DM], + // Note: Group and Self-DM excluded as these are not tied to a Workspace + WORKSPACE_ROOM_TYPES: [chatTypes.POLICY_ADMINS, chatTypes.POLICY_ANNOUNCE, chatTypes.DOMAIN_ALL, chatTypes.POLICY_ROOM, chatTypes.POLICY_EXPENSE_CHAT], ANDROID_PACKAGE_NAME, ANIMATED_TRANSITION: 300, ANIMATED_TRANSITION_FROM_VALUE: 100, From 54ae619460300cad448d1f153deee9daa9308cc2 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 13:05:08 -1000 Subject: [PATCH 10/14] Fix ts --- src/libs/ReportUtils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 72eea831350c..6ff941771f94 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1193,7 +1193,8 @@ function hasOnlyTransactionsWithPendingRoutes(iouReportID: string | undefined): * If the report is a thread and has a chat type set, it is a workspace chat. */ function isWorkspaceThread(report: OnyxEntry): boolean { - return isThread(report) && isChatReport(report) && CONST.WORKSPACE_ROOM_TYPES.includes(getChatType(report)); + const chatType = getChatType(report) ?? ''; + return isThread(report) && isChatReport(report) && CONST.WORKSPACE_ROOM_TYPES.includes(chatType); } /** From cdc85349708da77bdda048af686cd6b9ebb8ca2a Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 13:18:33 -1000 Subject: [PATCH 11/14] typescript. is it smart? sometimes. --- src/libs/ReportUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 6ff941771f94..479569b35bc8 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1194,7 +1194,7 @@ function hasOnlyTransactionsWithPendingRoutes(iouReportID: string | undefined): */ function isWorkspaceThread(report: OnyxEntry): boolean { const chatType = getChatType(report) ?? ''; - return isThread(report) && isChatReport(report) && CONST.WORKSPACE_ROOM_TYPES.includes(chatType); + return isThread(report) && isChatReport(report) && typeof chatType === 'string' && CONST.WORKSPACE_ROOM_TYPES.includes(chatType); } /** From 8eced8e9f73f89b086b62e26ddd6ad4dff16eaf3 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 13:22:39 -1000 Subject: [PATCH 12/14] Use some instead to fool typescript --- src/libs/ReportUtils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 479569b35bc8..9ca43d67e0d3 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1194,7 +1194,7 @@ function hasOnlyTransactionsWithPendingRoutes(iouReportID: string | undefined): */ function isWorkspaceThread(report: OnyxEntry): boolean { const chatType = getChatType(report) ?? ''; - return isThread(report) && isChatReport(report) && typeof chatType === 'string' && CONST.WORKSPACE_ROOM_TYPES.includes(chatType); + return isThread(report) && isChatReport(report) && CONST.WORKSPACE_ROOM_TYPES.some(type => chatType === type); } /** From 10beb0ad7b5acc44685db4cda7d1764198e81500 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 13:32:41 -1000 Subject: [PATCH 13/14] Remove null coalesce since we are using some --- src/libs/ReportUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 9ca43d67e0d3..11dd86a32ed3 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1193,8 +1193,8 @@ function hasOnlyTransactionsWithPendingRoutes(iouReportID: string | undefined): * If the report is a thread and has a chat type set, it is a workspace chat. */ function isWorkspaceThread(report: OnyxEntry): boolean { - const chatType = getChatType(report) ?? ''; - return isThread(report) && isChatReport(report) && CONST.WORKSPACE_ROOM_TYPES.some(type => chatType === type); + const chatType = getChatType(report); + return isThread(report) && isChatReport(report) && CONST.WORKSPACE_ROOM_TYPES.some((type) => chatType === type); } /** From 04cfc03ba3e0e7ca59f9e370613a8471bb244910 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Fri, 29 Mar 2024 16:39:21 -1000 Subject: [PATCH 14/14] Clean up logic for creating Group Chats --- src/libs/actions/IOU.ts | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 30892bef5760..4702b7a18958 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -2249,15 +2249,30 @@ function trackExpense( } function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, participants: Participant[], participantAccountIDs: number[], currentUserAccountID: number) { + // The existing chat report could be passed as reportID or exist on the sole "participant" (in this case a report option) const existingChatReportID = existingSplitChatReportID || participants[0].reportID; + + // Check if the report is available locally if we do have one let existingSplitChatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${existingChatReportID}`]; - if (!existingSplitChatReport) { - existingSplitChatReport = participants.length < 2 ? ReportUtils.getChatByParticipants(participantAccountIDs) : null; + + // If we do not have one locally then we will search for a chat with the same participants (only for 1:1 chats). + const shouldGetOrCreateOneOneDM = participants.length < 2; + if (!existingSplitChatReport && shouldGetOrCreateOneOneDM) { + existingSplitChatReport = ReportUtils.getChatByParticipants(participantAccountIDs); + } + + // We found an existing chat report we are done... + if (existingSplitChatReport) { + // Yes, these are the same, but give the caller a way to identify if we created a new report or not + return {existingSplitChatReport, splitChatReport: existingSplitChatReport}; } - let newChat: ReportUtils.OptimisticChatReport | EmptyObject = {}; + + // No existing chat by this point we need to create it const allParticipantsAccountIDs = [...participantAccountIDs, currentUserAccountID]; - if (!existingSplitChatReport && participants.length > 1) { - newChat = ReportUtils.buildOptimisticChatReport( + + // Create a Group Chat if we have multiple participants + if (participants.length > 1) { + const splitChatReport = ReportUtils.buildOptimisticChatReport( allParticipantsAccountIDs, '', CONST.REPORT.CHAT_TYPE.GROUP, @@ -2269,12 +2284,12 @@ function getOrCreateOptimisticSplitChatReport(existingSplitChatReportID: string, undefined, CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, ); + return {existingSplitChatReport: null, splitChatReport}; } - if (isEmptyObject(newChat)) { - newChat = ReportUtils.buildOptimisticChatReport(allParticipantsAccountIDs); - } - const splitChatReport = existingSplitChatReport ?? newChat; - return {splitChatReport, existingSplitChatReport}; + + // Otherwise, create a new 1:1 chat report + const splitChatReport = ReportUtils.buildOptimisticChatReport(allParticipantsAccountIDs); + return {existingSplitChatReport: null, splitChatReport}; } /**