Skip to content

Commit 8a56bca

Browse files
authored
Merge pull request #73208 from software-mansion-labs/jnowakow/556957-remove-unused-params
Use existing selfDM report
2 parents 5d93183 + db22b6a commit 8a56bca

14 files changed

Lines changed: 110 additions & 37 deletions

src/ONYXKEYS.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ const ONYXKEYS = {
132132
/** Whether the user is a member of a policy other than their personal */
133133
HAS_NON_PERSONAL_POLICY: 'hasNonPersonalPolicy',
134134

135+
/** Key under which selfDM id is stored. Returned by OpenApp */
136+
SELF_DM_REPORT_ID: 'selfDMReportID',
137+
135138
/** NVP keys */
136139

137140
/** This NVP contains list of at most 5 recent attendees */
@@ -1297,6 +1300,7 @@ type OnyxValuesMapping = {
12971300
[ONYXKEYS.NVP_CSV_EXPORT_LAYOUTS]: Record<string, OnyxTypes.ExportTemplate>;
12981301
[ONYXKEYS.NVP_LAST_DISTANCE_EXPENSE_TYPE]: DistanceExpenseType;
12991302
[ONYXKEYS.HAS_DENIED_CONTACT_IMPORT_PROMPT]: boolean | undefined;
1303+
[ONYXKEYS.SELF_DM_REPORT_ID]: string | undefined;
13001304
};
13011305

13021306
type OnyxDerivedValuesMapping = {

src/libs/ReportUtils.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,17 @@ Onyx.connect({
12071207
},
12081208
});
12091209

1210+
let selfDMReportID: string | undefined;
1211+
Onyx.connectWithoutView({
1212+
key: ONYXKEYS.SELF_DM_REPORT_ID,
1213+
callback: (value) => {
1214+
if (!value) {
1215+
return;
1216+
}
1217+
selfDMReportID = value;
1218+
},
1219+
});
1220+
12101221
function getCurrentUserAvatar(): AvatarSource | undefined {
12111222
return currentUserPersonalDetails?.avatar;
12121223
}
@@ -1857,7 +1868,12 @@ function findSelfDMReportID(): string | undefined {
18571868
return;
18581869
}
18591870

1871+
if (selfDMReportID) {
1872+
return selfDMReportID;
1873+
}
1874+
18601875
const selfDMReport = Object.values(allReports).find((report) => isSelfDM(report) && !isThread(report));
1876+
18611877
return selfDMReport?.reportID;
18621878
}
18631879

