Skip to content

Commit d410151

Browse files
committed
fix: refactoring
1 parent 0cf5cae commit d410151

8 files changed

Lines changed: 250 additions & 42 deletions

File tree

src/components/ReportActionItem/MoneyRequestReceiptView.tsx

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {hasSeenTourSelector} from '@selectors/Onboarding';
22
import mapValues from 'lodash/mapValues';
3-
import React, {useEffect, useMemo, useRef, useState} from 'react';
3+
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
44
import {View} from 'react-native';
55
import type {StyleProp, ViewStyle} from 'react-native';
66
import type {OnyxEntry} from 'react-native-onyx';
@@ -9,7 +9,6 @@ import AttachmentPicker from '@components/AttachmentPicker';
99
import Icon from '@components/Icon';
1010
import {ModalActions} from '@components/Modal/Global/ModalContext';
1111
import OfflineWithFeedback from '@components/OfflineWithFeedback';
12-
import {usePersonalDetails} from '@components/OnyxListItemProvider';
1312
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
1413
import PressableWithoutFocus from '@components/Pressable/PressableWithoutFocus';
1514
import ReceiptAudit, {ReceiptAuditMessages} from '@components/ReceiptAudit';
@@ -71,6 +70,7 @@ import CONST from '@src/CONST';
7170
import ONYXKEYS from '@src/ONYXKEYS';
7271
import ROUTES from '@src/ROUTES';
7372
import type * as OnyxTypes from '@src/types/onyx';
73+
import type PersonalDetails from '@src/types/onyx/PersonalDetails';
7474
import type {TransactionPendingFieldsKey} from '@src/types/onyx/Transaction';
7575
import type {FileObject} from '@src/types/utils/Attachment';
7676
import {isEmptyObject} from '@src/types/utils/EmptyObject';
@@ -112,6 +112,13 @@ const receiptImageViolationNames = new Set<OnyxTypes.ViolationName>([
112112

113113
const receiptFieldViolationNames = new Set<OnyxTypes.ViolationName>([CONST.VIOLATIONS.MODIFIED_AMOUNT, CONST.VIOLATIONS.MODIFIED_DATE]);
114114

115+
const findConciergePersonalDetail = (list: OnyxEntry<OnyxTypes.PersonalDetailsList>): OnyxEntry<PersonalDetails> => {
116+
if (!list) {
117+
return undefined;
118+
}
119+
return Object.values(list).find((detail): detail is PersonalDetails => detail?.login === CONST.EMAIL.CONCIERGE);
120+
};
121+
115122
function MoneyRequestReceiptView({
116123
report,
117124
readonly = false,
@@ -134,7 +141,17 @@ function MoneyRequestReceiptView({
134141
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED);
135142
const [isSelfTourViewed] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {selector: hasSeenTourSelector});
136143
const [betas] = useOnyx(ONYXKEYS.BETAS);
137-
const personalDetails = usePersonalDetails();
144+
const [conciergePersonalDetail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: findConciergePersonalDetail});
145+
const reportOwnerSelector = useCallback(
146+
(list: OnyxEntry<OnyxTypes.PersonalDetailsList>): OnyxEntry<PersonalDetails> => (report?.ownerAccountID ? (list?.[report.ownerAccountID] ?? undefined) : undefined),
147+
[report?.ownerAccountID],
148+
);
149+
const [reportOwnerPersonalDetail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: reportOwnerSelector}, [reportOwnerSelector]);
150+
const chatReportOwnerSelector = useCallback(
151+
(list: OnyxEntry<OnyxTypes.PersonalDetailsList>): OnyxEntry<PersonalDetails> => (chatReport?.ownerAccountID ? (list?.[chatReport.ownerAccountID] ?? undefined) : undefined),
152+
[chatReport?.ownerAccountID],
153+
);
154+
const [chatReportOwnerPersonalDetail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: chatReportOwnerSelector}, [chatReportOwnerSelector]);
138155
const delegateAccountID = useDelegateAccountID();
139156

140157
const [isLoading, setIsLoading] = useState(true);
@@ -163,7 +180,8 @@ function MoneyRequestReceiptView({
163180
const didReceiptScanSucceed = hasReceipt && didReceiptScanSucceedTransactionUtils(transaction);
164181
const isInvoice = isInvoiceReport(moneyRequestReport);
165182
const isChatReportArchived = useReportIsArchived(moneyRequestReport?.chatReportID);
166-
const {login: currentUserLogin, accountID: currentUserAccountID, timezone: currentUserTimezone} = useCurrentUserPersonalDetails();
183+
const currentUserPersonalDetail = useCurrentUserPersonalDetails();
184+
const {login: currentUserLogin, accountID: currentUserAccountID, timezone: currentUserTimezone} = currentUserPersonalDetail;
167185
const theme = useTheme();
168186
const ancestors = useAncestors(report);
169187
const {hovered, bind: hoverBind} = useHover();
@@ -368,7 +386,19 @@ function MoneyRequestReceiptView({
368386
}
369387
if (transaction?.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) {
370388
if (chatReport?.reportID && getCreationReportErrors(chatReport)) {
371-
navigateToConciergeChatAndDeleteReport(chatReport.reportID, conciergeReportID, currentUserAccountID, introSelected, isSelfTourViewed, betas, personalDetails, true, true);
389+
navigateToConciergeChatAndDeleteReport(
390+
chatReport.reportID,
391+
conciergeReportID,
392+
currentUserAccountID,
393+
introSelected,
394+
isSelfTourViewed,
395+
betas,
396+
chatReportOwnerPersonalDetail,
397+
currentUserPersonalDetail,
398+
conciergePersonalDetail,
399+
true,
400+
true,
401+
);
372402
return;
373403
}
374404
if (parentReportAction) {
@@ -405,7 +435,19 @@ function MoneyRequestReceiptView({
405435
if (isInNarrowPaneModal) {
406436
Navigation.goBack();
407437
}
408-
navigateToConciergeChatAndDeleteReport(report.reportID, conciergeReportID, currentUserAccountID, introSelected, isSelfTourViewed, betas, personalDetails, true, true);
438+
navigateToConciergeChatAndDeleteReport(
439+
report.reportID,
440+
conciergeReportID,
441+
currentUserAccountID,
442+
introSelected,
443+
isSelfTourViewed,
444+
betas,
445+
reportOwnerPersonalDetail,
446+
currentUserPersonalDetail,
447+
conciergePersonalDetail,
448+
true,
449+
true,
450+
);
409451
}
410452
};
411453

src/components/ReportActionItem/TaskView.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,19 @@ function TaskView({report, parentReport, action}: TaskViewProps) {
118118
<OfflineWithFeedback
119119
shouldShowErrorMessages
120120
errors={report?.errorFields?.editTask ?? report?.errorFields?.createTask}
121-
onClose={() => clearTaskErrors(report, conciergeReportID, accountID, introSelected, betas, isSelfTourViewed, personalDetails)}
121+
onClose={() =>
122+
clearTaskErrors(
123+
report,
124+
conciergeReportID,
125+
accountID,
126+
introSelected,
127+
betas,
128+
isSelfTourViewed,
129+
report?.ownerAccountID ? (personalDetails?.[report.ownerAccountID] ?? undefined) : undefined,
130+
currentUserPersonalDetails,
131+
(personalDetails ? Object.values(personalDetails).find((detail) => detail?.login === CONST.EMAIL.CONCIERGE) : undefined) ?? undefined,
132+
)
133+
}
122134
errorRowStyles={styles.ph5}
123135
>
124136
<Hoverable>

src/libs/actions/Report/index.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ import type {
229229
VisibleReportActionsDerivedValue,
230230
} from '@src/types/onyx';
231231
import type {Decision} from '@src/types/onyx/OriginalMessage';
232+
import type PersonalDetails from '@src/types/onyx/PersonalDetails';
232233
import type {CurrentUserPersonalDetails, Timezone} from '@src/types/onyx/PersonalDetails';
233234
import type {ConnectionName} from '@src/types/onyx/Policy';
234235
import type {NotificationPreference, Participants, Participant as ReportParticipant, RoomVisibility, WriteCapability} from '@src/types/onyx/Report';
@@ -428,6 +429,20 @@ Onyx.connect({
428429
},
429430
});
430431

432+
/**
433+
* Builds a partial PersonalDetailsList containing only the records passed in. Skips entries with no accountID.
434+
*/
435+
function buildPersonalDetailsList(details: Array<OnyxEntry<PersonalDetails>>): PersonalDetailsList {
436+
const result: PersonalDetailsList = {};
437+
for (const detail of details) {
438+
if (detail?.accountID == null) {
439+
continue;
440+
}
441+
result[detail.accountID] = detail;
442+
}
443+
return result;
444+
}
445+
431446
const typingWatchTimers: Record<string, NodeJS.Timeout> = {};
432447

433448
let reportIDDeeplinkedFromOldDot: string | undefined;
@@ -4112,7 +4127,9 @@ function navigateToConciergeChatAndDeleteReport(
41124127
introSelected: OnyxEntry<IntroSelected>,
41134128
isSelfTourViewed: boolean | undefined,
41144129
betas: OnyxEntry<Beta[]>,
4115-
personalDetails: OnyxEntry<PersonalDetailsList>,
4130+
reportOwnerPersonalDetail: OnyxEntry<PersonalDetails>,
4131+
currentUserPersonalDetail: OnyxEntry<PersonalDetails>,
4132+
conciergePersonalDetail: OnyxEntry<PersonalDetails>,
41164133
shouldPopToTop = false,
41174134
shouldDeleteChildReports = false,
41184135
) {
@@ -4122,6 +4139,7 @@ function navigateToConciergeChatAndDeleteReport(
41224139
} else {
41234140
Navigation.goBack();
41244141
}
4142+
const personalDetails = buildPersonalDetailsList([reportOwnerPersonalDetail, currentUserPersonalDetail, conciergePersonalDetail]);
41254143
navigateToConciergeChat(conciergeReportID, introSelected, currentUserAccountID, isSelfTourViewed, betas, false, undefined, undefined, undefined, personalDetails);
41264144
// eslint-disable-next-line @typescript-eslint/no-deprecated
41274145
InteractionManager.runAfterInteractions(() => {
@@ -4136,7 +4154,9 @@ function clearCreateChatError(
41364154
currentUserAccountID: number,
41374155
betas: OnyxEntry<Beta[]>,
41384156
isSelfTourViewed: boolean | undefined,
4139-
personalDetails: OnyxEntry<PersonalDetailsList>,
4157+
reportOwnerPersonalDetail: OnyxEntry<PersonalDetails>,
4158+
currentUserPersonalDetail: OnyxEntry<PersonalDetails>,
4159+
conciergePersonalDetail: OnyxEntry<PersonalDetails>,
41404160
) {
41414161
const metaData = getReportMetadata(report?.reportID);
41424162
const isOptimisticReport = metaData?.isOptimisticReport;
@@ -4145,7 +4165,19 @@ function clearCreateChatError(
41454165
return;
41464166
}
41474167

4148-
navigateToConciergeChatAndDeleteReport(report?.reportID, conciergeReportID, currentUserAccountID, introSelected, isSelfTourViewed, betas, personalDetails, undefined, true);
4168+
navigateToConciergeChatAndDeleteReport(
4169+
report?.reportID,
4170+
conciergeReportID,
4171+
currentUserAccountID,
4172+
introSelected,
4173+
isSelfTourViewed,
4174+
betas,
4175+
reportOwnerPersonalDetail,
4176+
currentUserPersonalDetail,
4177+
conciergePersonalDetail,
4178+
undefined,
4179+
true,
4180+
);
41494181
}
41504182

41514183
/**

src/libs/actions/Task.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import ROUTES from '@src/ROUTES';
2525
import type {Route} from '@src/ROUTES';
2626
import type * as OnyxTypes from '@src/types/onyx';
2727
import type {Icon} from '@src/types/onyx/OnyxCommon';
28+
import type PersonalDetails from '@src/types/onyx/PersonalDetails';
2829
import type {ReportActions} from '@src/types/onyx/ReportAction';
2930
import type ReportAction from '@src/types/onyx/ReportAction';
3031
import type {OnyxData} from '@src/types/onyx/Request';
@@ -1421,7 +1422,9 @@ function clearTaskErrors(
14211422
introSelected: OnyxEntry<OnyxTypes.IntroSelected>,
14221423
betas: OnyxEntry<OnyxTypes.Beta[]>,
14231424
isSelfTourViewed: boolean | undefined,
1424-
personalDetails: OnyxEntry<OnyxTypes.PersonalDetailsList>,
1425+
reportOwnerPersonalDetail: OnyxEntry<PersonalDetails>,
1426+
currentUserPersonalDetail: OnyxEntry<PersonalDetails>,
1427+
conciergePersonalDetail: OnyxEntry<PersonalDetails>,
14251428
) {
14261429
const reportID = report?.reportID;
14271430
if (!reportID) {
@@ -1432,7 +1435,19 @@ function clearTaskErrors(
14321435
if (report?.pendingFields?.createChat === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD) {
14331436
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.parentReportID}`, report.parentReportActionID ? {[report.parentReportActionID]: null} : {});
14341437

1435-
navigateToConciergeChatAndDeleteReport(reportID, conciergeReportID, currentUserAccountID, introSelected, isSelfTourViewed, betas, personalDetails, undefined, undefined);
1438+
navigateToConciergeChatAndDeleteReport(
1439+
reportID,
1440+
conciergeReportID,
1441+
currentUserAccountID,
1442+
introSelected,
1443+
isSelfTourViewed,
1444+
betas,
1445+
reportOwnerPersonalDetail,
1446+
currentUserPersonalDetail,
1447+
conciergePersonalDetail,
1448+
undefined,
1449+
undefined,
1450+
);
14361451
return;
14371452
}
14381453

src/pages/Debug/Report/DebugReportPage.tsx

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {View} from 'react-native';
44
import type {OnyxEntry} from 'react-native-onyx';
55
import Button from '@components/Button';
66
import HeaderWithBackButton from '@components/HeaderWithBackButton';
7-
import {usePersonalDetails} from '@components/OnyxListItemProvider';
87
import ScreenWrapper from '@components/ScreenWrapper';
98
import Text from '@components/Text';
109
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
@@ -33,9 +32,17 @@ import CONST from '@src/CONST';
3332
import ONYXKEYS from '@src/ONYXKEYS';
3433
import ROUTES from '@src/ROUTES';
3534
import type SCREENS from '@src/SCREENS';
36-
import type {ReportAttributesDerivedValue} from '@src/types/onyx';
35+
import type {PersonalDetailsList, ReportAttributesDerivedValue} from '@src/types/onyx';
36+
import type PersonalDetails from '@src/types/onyx/PersonalDetails';
3737
import DebugReportActions from './DebugReportActions';
3838

39+
const findConciergePersonalDetail = (list: OnyxEntry<PersonalDetailsList>): OnyxEntry<PersonalDetails> => {
40+
if (!list) {
41+
return undefined;
42+
}
43+
return Object.values(list).find((detail): detail is PersonalDetails => detail?.login === CONST.EMAIL.CONCIERGE);
44+
};
45+
3946
type DebugReportPageProps = PlatformStackScreenProps<DebugParamList, typeof SCREENS.DEBUG.REPORT>;
4047

4148
type Metadata = {
@@ -77,8 +84,14 @@ function DebugReportPage({
7784
const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID);
7885
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED);
7986
const [isSelfTourViewed] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {selector: hasSeenTourSelector});
80-
const personalDetails = usePersonalDetails();
81-
const {accountID: currentUserAccountID, login: currentUserLogin} = useCurrentUserPersonalDetails();
87+
const currentUserPersonalDetail = useCurrentUserPersonalDetails();
88+
const {accountID: currentUserAccountID, login: currentUserLogin} = currentUserPersonalDetail;
89+
const [conciergePersonalDetail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: findConciergePersonalDetail});
90+
const reportOwnerSelector = useCallback(
91+
(list: OnyxEntry<PersonalDetailsList>): OnyxEntry<PersonalDetails> => (report?.ownerAccountID ? (list?.[report.ownerAccountID] ?? undefined) : undefined),
92+
[report?.ownerAccountID],
93+
);
94+
const [reportOwnerPersonalDetail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: reportOwnerSelector}, [reportOwnerSelector]);
8295
const transactionID = DebugUtils.getTransactionID(report, reportActions);
8396
const isReportArchived = useReportIsArchived(reportID);
8497

@@ -187,7 +200,19 @@ function DebugReportPage({
187200
Debug.setDebugData(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, data);
188201
}}
189202
onDelete={() => {
190-
navigateToConciergeChatAndDeleteReport(reportID, conciergeReportID, currentUserAccountID, introSelected, isSelfTourViewed, betas, personalDetails, true, true);
203+
navigateToConciergeChatAndDeleteReport(
204+
reportID,
205+
conciergeReportID,
206+
currentUserAccountID,
207+
introSelected,
208+
isSelfTourViewed,
209+
betas,
210+
reportOwnerPersonalDetail,
211+
currentUserPersonalDetail,
212+
conciergePersonalDetail,
213+
true,
214+
true,
215+
);
191216
}}
192217
validate={DebugUtils.validateReportDraftProperty}
193218
>
@@ -246,14 +271,16 @@ function DebugReportPage({
246271
styles.textSupporting,
247272
theme.cardBG,
248273
transactionID,
249-
personalDetails,
250274
translate,
251275
icons.Eye,
252276
currentUserAccountID,
253277
conciergeReportID,
254278
introSelected,
255279
isSelfTourViewed,
256280
betas,
281+
reportOwnerPersonalDetail,
282+
currentUserPersonalDetail,
283+
conciergePersonalDetail,
257284
],
258285
);
259286

src/pages/inbox/report/ReportActionItemCreated.tsx

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {hasSeenTourSelector} from '@selectors/Onboarding';
2-
import React, {memo} from 'react';
2+
import React, {memo, useCallback} from 'react';
33
import {View} from 'react-native';
4+
import type {OnyxEntry} from 'react-native-onyx';
45
import OfflineWithFeedback from '@components/OfflineWithFeedback';
5-
import {usePersonalDetails} from '@components/OnyxListItemProvider';
66
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
77
import ReportActionAvatars from '@components/ReportActionAvatars';
88
import ReportWelcomeText from '@components/ReportWelcomeText';
@@ -16,8 +16,17 @@ import {isChatReport, isCurrentUserInvoiceReceiver, isInvoiceRoom, navigateToDet
1616
import {clearCreateChatError} from '@userActions/Report';
1717
import CONST from '@src/CONST';
1818
import ONYXKEYS from '@src/ONYXKEYS';
19+
import type {PersonalDetailsList} from '@src/types/onyx';
20+
import type PersonalDetails from '@src/types/onyx/PersonalDetails';
1921
import AnimatedEmptyStateBackground from './AnimatedEmptyStateBackground';
2022

23+
const findConciergePersonalDetail = (list: OnyxEntry<PersonalDetailsList>): OnyxEntry<PersonalDetails> => {
24+
if (!list) {
25+
return undefined;
26+
}
27+
return Object.values(list).find((detail): detail is PersonalDetails => detail?.login === CONST.EMAIL.CONCIERGE);
28+
};
29+
2130
type ReportActionItemCreatedProps = {
2231
/** The id of the report */
2332
reportID: string | undefined;
@@ -37,8 +46,14 @@ function ReportActionItemCreated({reportID, policyID}: ReportActionItemCreatedPr
3746
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED);
3847
const [betas] = useOnyx(ONYXKEYS.BETAS);
3948
const [isSelfTourViewed] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {selector: hasSeenTourSelector});
40-
const personalDetails = usePersonalDetails();
41-
const {accountID: currentUserAccountID} = useCurrentUserPersonalDetails();
49+
const currentUserPersonalDetail = useCurrentUserPersonalDetails();
50+
const {accountID: currentUserAccountID} = currentUserPersonalDetail;
51+
const [conciergePersonalDetail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: findConciergePersonalDetail});
52+
const reportOwnerSelector = useCallback(
53+
(list: OnyxEntry<PersonalDetailsList>): OnyxEntry<PersonalDetails> => (report?.ownerAccountID ? (list?.[report.ownerAccountID] ?? undefined) : undefined),
54+
[report?.ownerAccountID],
55+
);
56+
const [reportOwnerPersonalDetail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: reportOwnerSelector}, [reportOwnerSelector]);
4257

4358
if (!isChatReport(report)) {
4459
return null;
@@ -51,7 +66,19 @@ function ReportActionItemCreated({reportID, policyID}: ReportActionItemCreatedPr
5166
pendingAction={report?.pendingFields?.addWorkspaceRoom ?? report?.pendingFields?.createChat}
5267
errors={report?.errorFields?.addWorkspaceRoom ?? report?.errorFields?.createChat}
5368
errorRowStyles={[styles.ml10, styles.mr2]}
54-
onClose={() => clearCreateChatError(report, conciergeReportID, introSelected, currentUserAccountID, betas, isSelfTourViewed, personalDetails)}
69+
onClose={() =>
70+
clearCreateChatError(
71+
report,
72+
conciergeReportID,
73+
introSelected,
74+
currentUserAccountID,
75+
betas,
76+
isSelfTourViewed,
77+
reportOwnerPersonalDetail,
78+
currentUserPersonalDetail,
79+
conciergePersonalDetail,
80+
)
81+
}
5582
>
5683
<View style={[styles.pRelative]}>
5784
<AnimatedEmptyStateBackground />

0 commit comments

Comments
 (0)