Skip to content

Commit 48b80f6

Browse files
authored
Merge pull request #53203 from callstack-internal/refactor/migrate-isOptimisticReport-to-metadata
refactor: move isOptimisticReport to reportMetadata
2 parents fb8be4d + a9fa3eb commit 48b80f6

16 files changed

+352
-113
lines changed

src/libs/DebugUtils.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ function validateReportDraftProperty(key: keyof Report, value: string) {
477477
case 'isPinned':
478478
case 'hasParentAccess':
479479
case 'isDeletedParentAction':
480-
case 'isOptimisticReport':
481480
case 'isWaitingOnBankAccount':
482481
case 'isCancelledIOU':
483482
return validateBoolean(value);
@@ -606,7 +605,6 @@ function validateReportDraftProperty(key: keyof Report, value: string) {
606605
lastMessageTranslationKey: CONST.RED_BRICK_ROAD_PENDING_ACTION,
607606
parentReportID: CONST.RED_BRICK_ROAD_PENDING_ACTION,
608607
parentReportActionID: CONST.RED_BRICK_ROAD_PENDING_ACTION,
609-
isOptimisticReport: CONST.RED_BRICK_ROAD_PENDING_ACTION,
610608
managerID: CONST.RED_BRICK_ROAD_PENDING_ACTION,
611609
lastVisibleActionLastModified: CONST.RED_BRICK_ROAD_PENDING_ACTION,
612610
lastMessageHtml: CONST.RED_BRICK_ROAD_PENDING_ACTION,

src/libs/ReportUtils.ts

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,7 @@ type OptimisticChatReport = Pick<
331331
| 'writeCapability'
332332
| 'avatarUrl'
333333
| 'invoiceReceiver'
334-
> & {
335-
isOptimisticReport: true;
336-
};
334+
>;
337335

338336
type OptimisticExportIntegrationAction = OriginalMessageExportedToIntegration &
339337
Pick<
@@ -5457,7 +5455,6 @@ function buildOptimisticChatReport(
54575455
const currentTime = DateUtils.getDBTime();
54585456
const isNewlyCreatedWorkspaceChat = chatType === CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT && isOwnPolicyExpenseChat;
54595457
const optimisticChatReport: OptimisticChatReport = {
5460-
isOptimisticReport: true,
54615458
type: CONST.REPORT.TYPE.CHAT,
54625459
chatType,
54635460
isOwnPolicyExpenseChat,
@@ -5964,6 +5961,12 @@ function buildOptimisticAnnounceChat(policyID: string, accountIDs: number[]): Op
59645961
addWorkspaceRoom: null,
59655962
},
59665963
pendingAction: null,
5964+
},
5965+
},
5966+
{
5967+
onyxMethod: Onyx.METHOD.MERGE,
5968+
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${announceChatData.reportID}`,
5969+
value: {
59675970
isOptimisticReport: false,
59685971
},
59695972
},
@@ -5986,6 +5989,12 @@ function buildOptimisticAnnounceChat(policyID: string, accountIDs: number[]): Op
59865989
addWorkspaceRoom: null,
59875990
},
59885991
pendingAction: null,
5992+
},
5993+
},
5994+
{
5995+
onyxMethod: Onyx.METHOD.MERGE,
5996+
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${announceChatData.reportID}`,
5997+
value: {
59895998
isOptimisticReport: false,
59905999
},
59916000
},
@@ -6159,7 +6168,6 @@ function buildTransactionThread(
61596168
if (existingTransactionThreadReportID && existingTransactionThreadReport) {
61606169
return {
61616170
...existingTransactionThreadReport,
6162-
isOptimisticReport: true,
61636171
parentReportActionID: reportAction?.reportActionID,
61646172
parentReportID: moneyRequestReport?.reportID,
61656173
reportName: getTransactionReportName(reportAction),
@@ -7390,14 +7398,15 @@ function getTaskAssigneeChatOnyxData(
73907398
let optimisticAssigneeAddComment: OptimisticReportAction | undefined;
73917399
// Set if this is a new chat that needs to be created for the assignee
73927400
let optimisticChatCreatedReportAction: OptimisticCreatedReportAction | undefined;
7401+
const assigneeChatReportMetadata = getReportMetadata(assigneeChatReportID);
73937402
const currentTime = DateUtils.getDBTime();
73947403
const optimisticData: OnyxUpdate[] = [];
73957404
const successData: OnyxUpdate[] = [];
73967405
const failureData: OnyxUpdate[] = [];
73977406

73987407
// You're able to assign a task to someone you haven't chatted with before - so we need to optimistically create the chat and the chat reportActions
73997408
// Only add the assignee chat report to onyx if we haven't already set it optimistically
7400-
if (assigneeChatReport?.isOptimisticReport && assigneeChatReport.pendingFields?.createChat !== CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) {
7409+
if (assigneeChatReportMetadata?.isOptimisticReport && assigneeChatReport?.pendingFields?.createChat !== CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) {
74017410
optimisticChatCreatedReportAction = buildOptimisticCreatedReportAction(assigneeChatReportID);
74027411
optimisticData.push(
74037412
{
@@ -7416,18 +7425,26 @@ function getTaskAssigneeChatOnyxData(
74167425
},
74177426
);
74187427

7419-
successData.push({
7420-
onyxMethod: Onyx.METHOD.MERGE,
7421-
key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`,
7422-
value: {
7423-
pendingFields: {
7424-
createChat: null,
7428+
successData.push(
7429+
{
7430+
onyxMethod: Onyx.METHOD.MERGE,
7431+
key: `${ONYXKEYS.COLLECTION.REPORT}${assigneeChatReportID}`,
7432+
value: {
7433+
pendingFields: {
7434+
createChat: null,
7435+
},
7436+
// BE will send a different participant. We clear the optimistic one to avoid duplicated entries
7437+
participants: {[assigneeAccountID]: null},
74257438
},
7426-
isOptimisticReport: false,
7427-
// BE will send a different participant. We clear the optimistic one to avoid duplicated entries
7428-
participants: {[assigneeAccountID]: null},
74297439
},
7430-
});
7440+
{
7441+
onyxMethod: Onyx.METHOD.MERGE,
7442+
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${assigneeChatReportID}`,
7443+
value: {
7444+
isOptimisticReport: false,
7445+
},
7446+
},
7447+
);
74317448

74327449
failureData.push(
74337450
{
@@ -8457,6 +8474,10 @@ function hasInvoiceReports() {
84578474
return allReports.some((report) => isInvoiceReport(report));
84588475
}
84598476

8477+
function getReportMetadata(reportID?: string) {
8478+
return allReportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportID}`];
8479+
}
8480+
84608481
export {
84618482
addDomainToShortMention,
84628483
completeShortMention,
@@ -8781,6 +8802,7 @@ export {
87818802
getAllReportErrors,
87828803
getAllReportActionsErrorsAndReportActionThatRequiresAttention,
87838804
hasInvoiceReports,
8805+
getReportMetadata,
87848806
};
87858807

87868808
export type {

src/libs/actions/IOU.ts

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -746,16 +746,24 @@ function buildOnyxDataForMoneyRequest(
746746
}
747747

748748
if (isNewChatReport) {
749-
successData.push({
750-
onyxMethod: Onyx.METHOD.MERGE,
751-
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`,
752-
value: {
753-
participants: redundantParticipants,
754-
pendingFields: null,
755-
errorFields: null,
756-
isOptimisticReport: false,
749+
successData.push(
750+
{
751+
onyxMethod: Onyx.METHOD.MERGE,
752+
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`,
753+
value: {
754+
participants: redundantParticipants,
755+
pendingFields: null,
756+
errorFields: null,
757+
},
757758
},
758-
});
759+
{
760+
onyxMethod: Onyx.METHOD.MERGE,
761+
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${chatReport?.reportID}`,
762+
value: {
763+
isOptimisticReport: false,
764+
},
765+
},
766+
);
759767
}
760768

761769
successData.push(
@@ -766,6 +774,12 @@ function buildOnyxDataForMoneyRequest(
766774
participants: redundantParticipants,
767775
pendingFields: null,
768776
errorFields: null,
777+
},
778+
},
779+
{
780+
onyxMethod: Onyx.METHOD.MERGE,
781+
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${iouReport.reportID}`,
782+
value: {
769783
isOptimisticReport: false,
770784
},
771785
},
@@ -776,6 +790,12 @@ function buildOnyxDataForMoneyRequest(
776790
participants: redundantParticipants,
777791
pendingFields: null,
778792
errorFields: null,
793+
},
794+
},
795+
{
796+
onyxMethod: Onyx.METHOD.MERGE,
797+
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${transactionThreadReport?.reportID}`,
798+
value: {
779799
isOptimisticReport: false,
780800
},
781801
},
@@ -1125,6 +1145,12 @@ function buildOnyxDataForInvoice(
11251145
participants: redundantParticipants,
11261146
pendingFields: null,
11271147
errorFields: null,
1148+
},
1149+
},
1150+
{
1151+
onyxMethod: Onyx.METHOD.MERGE,
1152+
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${iouReport.reportID}`,
1153+
value: {
11281154
isOptimisticReport: false,
11291155
},
11301156
},
@@ -1135,6 +1161,12 @@ function buildOnyxDataForInvoice(
11351161
participants: redundantParticipants,
11361162
pendingFields: null,
11371163
errorFields: null,
1164+
},
1165+
},
1166+
{
1167+
onyxMethod: Onyx.METHOD.MERGE,
1168+
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${transactionThreadReport.reportID}`,
1169+
value: {
11381170
isOptimisticReport: false,
11391171
},
11401172
},
@@ -1190,16 +1222,24 @@ function buildOnyxDataForInvoice(
11901222
);
11911223

11921224
if (isNewChatReport) {
1193-
successData.push({
1194-
onyxMethod: Onyx.METHOD.MERGE,
1195-
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`,
1196-
value: {
1197-
participants: redundantParticipants,
1198-
pendingFields: null,
1199-
errorFields: null,
1200-
isOptimisticReport: false,
1225+
successData.push(
1226+
{
1227+
onyxMethod: Onyx.METHOD.MERGE,
1228+
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport?.reportID}`,
1229+
value: {
1230+
participants: redundantParticipants,
1231+
pendingFields: null,
1232+
errorFields: null,
1233+
},
12011234
},
1202-
});
1235+
{
1236+
onyxMethod: Onyx.METHOD.MERGE,
1237+
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${chatReport?.reportID}`,
1238+
value: {
1239+
isOptimisticReport: false,
1240+
},
1241+
},
1242+
);
12031243
}
12041244

12051245
const errorKey = DateUtils.getMicroseconds();
@@ -1562,6 +1602,12 @@ function buildOnyxDataForTrackExpense(
15621602
value: {
15631603
pendingFields: null,
15641604
errorFields: null,
1605+
},
1606+
},
1607+
{
1608+
onyxMethod: Onyx.METHOD.MERGE,
1609+
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${transactionThreadReport?.reportID}`,
1610+
value: {
15651611
isOptimisticReport: false,
15661612
},
15671613
},
@@ -6245,6 +6291,12 @@ function getSendMoneyParams(
62456291
key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticTransactionThread.reportID}`,
62466292
value: {
62476293
participants: redundantParticipants,
6294+
},
6295+
},
6296+
{
6297+
onyxMethod: Onyx.METHOD.MERGE,
6298+
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${optimisticTransactionThread.reportID}`,
6299+
value: {
62486300
isOptimisticReport: false,
62496301
},
62506302
},
@@ -6264,7 +6316,7 @@ function getSendMoneyParams(
62646316
},
62656317
{
62666318
onyxMethod: Onyx.METHOD.MERGE,
6267-
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`,
6319+
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${chatReport.reportID}`,
62686320
value: {
62696321
isOptimisticReport: false,
62706322
},

0 commit comments

Comments
 (0)