@@ -2480,9 +2496,8 @@ function isIOURequest(report: OnyxInputOrEntry<Report>): boolean {
24802496
*/
24812497
function isTrackExpenseReport(report: OnyxInputOrEntry<Report>): boolean {
24822498
if (isThread(report)) {
2483-
const selfDMReportID = findSelfDMReportID();
24842499
const parentReportAction = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`]?.[report.parentReportActionID];
2485-
return !isEmptyObject(parentReportAction) && selfDMReportID === report.parentReportID && isTrackExpenseAction(parentReportAction);
2500+
return !isEmptyObject(parentReportAction) && findSelfDMReportID() === report.parentReportID && isTrackExpenseAction(parentReportAction);
24862501
}
24872502
return false;
24882503
}
@@ -6469,7 +6484,7 @@ function buildOptimisticTaskCommentReportAction(
64696484

64706485
function buildOptimisticSelfDMReport(created: string): Report {
64716486
return {
6472-
reportID: generateReportID(),
6487+
reportID: findSelfDMReportID() ?? generateReportID(),
64736488
participants: {
64746489
[currentUserAccountID ?? CONST.DEFAULT_NUMBER_ID]: {
64756490
notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.MUTE,
@@ -6832,8 +6847,7 @@ function getMovedTransactionMessage(report: OnyxEntry<Report>) {
68326847
}
68336848

68346849
function getUnreportedTransactionMessage() {
6835-
const selfDMReportID = findSelfDMReportID();
6836-
const reportUrl = `${environmentURL}/r/${selfDMReportID}`;
6850+
const reportUrl = `${environmentURL}/r/${findSelfDMReportID()}`;
68376851
// eslint-disable-next-line @typescript-eslint/no-deprecated
68386852
const message = translateLocal('iou.unreportedTransaction', {
68396853
reportUrl,
@@ -11690,8 +11704,7 @@ function prepareOnboardingOnyxData(
1169011704

1169111705
let selfDMParameters: SelfDMParameters = {};
1169211706
if (engagementChoice === CONST.ONBOARDING_CHOICES.PERSONAL_SPEND || engagementChoice === CONST.ONBOARDING_CHOICES.TRACK_WORKSPACE) {
11693-
const selfDMReportID = findSelfDMReportID();
11694-
let selfDMReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${selfDMReportID}`];
11707+
let selfDMReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${findSelfDMReportID()}`];
1169511708
let createdAction: ReportAction;
1169611709
if (!selfDMReport) {
1169711710
const currentTime = DateUtils.getDBTime();

src/libs/actions/Transaction.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
buildOptimisticUnHoldReportAction,
2323
buildOptimisticUnreportedTransactionAction,
2424
buildTransactionThread,
25-
findSelfDMReportID,
2625
getReportTransactions,
2726
getTransactionDetails,
2827
hasViolations as hasViolationsReportUtils,
@@ -701,6 +700,7 @@ function changeTransactionsReport(
701700
policy?: OnyxEntry<Policy>,
702701
reportNextStep?: OnyxEntry<ReportNextStepDeprecated>,
703702
policyCategories?: OnyxEntry<PolicyCategories>,
703+
selfDMReportIDParam?: string,
704704
) {
705705
const reportID = newReport?.reportID ?? CONST.REPORT.UNREPORTED_REPORT_ID;
706706

@@ -720,15 +720,16 @@ function changeTransactionsReport(
720720
const failureData: OnyxUpdate[] = [];
721721
const successData: OnyxUpdate[] = [];
722722

723-
const existingSelfDMReportID = findSelfDMReportID();
724-
let selfDMReport: Report | undefined;
723+
let selfDMReportID = selfDMReportIDParam;
724+
let selfDMReport: Report | undefined = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${selfDMReportID}`];
725725
let selfDMCreatedReportAction: ReportAction | undefined;
726726
const currentUserAccountID = getCurrentUserAccountID();
727727

728-
if (!existingSelfDMReportID && reportID === CONST.REPORT.UNREPORTED_REPORT_ID) {
728+
if (!selfDMReport && reportID === CONST.REPORT.UNREPORTED_REPORT_ID) {
729729
const currentTime = DateUtils.getDBTime();
730730
selfDMReport = buildOptimisticSelfDMReport(currentTime);
731731
selfDMCreatedReportAction = buildOptimisticCreatedReportAction(email ?? '', currentTime);
732+
selfDMReportID = selfDMReport.reportID;
732733

733734
// Add optimistic updates for self DM report
734735
optimisticData.push(
@@ -811,8 +812,6 @@ function changeTransactionsReport(
811812
for (const transaction of transactions) {
812813
const isUnreportedExpense = !transaction.reportID || transaction.reportID === CONST.REPORT.UNREPORTED_REPORT_ID;
813814

814-
const selfDMReportID = existingSelfDMReportID ?? selfDMReport?.reportID;
815-
816815
const oldIOUAction = getIOUActionForReportID(isUnreportedExpense ? selfDMReportID : transaction.reportID, transaction.transactionID);
817816
if (!transaction.reportID || transaction.reportID === reportID) {
818817
continue;
@@ -1179,11 +1178,11 @@ function changeTransactionsReport(
11791178
: {}),
11801179
};
11811180

1182-
if (!existingSelfDMReportID && reportID === CONST.REPORT.UNREPORTED_REPORT_ID && selfDMReport && selfDMCreatedReportAction) {
1181+
if (reportID === CONST.REPORT.UNREPORTED_REPORT_ID && selfDMReport && selfDMCreatedReportAction) {
11831182
// Add self DM data to transaction data
11841183
transactionIDToReportActionAndThreadData[transaction.transactionID] = {
11851184
...baseTransactionData,
1186-
selfDMReportID: selfDMReport.reportID,
1185+
selfDMReportID,
11871186
selfDMCreatedReportActionID: selfDMCreatedReportAction.reportActionID,
11881187
};
11891188
} else {
@@ -1295,7 +1294,7 @@ function changeTransactionsReport(
12951294
}
12961295

12971296
// 9. Update next step for report
1298-
const destinationReportID = reportID === CONST.REPORT.UNREPORTED_REPORT_ID ? (existingSelfDMReportID ?? selfDMReport?.reportID) : reportID;
1297+
const destinationReportID = reportID === CONST.REPORT.UNREPORTED_REPORT_ID ? selfDMReportID : reportID;
12991298
const destinationReport = destinationReportID
13001299
? (allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${destinationReportID}`] ??
13011300
(destinationReportID === newReport?.reportID ? newReport : undefined) ??

src/pages/AddUnreportedExpense.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
2424
import interceptAnonymousUser from '@libs/interceptAnonymousUser';
2525
import type {AddUnreportedExpensesParamList} from '@libs/Navigation/types';
2626
import {canSubmitPerDiemExpenseFromWorkspace, getPerDiemCustomUnit} from '@libs/PolicyUtils';
27-
import {getTransactionDetails, isIOUReport} from '@libs/ReportUtils';
27+
import {findSelfDMReportID, getTransactionDetails, isIOUReport} from '@libs/ReportUtils';
2828
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
2929
import tokenizedSearch from '@libs/tokenizedSearch';
3030
import {createUnreportedExpenseSections, getAmount, getCurrency, getDescription, getMerchant, isPerDiemRequest} from '@libs/TransactionUtils';
@@ -61,6 +61,7 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) {
6161
const {isBetaEnabled} = usePermissions();
6262
const isASAPSubmitBetaEnabled = isBetaEnabled(CONST.BETAS.ASAP_SUBMIT);
6363
const session = useSession();
64+
const selfDMReportID = useMemo(() => findSelfDMReportID(), []);
6465
const shouldShowUnreportedTransactionsSkeletons = isLoadingUnreportedTransactions && hasMoreUnreportedTransactionsResults && !isOffline;
6566

6667
const getUnreportedTransactions = useCallback(
@@ -181,6 +182,7 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) {
181182
policy,
182183
reportNextStep,
183184
policyCategories,
185+
selfDMReportID,
184186
);
185187
}
186188
});

src/pages/NewReportWorkspaceSelectionPage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Navigation from '@libs/Navigation/Navigation';
2525
import type {NewReportWorkspaceSelectionNavigatorParamList} from '@libs/Navigation/types';
2626
import {getHeaderMessageForNonUserList} from '@libs/OptionsListUtils';
2727
import {canSubmitPerDiemExpenseFromWorkspace, isPolicyAdmin, shouldShowPolicy} from '@libs/PolicyUtils';
28-
import {getDefaultWorkspaceAvatar, getPolicyIDsWithEmptyReportsForAccount, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
28+
import {findSelfDMReportID, getDefaultWorkspaceAvatar, getPolicyIDsWithEmptyReportsForAccount, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
2929
import {buildCannedSearchQuery} from '@libs/SearchQueryUtils';
3030
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
3131
import {isPerDiemRequest} from '@libs/TransactionUtils';
@@ -72,6 +72,7 @@ function NewReportWorkspaceSelectionPage({route}: NewReportWorkspaceSelectionPag
7272
const shouldShowLoadingIndicator = isLoadingApp && !isOffline;
7373
const [pendingPolicySelection, setPendingPolicySelection] = useState<{policy: WorkspaceListItem; shouldShowEmptyReportConfirmation: boolean} | null>(null);
7474
const [accountID] = useOnyx(ONYXKEYS.SESSION, {selector: accountIDSelector, canBeMissing: true});
75+
const selfDMReportID = useMemo(() => findSelfDMReportID(), []);
7576

7677
const policiesWithEmptyReportsSelector = useCallback(
7778
(reports: OnyxCollection<OnyxTypes.Report>) => {
@@ -125,6 +126,7 @@ function NewReportWorkspaceSelectionPage({route}: NewReportWorkspaceSelectionPag
125126
policies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`],
126127
reportNextStep,
127128
undefined,
129+
selfDMReportID,
128130
);
129131

130132
// eslint-disable-next-line rulesdir/no-default-id-values

src/pages/ProfilePage.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-restricted-imports */
12
import {Str} from 'expensify-common';
23
import React, {useEffect, useMemo} from 'react';
34
import {View} from 'react-native';
@@ -74,17 +75,18 @@ function ProfilePage({route}: ProfilePageProps) {
7475
const [account] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: true});
7576
const [isDebugModeEnabled = false] = useOnyx(ONYXKEYS.IS_DEBUG_MODE_ENABLED, {canBeMissing: true});
7677
const guideCalendarLink = account?.guideDetails?.calendarLink ?? '';
78+
const selfDMReportID = useMemo(() => findSelfDMReportID(), []);
7779

7880
const accountID = Number(route.params?.accountID ?? CONST.DEFAULT_NUMBER_ID);
7981
const isCurrentUser = session?.accountID === accountID;
8082
const reportKey = useMemo(() => {
81-
const reportID = isCurrentUser ? findSelfDMReportID() : getChatByParticipants(session?.accountID ? [accountID, session.accountID] : [], reports)?.reportID;
83+
const reportID = isCurrentUser ? selfDMReportID : getChatByParticipants(session?.accountID ? [accountID, session.accountID] : [], reports)?.reportID;
8284

8385
if (isAnonymousUserSession() || !reportID) {
8486
return `${ONYXKEYS.COLLECTION.REPORT}0` as const;
8587
}
8688
return `${ONYXKEYS.COLLECTION.REPORT}${reportID}` as const;
87-
}, [accountID, isCurrentUser, reports, session]);
89+
}, [accountID, selfDMReportID, isCurrentUser, reports, session]);
8890
const [report] = useOnyx(reportKey, {canBeMissing: true});
8991

9092
const styles = useThemeStyles();

src/pages/Search/SearchTransactionsChangeReport.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {createNewReport} from '@libs/actions/Report';
1212
import {changeTransactionsReport} from '@libs/actions/Transaction';
1313
import setNavigationActionToMicrotaskQueue from '@libs/Navigation/helpers/setNavigationActionToMicrotaskQueue';
1414
import Navigation from '@libs/Navigation/Navigation';
15-
import {getReportOrDraftReport, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
15+
import {findSelfDMReportID, getReportOrDraftReport, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
1616
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
1717
import IOURequestEditReportCommon from '@pages/iou/request/step/IOURequestEditReportCommon';
1818
import CONST from '@src/CONST';
@@ -39,6 +39,7 @@ function SearchTransactionsChangeReport() {
3939
const session = useSession();
4040
const currentUserPersonalDetails = useCurrentUserPersonalDetails();
4141
const policyForMovingExpenses = policyForMovingExpensesID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyForMovingExpensesID}`] : undefined;
42+
const selfDMReportID = useMemo(() => findSelfDMReportID(), []);
4243

4344
const firstTransactionKey = selectedTransactionsKeys.at(0);
4445
const firstTransactionReportID = firstTransactionKey ? selectedTransactions[firstTransactionKey]?.reportID : undefined;
@@ -84,6 +85,7 @@ function SearchTransactionsChangeReport() {
8485
policyForMovingExpensesID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyForMovingExpensesID}`] : undefined,
8586
reportNextStep,
8687
undefined,
88+
selfDMReportID,
8789
);
8890
clearSelectedTransactions();
8991
});
@@ -125,6 +127,7 @@ function SearchTransactionsChangeReport() {
125127
allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${item.policyID}`],
126128
reportNextStep,
127129
allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${item.policyID}`],
130+
selfDMReportID,
128131
);
129132
// eslint-disable-next-line @typescript-eslint/no-deprecated
130133
InteractionManager.runAfterInteractions(() => {
@@ -138,7 +141,17 @@ function SearchTransactionsChangeReport() {
138141
if (selectedTransactionsKeys.length === 0) {
139142
return;
140143
}
141-
changeTransactionsReport(selectedTransactionsKeys, isASAPSubmitBetaEnabled, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? '');
144+
changeTransactionsReport(
145+
selectedTransactionsKeys,
146+
isASAPSubmitBetaEnabled,
147+
session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
148+
session?.email ?? '',
149+
undefined,
150+
undefined,
151+
undefined,
152+
undefined,
153+
selfDMReportID,
154+
);
142155
clearSelectedTransactions();
143156
Navigation.goBack();
144157
};

src/pages/TrackExpensePage.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import useOnyx from '@hooks/useOnyx';
99
import useThemeStyles from '@hooks/useThemeStyles';
1010
import interceptAnonymousUser from '@libs/interceptAnonymousUser';
1111
import Navigation from '@libs/Navigation/Navigation';
12-
import * as ReportUtils from '@libs/ReportUtils';
13-
import * as App from '@userActions/App';
14-
import * as IOU from '@userActions/IOU';
12+
import {findSelfDMReportID, generateReportID} from '@libs/ReportUtils';
13+
import {confirmReadyToOpenApp} from '@userActions/App';
14+
import {startMoneyRequest} from '@userActions/IOU';
1515
import CONST from '@src/CONST';
1616
import ONYXKEYS from '@src/ONYXKEYS';
1717
import ROUTES from '@src/ROUTES';
@@ -26,21 +26,21 @@ function TrackExpensePage() {
2626
const styles = useThemeStyles();
2727
const isUnmounted = useRef(false);
2828
const {isOffline} = useNetwork();
29-
const [hasSeenTrackTraining, hasSeenTrackTrainingResult] = useOnyx(ONYXKEYS.NVP_HAS_SEEN_TRACK_TRAINING);
29+
const [hasSeenTrackTraining, hasSeenTrackTrainingResult] = useOnyx(ONYXKEYS.NVP_HAS_SEEN_TRACK_TRAINING, {canBeMissing: true});
3030
const isLoadingHasSeenTrackTraining = isLoadingOnyxValue(hasSeenTrackTrainingResult);
3131

3232
useFocusEffect(() => {
3333
interceptAnonymousUser(() => {
34-
App.confirmReadyToOpenApp();
34+
confirmReadyToOpenApp();
3535
Navigation.isNavigationReady().then(() => {
3636
if (isUnmounted.current || isLoadingHasSeenTrackTraining) {
3737
return;
3838
}
3939
Navigation.goBack();
40-
IOU.startMoneyRequest(
40+
startMoneyRequest(
4141
CONST.IOU.TYPE.TRACK,
4242
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
43-
ReportUtils.findSelfDMReportID() || ReportUtils.generateReportID(),
43+
findSelfDMReportID() || generateReportID(),
4444
);
4545

4646
if (!hasSeenTrackTraining && !isOffline) {

src/pages/iou/request/step/IOURequestEditReport.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
1212
import {changeTransactionsReport} from '@libs/actions/Transaction';
1313
import setNavigationActionToMicrotaskQueue from '@libs/Navigation/helpers/setNavigationActionToMicrotaskQueue';
1414
import Navigation from '@libs/Navigation/Navigation';
15-
import {hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
15+
import {findSelfDMReportID, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils';
1616
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
1717
import {isPerDiemRequest} from '@libs/TransactionUtils';
1818
import {createNewReport} from '@userActions/Report';
@@ -58,6 +58,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) {
5858
const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: true});
5959
const hasViolations = hasViolationsReportUtils(undefined, transactionViolations);
6060
const policyForMovingExpenses = policyForMovingExpensesID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyForMovingExpensesID}`] : undefined;
61+
const selfDMReportID = useMemo(() => findSelfDMReportID(), []);
6162

6263
const selectReport = (item: TransactionGroupListItem, report?: OnyxEntry<Report>) => {
6364
if (selectedTransactionIDs.length === 0 || item.value === reportID) {
@@ -77,6 +78,7 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) {
7778
allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${item.policyID}`],
7879
reportNextStep,
7980
allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${item.policyID}`],
81+
selfDMReportID,
8082
);
8183
turnOffMobileSelectionMode();
8284
clearSelectedTransactions(true);
@@ -89,7 +91,17 @@ function IOURequestEditReport({route}: IOURequestEditReportProps) {
8991
if (!selectedReport || selectedTransactionIDs.length === 0) {
9092
return;
9193
}
92-
changeTransactionsReport(selectedTransactionIDs, isASAPSubmitBetaEnabled, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? '');
94+
changeTransactionsReport(
95+
selectedTransactionIDs,
96+
isASAPSubmitBetaEnabled,
97+
session?.accountID ?? CONST.DEFAULT_NUMBER_ID,
98+
session?.email ?? '',
99+
undefined,
100+
undefined,
101+
undefined,
102+
undefined,
103+
selfDMReportID,
104+
);
93105
if (shouldTurnOffSelectionMode) {
94106
turnOffMobileSelectionMode();
95107
}

0 commit comments

Comments
 (0)