Skip to content

Commit

Permalink
Merge pull request #52311 from margelo/cleanup/optionlistutils-report…
Browse files Browse the repository at this point in the history
…-field
  • Loading branch information
dangrous authored Nov 12, 2024
2 parents 7b31a72 + 46e89b0 commit b9e6208
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 117 deletions.
108 changes: 0 additions & 108 deletions src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ type GetOptionsConfig = {
taxRates?: TaxRatesWithDefault;
policy?: OnyxEntry<Policy>;
transaction?: OnyxEntry<Transaction>;
includePolicyReportFieldOptions?: boolean;
policyReportFieldOptions?: string[];
recentlyUsedPolicyReportFieldOptions?: string[];
transactionViolations?: OnyxCollection<TransactionViolation[]>;
includeInvoiceRooms?: boolean;
includeDomainEmail?: boolean;
Expand Down Expand Up @@ -217,7 +214,6 @@ type Options = {
categoryOptions: CategoryTreeSection[];
tagOptions: CategorySection[];
taxRatesOptions: CategorySection[];
policyReportFieldOptions?: CategorySection[] | null;
};

type PreviewConfig = {showChatPreviewLine?: boolean; forcePolicyNamePreview?: boolean; showPersonalDetails?: boolean};
Expand Down Expand Up @@ -1309,81 +1305,6 @@ function hasEnabledTags(policyTagList: Array<PolicyTagLists[keyof PolicyTagLists
return hasEnabledOptions(policyTagValueList);
}

/**
* Transforms the provided report field options into option objects.
*
* @param reportFieldOptions - an initial report field options array
*/
function getReportFieldOptions(reportFieldOptions: string[]): Option[] {
return reportFieldOptions.map((name) => ({
text: name,
keyForList: name,
searchText: name,
tooltipText: name,
isDisabled: false,
}));
}

/**
* Build the section list for report field options
*/
function getReportFieldOptionsSection(options: string[], recentlyUsedOptions: string[], selectedOptions: Array<Partial<ReportUtils.OptionData>>, searchInputValue: string) {
const reportFieldOptionsSections = [];
const selectedOptionKeys = selectedOptions.map(({text, keyForList, name}) => text ?? keyForList ?? name ?? '').filter((o) => !!o);
let indexOffset = 0;

if (searchInputValue) {
const searchOptions = options.filter((option) => option.toLowerCase().includes(searchInputValue.toLowerCase()));

reportFieldOptionsSections.push({
// "Search" section
title: '',
shouldShow: true,
indexOffset,
data: getReportFieldOptions(searchOptions),
});

return reportFieldOptionsSections;
}

const filteredRecentlyUsedOptions = recentlyUsedOptions.filter((recentlyUsedOption) => !selectedOptionKeys.includes(recentlyUsedOption));
const filteredOptions = options.filter((option) => !selectedOptionKeys.includes(option));

if (selectedOptionKeys.length) {
reportFieldOptionsSections.push({
// "Selected" section
title: '',
shouldShow: true,
indexOffset,
data: getReportFieldOptions(selectedOptionKeys),
});

indexOffset += selectedOptionKeys.length;
}

if (filteredRecentlyUsedOptions.length > 0) {
reportFieldOptionsSections.push({
// "Recent" section
title: Localize.translateLocal('common.recent'),
shouldShow: true,
indexOffset,
data: getReportFieldOptions(filteredRecentlyUsedOptions),
});

indexOffset += filteredRecentlyUsedOptions.length;
}

reportFieldOptionsSections.push({
// "All" section when items amount more than the threshold
title: Localize.translateLocal('common.all'),
shouldShow: true,
indexOffset,
data: getReportFieldOptions(filteredOptions),
});

return reportFieldOptionsSections;
}

/**
* Sorts tax rates alphabetically by name.
*/
Expand Down Expand Up @@ -1727,9 +1648,6 @@ function getOptions(
policy,
transaction,
includeSelfDM = false,
includePolicyReportFieldOptions = false,
policyReportFieldOptions = [],
recentlyUsedPolicyReportFieldOptions = [],
includeInvoiceRooms = false,
includeDomainEmail = false,
action,
Expand Down Expand Up @@ -1779,20 +1697,6 @@ function getOptions(
};
}

if (includePolicyReportFieldOptions) {
const transformedPolicyReportFieldOptions = getReportFieldOptionsSection(policyReportFieldOptions, recentlyUsedPolicyReportFieldOptions, selectedOptions, searchInputValue);
return {
recentReports: [],
personalDetails: [],
userToInvite: null,
currentUserOption: null,
categoryOptions: [],
tagOptions: [],
taxRatesOptions: [],
policyReportFieldOptions: transformedPolicyReportFieldOptions,
};
}

const parsedPhoneNumber = PhoneNumber.parsePhoneNumber(LoginUtils.appendCountryCode(Str.removeSMSDomain(searchInputValue)));
const searchValue = parsedPhoneNumber.possible ? parsedPhoneNumber.number?.e164 ?? '' : searchInputValue.toLowerCase();
const topmostReportId = Navigation.getTopmostReportId() ?? '-1';
Expand Down Expand Up @@ -2145,9 +2049,6 @@ type FilteredOptionsParams = {
taxRates?: TaxRatesWithDefault;
maxRecentReportsToShow?: number;
includeSelfDM?: boolean;
includePolicyReportFieldOptions?: boolean;
policyReportFieldOptions?: string[];
recentlyUsedPolicyReportFieldOptions?: string[];
includeInvoiceRooms?: boolean;
action?: IOUAction;
sortByReportTypeInSearch?: boolean;
Expand Down Expand Up @@ -2186,9 +2087,6 @@ function getFilteredOptions(params: FilteredOptionsParamsWithDefaultSearchValue
maxRecentReportsToShow = CONST.IOU.MAX_RECENT_REPORTS_TO_SHOW,
taxRates = {} as TaxRatesWithDefault,
includeSelfDM = false,
includePolicyReportFieldOptions = false,
policyReportFieldOptions = [],
recentlyUsedPolicyReportFieldOptions = [],
includeInvoiceRooms = false,
action,
sortByReportTypeInSearch = false,
Expand All @@ -2215,9 +2113,6 @@ function getFilteredOptions(params: FilteredOptionsParamsWithDefaultSearchValue
includeTaxRates,
taxRates,
includeSelfDM,
includePolicyReportFieldOptions,
policyReportFieldOptions,
recentlyUsedPolicyReportFieldOptions,
includeInvoiceRooms,
action,
sortByReportTypeInSearch,
Expand Down Expand Up @@ -2260,9 +2155,6 @@ function getAttendeeOptions(
maxRecentReportsToShow: 0,
taxRates: {} as TaxRatesWithDefault,
includeSelfDM: false,
includePolicyReportFieldOptions: false,
policyReportFieldOptions: [],
recentlyUsedPolicyReportFieldOptions: [],
includeInvoiceRooms,
action,
sortByReportTypeInSearch,
Expand Down
90 changes: 90 additions & 0 deletions src/libs/ReportFieldOptionsListUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import * as Localize from './Localize';
import type {Option} from './OptionsListUtils';
import type * as ReportUtils from './ReportUtils';

/**
* Transforms the provided report field options into option objects.
*
* @param reportFieldOptions - an initial report field options array
*/
function getReportFieldOptions(reportFieldOptions: string[]): Option[] {
return reportFieldOptions.map((name) => ({
text: name,
keyForList: name,
searchText: name,
tooltipText: name,
isDisabled: false,
}));
}

/**
* Build the section list for report field options
*/
function getReportFieldOptionsSection({
options,
recentlyUsedOptions,
selectedOptions,
searchValue,
}: {
options: string[];
recentlyUsedOptions: string[];
selectedOptions: Array<Partial<ReportUtils.OptionData>>;
searchValue: string;
}) {
const reportFieldOptionsSections = [];
const selectedOptionKeys = selectedOptions.map(({text, keyForList, name}) => text ?? keyForList ?? name ?? '').filter((o) => !!o);
let indexOffset = 0;

if (searchValue) {
const searchOptions = options.filter((option) => option.toLowerCase().includes(searchValue.toLowerCase()));

reportFieldOptionsSections.push({
// "Search" section
title: '',
shouldShow: true,
indexOffset,
data: getReportFieldOptions(searchOptions),
});

return reportFieldOptionsSections;
}

const filteredRecentlyUsedOptions = recentlyUsedOptions.filter((recentlyUsedOption) => !selectedOptionKeys.includes(recentlyUsedOption));
const filteredOptions = options.filter((option) => !selectedOptionKeys.includes(option));

if (selectedOptionKeys.length) {
reportFieldOptionsSections.push({
// "Selected" section
title: '',
shouldShow: true,
indexOffset,
data: getReportFieldOptions(selectedOptionKeys),
});

indexOffset += selectedOptionKeys.length;
}

if (filteredRecentlyUsedOptions.length > 0) {
reportFieldOptionsSections.push({
// "Recent" section
title: Localize.translateLocal('common.recent'),
shouldShow: true,
indexOffset,
data: getReportFieldOptions(filteredRecentlyUsedOptions),
});

indexOffset += filteredRecentlyUsedOptions.length;
}

reportFieldOptionsSections.push({
// "All" section when items amount more than the threshold
title: Localize.translateLocal('common.all'),
shouldShow: true,
indexOffset,
data: getReportFieldOptions(filteredOptions),
});

return reportFieldOptionsSections;
}

export {getReportFieldOptionsSection, getReportFieldOptions};
15 changes: 6 additions & 9 deletions src/pages/EditReportFieldDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import localeCompare from '@libs/LocaleCompare';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as ReportFieldOptionsListUtils from '@libs/ReportFieldOptionsListUtils';
import ONYXKEYS from '@src/ONYXKEYS';

type EditReportFieldDropdownPageComponentProps = {
Expand Down Expand Up @@ -58,7 +59,7 @@ function EditReportFieldDropdownPage({onSubmit, fieldKey, fieldValue, fieldOptio
const [sections, headerMessage] = useMemo(() => {
const validFieldOptions = fieldOptions?.filter((option) => !!option)?.sort(localeCompare);

const {policyReportFieldOptions} = OptionsListUtils.getFilteredOptions({
const policyReportFieldOptions = ReportFieldOptionsListUtils.getReportFieldOptionsSection({
searchValue: debouncedSearchValue,
selectedOptions: [
{
Expand All @@ -67,21 +68,17 @@ function EditReportFieldDropdownPage({onSubmit, fieldKey, fieldValue, fieldOptio
text: fieldValue,
},
],

includeP2P: false,
canInviteUser: false,
includePolicyReportFieldOptions: true,
policyReportFieldOptions: validFieldOptions,
recentlyUsedPolicyReportFieldOptions: recentlyUsedOptions,
options: validFieldOptions,
recentlyUsedOptions,
});

const policyReportFieldData = policyReportFieldOptions?.[0]?.data ?? [];
const policyReportFieldData = policyReportFieldOptions.at(0)?.data ?? [];
const header = OptionsListUtils.getHeaderMessageForNonUserList(policyReportFieldData.length > 0, debouncedSearchValue);

return [policyReportFieldOptions, header];
}, [recentlyUsedOptions, debouncedSearchValue, fieldValue, fieldOptions]);

const selectedOptionKey = useMemo(() => (sections?.[0]?.data ?? []).filter((option) => option.searchText === fieldValue)?.at(0)?.keyForList, [sections, fieldValue]);
const selectedOptionKey = useMemo(() => (sections.at(0)?.data ?? []).filter((option) => option.searchText === fieldValue)?.at(0)?.keyForList, [sections, fieldValue]);
return (
<SelectionList
textInputValue={searchValue}
Expand Down

0 comments on commit b9e6208

Please sign in to comment.