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
122 changes: 66 additions & 56 deletions src/libs/OptionsListUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
*/

let allReports: OnyxCollection<Report>;
Onyx.connect({

Check warning on line 210 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function

Check warning on line 210 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -219,7 +219,7 @@
const allSortedReportActions: Record<string, ReportAction[]> = {};
const cachedOneTransactionThreadReportIDs: Record<string, string | undefined> = {};
let allReportActions: OnyxCollection<ReportActions>;
Onyx.connect({

Check warning on line 222 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => {
Expand Down Expand Up @@ -264,7 +264,7 @@
});

let activePolicyID: OnyxEntry<string>;
Onyx.connect({

Check warning on line 267 in src/libs/OptionsListUtils/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NVP_ACTIVE_POLICY_ID,
callback: (value) => (activePolicyID = value),
});
Expand Down Expand Up @@ -933,23 +933,36 @@
return lastMessageTextFromReport || (report?.lastMessageText ?? '');
}

type CreateOptionParams = {
accountIDs: number[];
personalDetails: OnyxInputOrEntry<PersonalDetailsList>;
report: OnyxInputOrEntry<Report>;
currentUserAccountID: number;
privateIsArchived: string | undefined;
policy?: OnyxEntry<Policy>;
config?: PreviewConfig;
reportAttributesDerived?: ReportAttributesDerivedValue['reports'];
policyTags?: OnyxEntry<PolicyTagLists>;
visibleReportActionsData?: VisibleReportActionsDerivedValue;
translate?: LocalizedTranslate;
};

/**
* Creates a report list option - optimized for SearchOption context
*/
// eslint-disable-next-line max-params, @typescript-eslint/max-params -- this should be handled in the separate issue
function createOption(
accountIDs: number[],
personalDetails: OnyxInputOrEntry<PersonalDetailsList>,
report: OnyxInputOrEntry<Report>,
currentUserAccountID: number,
policy: OnyxEntry<Policy>,
privateIsArchived: string | undefined,
config?: PreviewConfig,
reportAttributesDerived?: ReportAttributesDerivedValue['reports'],
policyTags?: OnyxEntry<PolicyTagLists>,
visibleReportActionsData: VisibleReportActionsDerivedValue = {},
translate?: LocalizedTranslate,
): SearchOptionData {
function createOption({
accountIDs,
personalDetails,
report,
currentUserAccountID,
privateIsArchived,
policy,
config,
reportAttributesDerived,
policyTags,
visibleReportActionsData = {},
translate,
}: CreateOptionParams): SearchOptionData {
const {showChatPreviewLine = false, forcePolicyNamePreview = false, showPersonalDetails = false, selected, isSelected, isDisabled} = config ?? {};

// Initialize only the properties that are actually used in SearchOption context
Expand Down Expand Up @@ -1106,21 +1119,21 @@
const visibleParticipantAccountIDs = getParticipantsAccountIDsForDisplay(report, true);
const reportPolicyTags = policyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${getNonEmptyStringOnyxID(report?.policyID)}`];

const option = createOption(
visibleParticipantAccountIDs,
personalDetails ?? {},
!isEmptyObject(report) ? report : undefined,
const option = createOption({
accountIDs: visibleParticipantAccountIDs,
personalDetails: personalDetails ?? {},
report: !isEmptyObject(report) ? report : undefined,
currentUserAccountID,
policy,
privateIsArchived,
{
config: {
showChatPreviewLine: false,
forcePolicyNamePreview: false,
},
reportAttributesDerived,
reportPolicyTags,
policyTags: reportPolicyTags,
visibleReportActionsData,
);
});

// Update text & alternateText because createOption returns workspace name only if report is owned by the user
if (option.isSelfDM) {
Expand Down Expand Up @@ -1169,21 +1182,21 @@
): OptionData {
const visibleParticipantAccountIDs = getParticipantsAccountIDsForDisplay(report, true);

const option = createOption(
visibleParticipantAccountIDs,
personalDetails ?? {},
!isEmptyObject(report) ? report : undefined,
const option = createOption({
accountIDs: visibleParticipantAccountIDs,
personalDetails: personalDetails ?? {},
report: !isEmptyObject(report) ? report : undefined,
currentUserAccountID,
policy,
privateIsArchived,
{
config: {
showChatPreviewLine: false,
forcePolicyNamePreview: false,
},
reportAttributesDerived,
policyTags,
visibleReportActionsData,
);
});

// Update text & alternateText because createOption returns workspace name only if report is owned by the user
if (option.isSelfDM) {
Expand Down Expand Up @@ -1226,21 +1239,21 @@
.filter(([, reportParticipant]) => reportParticipant && !isHiddenForCurrentUser(reportParticipant.notificationPreference))
.map(([accountID]) => Number(accountID));

const option = createOption(
visibleParticipantAccountIDs,
personalDetails ?? {},
!isEmptyObject(expenseReport) ? expenseReport : null,
const option = createOption({
accountIDs: visibleParticipantAccountIDs,
personalDetails: personalDetails ?? {},
report: !isEmptyObject(expenseReport) ? expenseReport : null,
currentUserAccountID,
policy,
privateIsArchived,
{
config: {
showChatPreviewLine: false,
forcePolicyNamePreview: false,
},
reportAttributesDerived,
policyTags,
visibleReportActionsData,
);
});

// Update text & alternateText because createOption returns workspace name only if report is owned by the user
option.text = getPolicyName({report: expenseReport});
Expand Down Expand Up @@ -1377,7 +1390,7 @@
reportMapEntry,
reportOption: {
item: report,
...createOption(accountIDs, personalDetails, report, currentUserAccountID, policy, privateIsArchived, undefined, reportAttributesDerived, policyTags, visibleReportActionsData),
...createOption({accountIDs, personalDetails, report, currentUserAccountID, privateIsArchived, policy, reportAttributesDerived, policyTags, visibleReportActionsData}),
},
};
}
Expand Down Expand Up @@ -1431,20 +1444,20 @@
const reportPolicyTags = policyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${getNonEmptyStringOnyxID(report?.policyID)}`];
return {
item: personalDetail,
...createOption(
[personalDetail?.accountID ?? CONST.DEFAULT_NUMBER_ID],
...createOption({
accountIDs: [personalDetail?.accountID ?? CONST.DEFAULT_NUMBER_ID],
personalDetails,
report,
currentUserAccountID,
policy,
privateIsArchived,
{
config: {
showPersonalDetails: true,
},
reportAttributesDerived,
reportPolicyTags,
policyTags: reportPolicyTags,
visibleReportActionsData,
),
}),
};
});

Expand Down Expand Up @@ -1586,18 +1599,18 @@
const reportPolicyTags = policyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${getNonEmptyStringOnyxID(report?.policyID)}`];
return {
item: personalDetail,
...createOption(
[accountID],
...createOption({
accountIDs: [accountID],
personalDetails,
reportMapForAccountIDs[accountID],
report: reportMapForAccountIDs[accountID],
currentUserAccountID,
policy,
privateIsArchived,
{showPersonalDetails: true},
config: {showPersonalDetails: true},
reportAttributesDerived,
reportPolicyTags,
policyTags: reportPolicyTags,
visibleReportActionsData,
),
}),
};
})
: [];
Expand All @@ -1623,7 +1636,7 @@

return {
item: report,
...createOption(accountIDs, personalDetails, report, currentUserAccountID, policy, privateIsArchived, config, reportAttributesDerived, policyTags, visibleReportActionsData),
...createOption({accountIDs, personalDetails, report, currentUserAccountID, privateIsArchived, policy, config, reportAttributesDerived, policyTags, visibleReportActionsData}),
};
}

Expand Down Expand Up @@ -1947,18 +1960,15 @@
login: searchValue,
},
};
const userToInvite = createOption(
[optimisticAccountID],
personalDetailsExtended,
null,
const userToInvite = createOption({
accountIDs: [optimisticAccountID],
personalDetails: personalDetailsExtended,
report: null,
currentUserAccountID,
undefined,
undefined,
{showChatPreviewLine},
undefined,
undefined,
privateIsArchived: undefined,
config: {showChatPreviewLine},
visibleReportActionsData,
);
});
userToInvite.isOptimisticAccount = true;
userToInvite.login = searchValue;

Expand Down
21 changes: 17 additions & 4 deletions tests/unit/OptionsListUtilsTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3950,7 +3950,14 @@ describe('OptionsListUtils', () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`, transaction);
await waitForBatchedUpdates();

const result = createOption([1, 2], PERSONAL_DETAILS, report, CURRENT_USER_ACCOUNT_ID, undefined, undefined, {showChatPreviewLine: true});
const result = createOption({
accountIDs: [1, 2],
personalDetails: PERSONAL_DETAILS,
report,
currentUserAccountID: CURRENT_USER_ACCOUNT_ID,
privateIsArchived: undefined,
config: {showChatPreviewLine: true},
});

expect(result.alternateText).toBe('Iron Man owes ₫34');
});
Expand Down Expand Up @@ -3978,7 +3985,7 @@ describe('OptionsListUtils', () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`, chatReport);
await waitForBatchedUpdates();

const result = createOption([1, 2], PERSONAL_DETAILS, report, 1, undefined, undefined);
const result = createOption({accountIDs: [1, 2], personalDetails: PERSONAL_DETAILS, report, currentUserAccountID: 1, privateIsArchived: undefined});

expect(result.reportID).toBe(reportID);
expect(typeof result.text).toBe('string');
Expand All @@ -3997,7 +4004,7 @@ describe('OptionsListUtils', () => {
await waitForBatchedUpdates();

// Should not throw when reports is undefined
const result = createOption([1, 2], PERSONAL_DETAILS, report, 1, undefined, undefined, undefined);
const result = createOption({accountIDs: [1, 2], personalDetails: PERSONAL_DETAILS, report, currentUserAccountID: 1, privateIsArchived: undefined});

expect(result.reportID).toBe(report.reportID);
});
Expand Down Expand Up @@ -6542,7 +6549,13 @@ describe('OptionsListUtils', () => {
await waitForBatchedUpdates();

// When we call createOption with the linked chat report
const result = createOption([1, 2], PERSONAL_DETAILS, expenseReport, CURRENT_USER_ACCOUNT_ID, undefined, undefined);
const result = createOption({
accountIDs: [1, 2],
personalDetails: PERSONAL_DETAILS,
report: expenseReport,
currentUserAccountID: CURRENT_USER_ACCOUNT_ID,
privateIsArchived: undefined,
});

// Then the option should be created successfully
expect(result).toBeDefined();
Expand Down
Loading