From 6220f5c54b6daa6a7b17a15c87e5854a92ef151e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hanno=20J=2E=20G=C3=B6decke?= Date: Wed, 22 Jan 2025 11:04:46 +0100 Subject: [PATCH] wip: don't do calculations in getValidOptions, do them only once in getValidReports not sure yet if the type setup complicates things too much --- src/libs/OptionsListUtils.ts | 84 ++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/src/libs/OptionsListUtils.ts b/src/libs/OptionsListUtils.ts index 75808914d72f..bb21c69e1ef8 100644 --- a/src/libs/OptionsListUtils.ts +++ b/src/libs/OptionsListUtils.ts @@ -193,15 +193,23 @@ type GetValidReportsConfig = { includeInvoiceRooms?: boolean; includeDomainEmail?: boolean; optionsToExclude?: Array>; + + // These two will change the return type + shouldSeparateWorkspaceChat?: boolean; + shouldSeparateSelfDMChat?: boolean; } & GetValidOptionsSharedConfig; +type GetValidReportsReturnTypeCombined = { + selfDMOptions: OptionData[]; + workspaceOptions: OptionData[]; + recentReports: OptionData[]; +}; + type GetOptionsConfig = { excludeLogins?: string[]; includeRecentReports?: boolean; includeSelectedOptions?: boolean; recentAttendees?: Attendee[]; - shouldSeparateWorkspaceChat?: boolean; - shouldSeparateSelfDMChat?: boolean; } & GetValidReportsConfig; type GetUserToInviteConfig = { @@ -1245,7 +1253,26 @@ function getUserToInviteOption({ function getValidReports( reports: OptionList['reports'], - { + config: GetValidReportsConfig & {shouldSeparateSelfDMChat: true; shouldSeparateWorkspaceChat: true}, +): GetValidReportsReturnTypeCombined; + +function getValidReports( + reports: OptionList['reports'], + config: GetValidReportsConfig & {shouldSeparateSelfDMChat: true; shouldSeparateWorkspaceChat?: false}, +): Omit; + +function getValidReports( + reports: OptionList['reports'], + config: GetValidReportsConfig & {shouldSeparateSelfDMChat?: false; shouldSeparateWorkspaceChat: true}, +): Omit; + +function getValidReports(reports: OptionList['reports'], config: GetValidReportsConfig & {shouldSeparateSelfDMChat?: false; shouldSeparateWorkspaceChat?: false}): OptionData[]; + +function getValidReports( + reports: OptionList['reports'], + config: GetValidReportsConfig, +): GetValidReportsReturnTypeCombined | Omit | Omit | OptionData[] { + const { betas = [], includeMultipleParticipantReports = false, showChatPreviewLine = false, @@ -1263,8 +1290,10 @@ function getValidReports( includeDomainEmail = false, shouldBoldTitleByDefault = true, optionsToExclude = [], - }: GetValidReportsConfig, -) { + shouldSeparateSelfDMChat, + shouldSeparateWorkspaceChat, + } = config; + const topmostReportId = Navigation.getTopmostReportId(); const validReportOptions: OptionData[] = []; @@ -1399,6 +1428,22 @@ function getValidReports( lastIOUCreationDate, }; + let workspaceChats: OptionData[] = []; + + if (shouldSeparateWorkspaceChat) { + workspaceChats = recentReportOptions.filter((option) => option.isOwnPolicyExpenseChat && !option.private_isArchived); + } + + let selfDMChat: OptionData | undefined; + + if (shouldSeparateWorkspaceChat) { + recentReportOptions = recentReportOptions.filter((option) => !option.isPolicyExpenseChat); + } + if (shouldSeparateSelfDMChat) { + selfDMChat = recentReportOptions.find((option) => option.isSelfDM); + recentReportOptions = recentReportOptions.filter((option) => !option.isSelfDM); + } + validReportOptions.push(newReportOption); } @@ -1410,16 +1455,7 @@ function getValidReports( */ function getValidOptions( options: OptionList, - { - excludeLogins = [], - includeSelectedOptions = false, - includeRecentReports = true, - recentAttendees, - selectedOptions = [], - shouldSeparateSelfDMChat = false, - shouldSeparateWorkspaceChat = false, - ...config - }: GetOptionsConfig = {}, + {excludeLogins = [], includeSelectedOptions = false, includeRecentReports = true, recentAttendees, selectedOptions = [], ...config}: GetOptionsConfig = {}, ): Options { // Gather shared configs: const optionsToExclude: Option[] = [{login: CONST.EMAIL.NOTIFICATIONS}]; @@ -1437,7 +1473,7 @@ function getValidOptions( // Get valid recent reports: let recentReportOptions: OptionData[] = []; if (includeRecentReports) { - recentReportOptions = getValidReports(options.reports, { + const res = getValidReports(options.reports, { ...getValidReportsConfig, includeP2P, includeDomainEmail, @@ -1476,22 +1512,6 @@ function getValidOptions( } } - let workspaceChats: OptionData[] = []; - - if (shouldSeparateWorkspaceChat) { - workspaceChats = recentReportOptions.filter((option) => option.isOwnPolicyExpenseChat && !option.private_isArchived); - } - - let selfDMChat: OptionData | undefined; - - if (shouldSeparateWorkspaceChat) { - recentReportOptions = recentReportOptions.filter((option) => !option.isPolicyExpenseChat); - } - if (shouldSeparateSelfDMChat) { - selfDMChat = recentReportOptions.find((option) => option.isSelfDM); - recentReportOptions = recentReportOptions.filter((option) => !option.isSelfDM); - } - return { personalDetails: personalDetailsOptions, recentReports: recentReportOptions,