From ccd211776be81bc7e8c3e0b77ca616542cffa049 Mon Sep 17 00:00:00 2001 From: Chuck Dries Date: Thu, 31 Jul 2025 14:22:56 -0700 Subject: [PATCH 1/4] Revert "Merge pull request #67346 from Expensify/revert-65463-chuckdries/sync-drafts" This reverts commit 3642f6b7c86418dbe8589f311302dca622d99399, reversing changes made to 39b57bddf878984c140682b39455ddc749209f0a. --- src/ONYXKEYS.ts | 8 +++- .../LHNOptionsList/LHNOptionsList.tsx | 7 +-- src/hooks/useDiffPrevious.ts | 16 +++++++ src/hooks/usePriorityChange.ts | 2 +- src/hooks/useSidebarOrderedReports.tsx | 16 ++++--- .../SaveReportDraftCommentParams.ts | 6 +++ src/libs/API/parameters/index.ts | 1 + src/libs/API/types.ts | 2 + src/libs/DraftCommentUtils.ts | 25 ++++------- src/libs/SidebarUtils.ts | 29 +++++++++--- src/libs/actions/App.ts | 2 +- src/libs/actions/IOU.ts | 6 ++- src/libs/actions/Policy/DistanceRate.ts | 6 ++- src/libs/actions/Policy/Member.ts | 6 ++- src/libs/actions/Policy/Policy.ts | 6 ++- src/libs/actions/Policy/ReportField.ts | 6 ++- src/libs/actions/Policy/Tag.ts | 7 ++- src/libs/actions/Report.ts | 32 +++++++++---- src/libs/migrateOnyx.ts | 2 + src/libs/migrations/MoveDraftsToNVP.ts | 43 ++++++++++++++++++ .../ComposerWithSuggestions.tsx | 45 +++++++------------ .../SilentCommentUpdater/index.android.tsx | 29 ------------ .../SilentCommentUpdater/index.tsx | 43 ------------------ .../SilentCommentUpdater/types.ts | 18 -------- .../ReportActionCompose/SuggestionEmoji.tsx | 2 +- .../ReportActionCompose/SuggestionMention.tsx | 2 +- .../ReportActionCompose/Suggestions.tsx | 2 +- src/types/onyx/DraftReportComments.ts | 6 +++ src/types/onyx/index.ts | 2 + tests/ui/LHNItemsPresence.tsx | 2 +- tests/unit/DebugUtilsTest.ts | 2 +- tests/unit/ReportUtilsTest.ts | 2 +- tests/unit/SidebarFilterTest.ts | 7 +-- tests/unit/SidebarOrderTest.ts | 20 +++++---- 34 files changed, 216 insertions(+), 194 deletions(-) create mode 100644 src/hooks/useDiffPrevious.ts create mode 100644 src/libs/API/parameters/SaveReportDraftCommentParams.ts create mode 100644 src/libs/migrations/MoveDraftsToNVP.ts delete mode 100644 src/pages/home/report/ReportActionCompose/SilentCommentUpdater/index.android.tsx delete mode 100644 src/pages/home/report/ReportActionCompose/SilentCommentUpdater/index.tsx delete mode 100644 src/pages/home/report/ReportActionCompose/SilentCommentUpdater/types.ts create mode 100644 src/types/onyx/DraftReportComments.ts diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index dbc230c4f48b..3444742c2c15 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -562,6 +562,9 @@ const ONYXKEYS = { NVP_LAST_ECASH_ANDROID_LOGIN: 'nvp_lastECashAndroidLogin', NVP_LAST_ANDROID_LOGIN: 'nvp_lastAndroidLogin', + /** Draft report comments */ + NVP_DRAFT_REPORT_COMMENTS: 'nvp_draftReportComments', + /** Collection Keys */ COLLECTION: { DOWNLOAD: 'download_', @@ -595,7 +598,8 @@ const ONYXKEYS = { REPORT_ACTIONS_DRAFTS: 'reportActionsDrafts_', REPORT_ACTIONS_PAGES: 'reportActionsPages_', REPORT_ACTIONS_REACTIONS: 'reportActionsReactions_', - REPORT_DRAFT_COMMENT: 'reportDraftComment_', + /** @deprecated */ + REPORT_DRAFT_COMMENT: 'reportDraftComment_', // eslint-disable-line deprecation/deprecation REPORT_IS_COMPOSER_FULL_SIZE: 'reportIsComposerFullSize_', REPORT_USER_IS_TYPING: 'reportUserIsTyping_', REPORT_USER_IS_LEAVING_ROOM: 'reportUserIsLeavingRoom_', @@ -997,6 +1001,7 @@ type OnyxCollectionValuesMapping = { [ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS]: OnyxTypes.ReportActionsDrafts; [ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES]: OnyxTypes.Pages; [ONYXKEYS.COLLECTION.REPORT_ACTIONS_REACTIONS]: OnyxTypes.ReportActionReactions; + // eslint-disable-next-line deprecation/deprecation [ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT]: string; [ONYXKEYS.COLLECTION.REPORT_IS_COMPOSER_FULL_SIZE]: boolean; [ONYXKEYS.COLLECTION.REPORT_USER_IS_TYPING]: OnyxTypes.ReportUserIsTyping; @@ -1221,6 +1226,7 @@ type OnyxValuesMapping = { [ONYXKEYS.NVP_INTEGRATION_SERVER_EXPORT_TEMPLATES]: OnyxTypes.IntegrationServerExportTemplate[]; [ONYXKEYS.ONBOARDING_USER_REPORTED_INTEGRATION]: OnboardingAccounting; [ONYXKEYS.HYBRID_APP]: OnyxTypes.HybridApp; + [ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS]: OnyxTypes.DraftReportComments; }; type OnyxDerivedValuesMapping = { diff --git a/src/components/LHNOptionsList/LHNOptionsList.tsx b/src/components/LHNOptionsList/LHNOptionsList.tsx index 8f01d301c013..aac8b42aad89 100644 --- a/src/components/LHNOptionsList/LHNOptionsList.tsx +++ b/src/components/LHNOptionsList/LHNOptionsList.tsx @@ -19,7 +19,6 @@ import useRootNavigationState from '@hooks/useRootNavigationState'; import useScrollEventEmitter from '@hooks/useScrollEventEmitter'; import useTheme from '@hooks/useTheme'; import useThemeStyles from '@hooks/useThemeStyles'; -import {isValidDraftComment} from '@libs/DraftCommentUtils'; import getPlatform from '@libs/getPlatform'; import Log from '@libs/Log'; import {getIOUReportIDOfLastAction, getLastMessageTextForReport} from '@libs/OptionsListUtils'; @@ -53,7 +52,7 @@ function LHNOptionsList({style, contentContainerStyles, data, onSelectRow, optio const [policy] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false}); const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {canBeMissing: true}); const [transactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {canBeMissing: false}); - const [draftComments] = useOnyx(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, {canBeMissing: false}); + const [draftComments] = useOnyx(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, {canBeMissing: true}); const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: false}); const [dismissedProductTraining, dismissedProductTrainingMetadata] = useOnyx(ONYXKEYS.NVP_DISMISSED_PRODUCT_TRAINING, {canBeMissing: true}); const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); @@ -189,7 +188,7 @@ function LHNOptionsList({style, contentContainerStyles, data, onSelectRow, optio ? (getOriginalMessage(itemParentReportAction)?.IOUTransactionID ?? CONST.DEFAULT_NUMBER_ID) : CONST.DEFAULT_NUMBER_ID; const itemTransaction = transactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`]; - const hasDraftComment = isValidDraftComment(draftComments?.[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`]); + const hasDraftComment = !!draftComments?.[reportID]; const canUserPerformWrite = canUserPerformWriteAction(item); const sortedReportActions = getSortedReportActionsForDisplay(itemReportActions, canUserPerformWrite); @@ -286,7 +285,6 @@ function LHNOptionsList({style, contentContainerStyles, data, onSelectRow, optio policy, personalDetails, data.length, - draftComments, optionMode, preferredLocale, transactions, @@ -303,7 +301,6 @@ function LHNOptionsList({style, contentContainerStyles, data, onSelectRow, optio policy, personalDetails, data.length, - draftComments, optionMode, preferredLocale, transactions, diff --git a/src/hooks/useDiffPrevious.ts b/src/hooks/useDiffPrevious.ts new file mode 100644 index 000000000000..2687a17b7670 --- /dev/null +++ b/src/hooks/useDiffPrevious.ts @@ -0,0 +1,16 @@ +import union from 'lodash/union'; +import {useMemo} from 'react'; +import usePrevious from './usePrevious'; + +/** This hook provides a list of which keys changed in an object vs the previous render + * akin to `sourceValue` for collections. Generally, using this hook at all is an anti-pattern. + * Avoid it at all costs */ +export default function useDiffPrevious>(value: T): string[] { + const previous = usePrevious(value); + const diff = useMemo(() => { + const allKeys = union(Object.keys(value), Object.keys(previous)); + return allKeys.filter((key) => value[key] !== previous[key]); + }, [value, previous]); + + return diff; +} diff --git a/src/hooks/usePriorityChange.ts b/src/hooks/usePriorityChange.ts index 62c83bed29ed..a3b01857ceb0 100644 --- a/src/hooks/usePriorityChange.ts +++ b/src/hooks/usePriorityChange.ts @@ -7,7 +7,7 @@ import usePrevious from './usePrevious'; function usePriorityMode() { const [priorityMode] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE, {canBeMissing: true}); - const [allReportsWithDraftComments] = useOnyx(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, {canBeMissing: true}); + const [allReportsWithDraftComments] = useOnyx(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, {canBeMissing: true}); const prevPriorityMode = usePrevious(priorityMode); useEffect(() => { diff --git a/src/hooks/useSidebarOrderedReports.tsx b/src/hooks/useSidebarOrderedReports.tsx index d3a43c179f16..74f1b6e6deca 100644 --- a/src/hooks/useSidebarOrderedReports.tsx +++ b/src/hooks/useSidebarOrderedReports.tsx @@ -7,9 +7,11 @@ import SidebarUtils from '@libs/SidebarUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type * as OnyxTypes from '@src/types/onyx'; +import {getEmptyObject} from '@src/types/utils/EmptyObject'; import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems'; import useCurrentReportID from './useCurrentReportID'; import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; +import useDiffPrevious from './useDiffPrevious'; import useLocalize from './useLocalize'; import useOnyx from './useOnyx'; import usePrevious from './usePrevious'; @@ -63,7 +65,8 @@ function SidebarOrderedReportsContextProvider({ const [transactions, {sourceValue: transactionsUpdates}] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION, {canBeMissing: true}); const [transactionViolations, {sourceValue: transactionViolationsUpdates}] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, {canBeMissing: true}); const [reportNameValuePairs, {sourceValue: reportNameValuePairsUpdates}] = useOnyx(ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS, {canBeMissing: true}); - const [, {sourceValue: reportsDraftsUpdates}] = useOnyx(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, {canBeMissing: true}); + const [drafts = getEmptyObject()] = useOnyx(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, {canBeMissing: true}); + const reportsDraftsUpdates = useDiffPrevious(drafts); const [betas] = useOnyx(ONYXKEYS.BETAS, {canBeMissing: true}); const [reportAttributes] = useOnyx(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, {selector: (value) => value?.reports, canBeMissing: true}); const [currentReportsToDisplay, setCurrentReportsToDisplay] = useState({}); @@ -96,8 +99,8 @@ function SidebarOrderedReportsContextProvider({ reportsToUpdate = Object.keys(transactionViolationsUpdates ?? {}) .map((key) => key.replace(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, ONYXKEYS.COLLECTION.TRANSACTION)) .map((key) => `${ONYXKEYS.COLLECTION.REPORT}${transactions?.[key]?.reportID}`); - } else if (reportsDraftsUpdates) { - reportsToUpdate = Object.keys(reportsDraftsUpdates).map((key) => key.replace(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, ONYXKEYS.COLLECTION.REPORT)); + } else if (reportsDraftsUpdates.length > 0) { + reportsToUpdate = reportsDraftsUpdates.map((key) => `${ONYXKEYS.COLLECTION.REPORT}${key}`); } else if (policiesUpdates) { const updatedPolicies = Object.keys(policiesUpdates).map((key) => key.replace(ONYXKEYS.COLLECTION.POLICY, '')); reportsToUpdate = Object.entries(chatReports ?? {}) @@ -148,10 +151,10 @@ function SidebarOrderedReportsContextProvider({ derivedCurrentReportID, priorityMode === CONST.PRIORITY_MODE.GSD, betas, - policies, transactionViolations, reportNameValuePairs, reportAttributes, + drafts, ); } else { reportsToDisplay = SidebarUtils.getReportsToDisplayInLHN( @@ -163,19 +166,20 @@ function SidebarOrderedReportsContextProvider({ transactionViolations, reportNameValuePairs, reportAttributes, + drafts, ); } return reportsToDisplay; // Rule disabled intentionally — triggering a re-render on currentReportsToDisplay would cause an infinite loop // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps - }, [getUpdatedReports, chatReports, derivedCurrentReportID, priorityMode, betas, policies, transactionViolations, reportNameValuePairs, reportAttributes]); + }, [getUpdatedReports, chatReports, derivedCurrentReportID, priorityMode, betas, policies, transactionViolations, reportNameValuePairs, reportAttributes, reportsDraftsUpdates]); useEffect(() => { setCurrentReportsToDisplay(reportsToDisplayInLHN); }, [reportsToDisplayInLHN]); const getOrderedReportIDs = useCallback( - () => SidebarUtils.sortReportsToDisplayInLHN(reportsToDisplayInLHN, priorityMode, localeCompare, reportNameValuePairs, reportAttributes), + () => SidebarUtils.sortReportsToDisplayInLHN(reportsToDisplayInLHN, priorityMode, localeCompare, reportNameValuePairs, reportAttributes, drafts), // Rule disabled intentionally - reports should be sorted only when the reportsToDisplayInLHN changes // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps [reportsToDisplayInLHN, localeCompare], diff --git a/src/libs/API/parameters/SaveReportDraftCommentParams.ts b/src/libs/API/parameters/SaveReportDraftCommentParams.ts new file mode 100644 index 000000000000..6b89e5c3ac05 --- /dev/null +++ b/src/libs/API/parameters/SaveReportDraftCommentParams.ts @@ -0,0 +1,6 @@ +type SaveReportDraftCommentParams = { + reportID: string; + comment: string; +}; + +export default SaveReportDraftCommentParams; diff --git a/src/libs/API/parameters/index.ts b/src/libs/API/parameters/index.ts index 9e726ef807da..eb9dc96deb3f 100644 --- a/src/libs/API/parameters/index.ts +++ b/src/libs/API/parameters/index.ts @@ -414,3 +414,4 @@ export type {default as ReopenReportParams} from './ReopenReportParams'; export type {default as OpenUnreportedExpensesPageParams} from './OpenUnreportedExpensesPageParams'; export type {default as VerifyTestDriveRecipientParams} from './VerifyTestDriveRecipientParams'; export type {default as ExportSearchWithTemplateParams} from './ExportSearchWithTemplateParams'; +export type {default as SaveReportDraftCommentParams} from './SaveReportDraftCommentParams'; diff --git a/src/libs/API/types.ts b/src/libs/API/types.ts index 4eb7e4e0db75..ef636c72ee60 100644 --- a/src/libs/API/types.ts +++ b/src/libs/API/types.ts @@ -11,6 +11,7 @@ import type UpdateBeneficialOwnersForBankAccountParams from './parameters/Update type ApiRequestType = ValueOf; const WRITE_COMMANDS = { + SAVE_REPORT_DRAFT_COMMENT: 'SaveReportDraftComment', CLEAN_POLICY_TAGS: 'ClearPolicyTags', IMPORT_MULTI_LEVEL_TAGS: 'ImportMultiLevelTags', SET_WORKSPACE_AUTO_REPORTING_FREQUENCY: 'SetWorkspaceAutoReportingFrequency', @@ -851,6 +852,7 @@ type WriteCommandParameters = { [WRITE_COMMANDS.FINISH_CORPAY_BANK_ACCOUNT_ONBOARDING]: Parameters.FinishCorpayBankAccountOnboardingParams; [WRITE_COMMANDS.DELETE_VACATION_DELEGATE]: null; [WRITE_COMMANDS.REOPEN_REPORT]: Parameters.ReopenReportParams; + [WRITE_COMMANDS.SAVE_REPORT_DRAFT_COMMENT]: Parameters.SaveReportDraftCommentParams; [WRITE_COMMANDS.DELETE_MONEY_REQUEST_ON_SEARCH]: Parameters.DeleteMoneyRequestOnSearchParams; [WRITE_COMMANDS.HOLD_MONEY_REQUEST_ON_SEARCH]: Parameters.HoldMoneyRequestOnSearchParams; diff --git a/src/libs/DraftCommentUtils.ts b/src/libs/DraftCommentUtils.ts index b3cb32498725..b95294820b1e 100644 --- a/src/libs/DraftCommentUtils.ts +++ b/src/libs/DraftCommentUtils.ts @@ -1,14 +1,14 @@ -import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; +import type {OnyxEntry} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {DraftReportComments} from '@src/types/onyx'; -let draftCommentCollection: OnyxCollection = {}; +let draftComments: OnyxEntry = {}; Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, + key: ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, callback: (nextVal) => { - draftCommentCollection = nextVal; + draftComments = nextVal; }, - waitForCollectionCallback: true, }); /** @@ -17,22 +17,15 @@ Onyx.connect({ * A valid use-case of this function is outside React components, like in utility functions. */ function getDraftComment(reportID: string): OnyxEntry | null | undefined { - return draftCommentCollection?.[ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT + reportID]; -} - -/** - * Returns true if the report has a valid draft comment. - * A valid draft comment is a non-empty string. - */ -function isValidDraftComment(comment?: string | null): boolean { - return !!comment; + return draftComments?.[reportID]; } /** * Returns true if the report has a valid draft comment. + * NOTE: please prefer useOnyx when possible */ function hasValidDraftComment(reportID: string): boolean { - return isValidDraftComment(getDraftComment(reportID)); + return !!getDraftComment(reportID); } /** @@ -44,4 +37,4 @@ function prepareDraftComment(comment: string | null) { return comment || null; } -export {getDraftComment, isValidDraftComment, hasValidDraftComment, prepareDraftComment}; +export {getDraftComment, hasValidDraftComment, prepareDraftComment}; diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index b9a78f64d3fa..b8bfa1a7d06a 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -6,7 +6,17 @@ import type {LocaleContextProps} from '@components/LocaleContextProvider'; import type {PartialPolicyForSidebar, ReportsToDisplayInLHN} from '@hooks/useSidebarOrderedReports'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Card, PersonalDetails, PersonalDetailsList, ReportActions, ReportAttributesDerivedValue, ReportNameValuePairs, Transaction, TransactionViolation} from '@src/types/onyx'; +import type { + Card, + DraftReportComments, + PersonalDetails, + PersonalDetailsList, + ReportActions, + ReportAttributesDerivedValue, + ReportNameValuePairs, + Transaction, + TransactionViolation, +} from '@src/types/onyx'; import type Beta from '@src/types/onyx/Beta'; import type {ReportAttributes} from '@src/types/onyx/DerivedValues'; import type {Errors} from '@src/types/onyx/OnyxCommon'; @@ -15,7 +25,6 @@ import type PriorityMode from '@src/types/onyx/PriorityMode'; import type Report from '@src/types/onyx/Report'; import type ReportAction from '@src/types/onyx/ReportAction'; import {extractCollectionItemID} from './CollectionUtils'; -import {hasValidDraftComment} from './DraftCommentUtils'; import {translateLocal} from './Localize'; import {getLastActorDisplayName, getLastMessageTextForReport, getPersonalDetailsForAccountIDs, shouldShowLastActorDisplayName} from './OptionsListUtils'; import Parser from './Parser'; @@ -206,6 +215,7 @@ function shouldDisplayReportInLHN( transactionViolations: OnyxCollection, isReportArchived?: boolean, reportAttributes?: ReportAttributesDerivedValue['reports'], + draftReportComments?: DraftReportComments, ) { if (!report) { return {shouldDisplay: false}; @@ -236,7 +246,12 @@ function shouldDisplayReportInLHN( // Check if report should override hidden status const isSystemChat = isSystemChatUtil(report); const shouldOverrideHidden = - hasValidDraftComment(report.reportID) || hasErrorsOtherThanFailedReceipt || isFocused || isSystemChat || !!report.isPinned || reportAttributes?.[report?.reportID]?.requiresAttention; + !!draftReportComments?.[report.reportID] || + hasErrorsOtherThanFailedReceipt || + isFocused || + isSystemChat || + !!report.isPinned || + reportAttributes?.[report?.reportID]?.requiresAttention; if (isHidden && !shouldOverrideHidden) { return {shouldDisplay: false}; @@ -267,6 +282,7 @@ function getReportsToDisplayInLHN( transactionViolations: OnyxCollection, reportNameValuePairs?: OnyxCollection, reportAttributes?: ReportAttributesDerivedValue['reports'], + draftReportComments?: DraftReportComments, ) { const isInFocusMode = priorityMode === CONST.PRIORITY_MODE.GSD; const allReportsDictValues = reports ?? {}; @@ -286,6 +302,7 @@ function getReportsToDisplayInLHN( transactionViolations, isArchivedReport(reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`]), reportAttributes, + draftReportComments, ); if (shouldDisplay) { @@ -303,10 +320,10 @@ function updateReportsToDisplayInLHN( currentReportId: string | undefined, isInFocusMode: boolean, betas: OnyxEntry, - policies: OnyxCollection, transactionViolations: OnyxCollection, reportNameValuePairs?: OnyxCollection, reportAttributes?: ReportAttributesDerivedValue['reports'], + draftReportComments?: DraftReportComments, ) { const displayedReportsCopy = {...displayedReports}; updatedReportsKeys.forEach((reportID) => { @@ -324,6 +341,7 @@ function updateReportsToDisplayInLHN( transactionViolations, isArchivedReport(reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${report.reportID}`]), reportAttributes, + draftReportComments, ); if (shouldDisplay) { @@ -345,6 +363,7 @@ function sortReportsToDisplayInLHN( localeCompare: LocaleContextProps['localeCompare'], reportNameValuePairs?: OnyxCollection, reportAttributes?: ReportAttributesDerivedValue['reports'], + draftReportComments?: DraftReportComments, ): string[] { Performance.markStart(CONST.TIMING.GET_ORDERED_REPORT_IDS); const isInFocusMode = priorityMode === CONST.PRIORITY_MODE.GSD; @@ -381,7 +400,7 @@ function sortReportsToDisplayInLHN( pinnedAndGBRReports.push(miniReport); } else if (report?.hasErrorsOtherThanFailedReceipt) { errorReports.push(miniReport); - } else if (hasValidDraftComment(report?.reportID)) { + } else if (draftReportComments?.[report?.reportID]) { draftReports.push(miniReport); } else if (isArchivedNonExpenseReport(report, !!rNVPs?.private_isArchived)) { archivedReports.push(miniReport); diff --git a/src/libs/actions/App.ts b/src/libs/actions/App.ts index b18bb62c2c23..d375747f2dfb 100644 --- a/src/libs/actions/App.ts +++ b/src/libs/actions/App.ts @@ -315,7 +315,7 @@ function getOnyxDataForOpenOrReconnect( // This ensures that any report with a draft comment is preserved in Onyx even if it doesn’t contain chat history const reportsWithDraftComments = Object.entries(allReportsWithDraftComments ?? {}) .filter(([, value]) => value !== null) - .map(([key]) => key.replace(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, '')) + .map(([key]) => key.replace(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, '')) .map((reportID) => allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]); reportsWithDraftComments?.forEach((report) => { diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index c8157321941a..0a903de5bcee 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -758,17 +758,19 @@ Onyx.connect({ const policyID = key.replace(ONYXKEYS.COLLECTION.POLICY, ''); const policyReports = getAllPolicyReports(policyID); const cleanUpMergeQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT}${string}`, NullishDeep> = {}; - const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${string}` | `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; + const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; + const cleanUpDrafts: Record = {}; policyReports.forEach((policyReport) => { if (!policyReport) { return; } const {reportID} = policyReport; - cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`] = null; + cleanUpDrafts[reportID] = null; cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}`] = null; }); Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, cleanUpMergeQueries); Onyx.multiSet(cleanUpSetQueries); + Onyx.merge(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, cleanUpDrafts); delete allPolicies[key]; return; } diff --git a/src/libs/actions/Policy/DistanceRate.ts b/src/libs/actions/Policy/DistanceRate.ts index 4bc028d444cd..023e2ea16b54 100644 --- a/src/libs/actions/Policy/DistanceRate.ts +++ b/src/libs/actions/Policy/DistanceRate.ts @@ -37,17 +37,19 @@ Onyx.connect({ const policyID = key.replace(ONYXKEYS.COLLECTION.POLICY, ''); const policyReports = ReportUtils.getAllPolicyReports(policyID); const cleanUpMergeQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT}${string}`, NullishDeep> = {}; - const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${string}` | `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; + const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; + const cleanUpDrafts: Record = {}; policyReports.forEach((policyReport) => { if (!policyReport) { return; } const {reportID} = policyReport; - cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`] = null; + cleanUpDrafts[reportID] = null; cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}`] = null; }); Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, cleanUpMergeQueries); Onyx.multiSet(cleanUpSetQueries); + Onyx.merge(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, cleanUpDrafts); delete allPolicies[key]; return; } diff --git a/src/libs/actions/Policy/Member.ts b/src/libs/actions/Policy/Member.ts index 8dc578a4abb6..a1d69d13692c 100644 --- a/src/libs/actions/Policy/Member.ts +++ b/src/libs/actions/Policy/Member.ts @@ -63,17 +63,19 @@ Onyx.connect({ const policyID = key.replace(ONYXKEYS.COLLECTION.POLICY, ''); const policyReports = ReportUtils.getAllPolicyReports(policyID); const cleanUpMergeQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT}${string}`, NullishDeep> = {}; - const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${string}` | `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; + const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; + const cleanUpDrafts: Record = {}; policyReports.forEach((policyReport) => { if (!policyReport) { return; } const {reportID} = policyReport; - cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`] = null; + cleanUpDrafts[reportID] = null; cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}`] = null; }); Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, cleanUpMergeQueries); Onyx.multiSet(cleanUpSetQueries); + Onyx.merge(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, cleanUpDrafts); delete allPolicies[key]; return; } diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 68394dabe7ab..70a0d0ba1b39 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -173,17 +173,19 @@ Onyx.connect({ const policyID = key.replace(ONYXKEYS.COLLECTION.POLICY, ''); const policyReports = ReportUtils.getAllPolicyReports(policyID); const cleanUpMergeQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT}${string}`, NullishDeep> = {}; - const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${string}` | `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; + const cleanUpDrafts: Record = {}; + const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; policyReports.forEach((policyReport) => { if (!policyReport) { return; } const {reportID} = policyReport; - cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`] = null; + cleanUpDrafts[reportID] = null; cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}`] = null; }); Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, cleanUpMergeQueries); Onyx.multiSet(cleanUpSetQueries); + Onyx.merge(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, cleanUpDrafts); delete allPolicies[key]; return; } diff --git a/src/libs/actions/Policy/ReportField.ts b/src/libs/actions/Policy/ReportField.ts index 85a25754b7e1..f08280c5ae97 100644 --- a/src/libs/actions/Policy/ReportField.ts +++ b/src/libs/actions/Policy/ReportField.ts @@ -61,17 +61,19 @@ Onyx.connect({ const policyID = key.replace(ONYXKEYS.COLLECTION.POLICY, ''); const policyReports = ReportUtils.getAllPolicyReports(policyID); const cleanUpMergeQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT}${string}`, NullishDeep> = {}; - const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${string}` | `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; + const cleanUpDrafts: Record = {}; + const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; policyReports.forEach((policyReport) => { if (!policyReport) { return; } const {reportID} = policyReport; - cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`] = null; + cleanUpDrafts[reportID] = null; cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}`] = null; }); Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, cleanUpMergeQueries); Onyx.multiSet(cleanUpSetQueries); + Onyx.merge(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, cleanUpDrafts); delete allPolicies[key]; return; } diff --git a/src/libs/actions/Policy/Tag.ts b/src/libs/actions/Policy/Tag.ts index 2f98884b9cfd..4fbaaec634b0 100644 --- a/src/libs/actions/Policy/Tag.ts +++ b/src/libs/actions/Policy/Tag.ts @@ -49,17 +49,16 @@ Onyx.connect({ const policyID = key.replace(ONYXKEYS.COLLECTION.POLICY, ''); const policyReports = ReportUtils.getAllPolicyReports(policyID); const cleanUpMergeQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT}${string}`, NullishDeep> = {}; - const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${string}` | `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; + const cleanUpDrafts: Record = {}; policyReports.forEach((policyReport) => { if (!policyReport) { return; } const {reportID} = policyReport; - cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`] = null; - cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}`] = null; + cleanUpDrafts[reportID] = null; }); Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, cleanUpMergeQueries); - Onyx.multiSet(cleanUpSetQueries); + Onyx.merge(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, cleanUpDrafts); delete allPolicies[key]; return; } diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 270df4f3c295..1ef72b987257 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -171,6 +171,7 @@ import INPUT_IDS from '@src/types/form/NewRoomForm'; import type { Account, DismissedProductTraining, + DraftReportComments, IntroSelected, InvitedEmailsToAccountIDs, NewGroupChatDraft, @@ -399,10 +400,9 @@ Onyx.connect({ callback: (val) => (introSelected = val), }); -let allReportDraftComments: Record = {}; +let allReportDraftComments: OnyxEntry = {}; Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, - waitForCollectionCallback: true, + key: ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, callback: (value) => (allReportDraftComments = value), }); @@ -425,16 +425,18 @@ Onyx.connect({ // More info: https://github.com/Expensify/App/issues/14260 const policyID = key.replace(ONYXKEYS.COLLECTION.POLICY, ''); const policyReports = getAllPolicyReports(policyID); - const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${string}` | `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; + const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {}; + const cleanUpDrafts: Record = {}; policyReports.forEach((policyReport) => { if (!policyReport) { return; } const {reportID} = policyReport; - cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`] = null; + cleanUpDrafts[reportID] = null; cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}`] = null; }); Onyx.multiSet(cleanUpSetQueries); + Onyx.merge(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, cleanUpDrafts); delete allPolicies[key]; return; } @@ -1759,10 +1761,22 @@ function saveReportDraft(reportID: string, report: Report) { /** * Saves the comment left by the user as they are typing. By saving this data the user can switch between chats, close * tab, refresh etc without worrying about loosing what they typed out. - * When empty string or null is passed, it will delete the draft comment from Onyx store. */ function saveReportDraftComment(reportID: string, comment: string | null, callback: () => void = () => {}) { - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`, prepareDraftComment(comment)).then(callback); + API.write( + WRITE_COMMANDS.SAVE_REPORT_DRAFT_COMMENT, + { + reportID, + // comment is quoted to intentionally preserve trailing whitespace as the user types + // otherwise it will be trimmed by the WAF _and_ Auth's SParseHTTP function + comment: `"${comment}"`, + }, + {optimisticData: [{onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, value: {[reportID]: prepareDraftComment(comment)}}]}, + { + checkAndFixConflictingRequest: (persistedRequests) => + resolveDuplicationConflictAction(persistedRequests, (request) => request.command === WRITE_COMMANDS.SAVE_REPORT_DRAFT_COMMENT && request.data?.reportID === reportID), + }, + ).then(callback); } /** Broadcasts whether or not a user is typing on a report over the report's private pusher channel. */ @@ -1815,7 +1829,7 @@ function handleReportChanged(report: OnyxEntry) { // Replacing the existing report's participants to avoid duplicates participants: existingReport?.participants ?? report.participants, }); - Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`, null); + Onyx.merge(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, {[reportID]: null}); }; // Only re-route them if they are still looking at the optimistically created report if (Navigation.getActiveRoute().includes(`/r/${reportID}`)) { @@ -1837,7 +1851,7 @@ function handleReportChanged(report: OnyxEntry) { // In case the user is not on the report screen, we will transfer the report draft comment directly to the existing report // after that clear the optimistically created report - const draftReportComment = allReportDraftComments?.[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`]; + const draftReportComment = allReportDraftComments?.[reportID]; if (!draftReportComment) { callback(); return; diff --git a/src/libs/migrateOnyx.ts b/src/libs/migrateOnyx.ts index f0a9ec9db977..f928ff262048 100644 --- a/src/libs/migrateOnyx.ts +++ b/src/libs/migrateOnyx.ts @@ -1,5 +1,6 @@ import Log from './Log'; import KeyReportActionsDraftByReportActionID from './migrations/KeyReportActionsDraftByReportActionID'; +import MoveDraftsToNVP from './migrations/MoveDraftsToNVP'; import MoveIsOptimisticReportToMetadata from './migrations/MoveIsOptimisticReportToMetadata'; import NVPMigration from './migrations/NVPMigration'; import PendingMembersToMetadata from './migrations/PendingMembersToMetadata'; @@ -25,6 +26,7 @@ export default function () { PronounsMigration, MoveIsOptimisticReportToMetadata, PendingMembersToMetadata, + MoveDraftsToNVP, ]; // Reduce all promises down to a single promise. All promises run in a linear fashion, waiting for the diff --git a/src/libs/migrations/MoveDraftsToNVP.ts b/src/libs/migrations/MoveDraftsToNVP.ts new file mode 100644 index 000000000000..eb2b1851ee5e --- /dev/null +++ b/src/libs/migrations/MoveDraftsToNVP.ts @@ -0,0 +1,43 @@ +import Onyx from 'react-native-onyx'; +import type {OnyxCollection, OnyxKey, OnyxMultiSetInput} from 'react-native-onyx'; +import Log from '@libs/Log'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {DraftReportComments} from '@src/types/onyx'; + +// moves individual drafts from `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}` to ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS +export default function (): Promise { + return new Promise((resolve) => { + // eslint-disable-next-line rulesdir/no-onyx-connect + const connection = Onyx.connect({ + // eslint-disable-next-line deprecation/deprecation + key: ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, + waitForCollectionCallback: true, + callback: (drafts: OnyxCollection) => { + Onyx.disconnect(connection); + + if (!drafts) { + Log.info('[Migrate Onyx] Skipped migration MoveDraftsToNVP because there were no drafts'); + return resolve(); + } + + const newDrafts: DraftReportComments = {}; + const draftsToClear: OnyxMultiSetInput = {}; + for (const [reportOnyxKey, draft] of Object.entries(drafts)) { + if (!draft) { + continue; + } + // eslint-disable-next-line deprecation/deprecation + newDrafts[reportOnyxKey.replace(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, '')] = draft; + draftsToClear[reportOnyxKey as OnyxKey] = null; + } + + // eslint-disable-next-line rulesdir/prefer-actions-set-data + Onyx.set(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, newDrafts); + + // eslint-disable-next-line rulesdir/prefer-actions-set-data + Onyx.multiSet(draftsToClear); + resolve(); + }, + }); + }); +} diff --git a/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx b/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx index ea17a85766d9..9303433213c3 100644 --- a/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx +++ b/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx @@ -34,20 +34,18 @@ import canFocusInputOnScreenFocus from '@libs/canFocusInputOnScreenFocus'; import {forceClearInput} from '@libs/ComponentUtils'; import {canSkipTriggerHotkeys, findCommonSuffixLength, insertText, insertWhiteSpaceAtIndex} from '@libs/ComposerUtils'; import convertToLTRForComposer from '@libs/convertToLTRForComposer'; -import {getDraftComment} from '@libs/DraftCommentUtils'; import {containsOnlyEmojis, extractEmojis, getAddedEmojis, getPreferredSkinToneIndex, replaceAndExtractEmojis} from '@libs/EmojiUtils'; import focusComposerWithDelay from '@libs/focusComposerWithDelay'; import getPlatform from '@libs/getPlatform'; import {addKeyDownPressListener, removeKeyDownPressListener} from '@libs/KeyboardShortcut/KeyDownPressListener'; import Parser from '@libs/Parser'; import ReportActionComposeFocusManager from '@libs/ReportActionComposeFocusManager'; -import {isValidReportIDFromPath, shouldAutoFocusOnKeyPress} from '@libs/ReportUtils'; +import {shouldAutoFocusOnKeyPress} from '@libs/ReportUtils'; import updateMultilineInputRange from '@libs/updateMultilineInputRange'; import willBlurTextInputOnTapOutsideFunc from '@libs/willBlurTextInputOnTapOutside'; import getCursorPosition from '@pages/home/report/ReportActionCompose/getCursorPosition'; import getScrollPosition from '@pages/home/report/ReportActionCompose/getScrollPosition'; import type {SuggestionsRef} from '@pages/home/report/ReportActionCompose/ReportActionCompose'; -import SilentCommentUpdater from '@pages/home/report/ReportActionCompose/SilentCommentUpdater'; import Suggestions from '@pages/home/report/ReportActionCompose/Suggestions'; import type {FileObject} from '@pages/media/AttachmentModalScreen/types'; import {isEmojiPickerVisible} from '@userActions/EmojiPickerAction'; @@ -248,14 +246,18 @@ function ComposerWithSuggestions( const mobileInputScrollPosition = useRef(0); const cursorPositionValue = useSharedValue({x: 0, y: 0}); const tag = useSharedValue(-1); - const draftComment = getDraftComment(reportID) ?? ''; + const [draftReportComment] = useOnyx(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, {canBeMissing: true, selector: (draftReportComments) => draftReportComments?.[reportID]}); const [value, setValue] = useState(() => { - if (draftComment) { - emojisPresentBefore.current = extractEmojis(draftComment); + if (draftReportComment) { + emojisPresentBefore.current = extractEmojis(draftReportComment); } - return draftComment; + return draftReportComment ?? ''; }); + useEffect(() => { + setValue(draftReportComment ?? ''); + }, [draftReportComment]); + const commentRef = useRef(value); const [modal] = useOnyx(ONYXKEYS.MODAL, {canBeMissing: true}); @@ -375,7 +377,7 @@ function ComposerWithSuggestions( * Update the value of the comment in Onyx */ const updateComment = useCallback( - (commentValue: string, shouldDebounceSaveComment?: boolean) => { + (commentValue: string) => { raiseIsScrollLikelyLayoutTriggered(); const {startIndex, endIndex, diff} = findNewlyAddedChars(lastTextRef.current, commentValue); const isEmojiInserted = diff.length && endIndex > startIndex && diff.trim() === diff && containsOnlyEmojis(diff); @@ -417,12 +419,9 @@ function ComposerWithSuggestions( } commentRef.current = newCommentConverted; - if (shouldDebounceSaveComment) { - isCommentPendingSaved.current = true; - debouncedSaveReportComment(reportID, newCommentConverted); - } else { - saveReportDraftComment(reportID, newCommentConverted); - } + + isCommentPendingSaved.current = true; + debouncedSaveReportComment(reportID, newCommentConverted); if (newCommentConverted) { debouncedBroadcastUserIsTyping(reportID); } @@ -437,7 +436,7 @@ function ComposerWithSuggestions( (text: string) => { // selection replacement should be debounced to avoid conflicts with text typing // (f.e. when emoji is being picked and 1 second still did not pass after user finished typing) - updateComment(insertText(commentRef.current, selection, text), true); + updateComment(insertText(commentRef.current, selection, text)); }, [selection, updateComment], ); @@ -495,7 +494,7 @@ function ComposerWithSuggestions( positionX: prevSelection.positionX, positionY: prevSelection.positionY, })); - updateComment(newText, true); + updateComment(newText); } } }, @@ -504,7 +503,7 @@ function ComposerWithSuggestions( const onChangeText = useCallback( (commentValue: string) => { - updateComment(commentValue, true); + updateComment(commentValue); if (isIOSNative && syncSelectionWithOnChangeTextRef.current) { const positionSnapshot = syncSelectionWithOnChangeTextRef.current.position; @@ -726,7 +725,7 @@ function ComposerWithSuggestions( mobileInputScrollPosition.current = 0; // Note: use the value when the clear happened, not the current value which might have changed already onCleared(text); - updateComment('', true); + updateComment(''); }, [onCleared, updateComment], ); @@ -844,16 +843,6 @@ function ComposerWithSuggestions( resetKeyboardInput={resetKeyboardInput} /> - {isValidReportIDFromPath(reportID) && ( - - )} - {/* Only used for testing so far */} {children} diff --git a/src/pages/home/report/ReportActionCompose/SilentCommentUpdater/index.android.tsx b/src/pages/home/report/ReportActionCompose/SilentCommentUpdater/index.android.tsx deleted file mode 100644 index b64672b28f9f..000000000000 --- a/src/pages/home/report/ReportActionCompose/SilentCommentUpdater/index.android.tsx +++ /dev/null @@ -1,29 +0,0 @@ -import {useEffect} from 'react'; -import useOnyx from '@hooks/useOnyx'; -import ONYXKEYS from '@src/ONYXKEYS'; -import type SilentCommentUpdaterProps from './types'; - -/** - * Adding .android component to disable updating comment when prev comment is different - * it fixes issue on Android, assuming we don't need tab sync on mobiles - https://github.com/Expensify/App/issues/28562 - */ - -/** - * This component doesn't render anything. It runs a side effect to update the comment of a report under certain conditions. - * It is connected to the actual draft comment in onyx. The comment in onyx might updates multiple times, and we want to avoid - * re-rendering a UI component for that. That's why the side effect was moved down to a separate component. - */ -function SilentCommentUpdater({updateComment, reportID}: SilentCommentUpdaterProps) { - const [comment = ''] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`, {canBeMissing: true}); - - useEffect(() => { - updateComment(comment); - // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps -- We need to run this on mount - }, []); - - return null; -} - -SilentCommentUpdater.displayName = 'SilentCommentUpdater'; - -export default SilentCommentUpdater; diff --git a/src/pages/home/report/ReportActionCompose/SilentCommentUpdater/index.tsx b/src/pages/home/report/ReportActionCompose/SilentCommentUpdater/index.tsx deleted file mode 100644 index efe1c79c28d8..000000000000 --- a/src/pages/home/report/ReportActionCompose/SilentCommentUpdater/index.tsx +++ /dev/null @@ -1,43 +0,0 @@ -import {useEffect} from 'react'; -import useLocalize from '@hooks/useLocalize'; -import useOnyx from '@hooks/useOnyx'; -import usePrevious from '@hooks/usePrevious'; -import ONYXKEYS from '@src/ONYXKEYS'; -import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; -import type SilentCommentUpdaterProps from './types'; - -/** - * This component doesn't render anything. It runs a side effect to update the comment of a report under certain conditions. - * It is connected to the actual draft comment in onyx. The comment in onyx might updates multiple times, and we want to avoid - * re-rendering a UI component for that. That's why the side effect was moved down to a separate component. - */ -function SilentCommentUpdater({commentRef, reportID, value, updateComment, isCommentPendingSaved}: SilentCommentUpdaterProps) { - const [comment = '', commentResult] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`, {canBeMissing: true}); - const prevCommentProp = usePrevious(comment); - const prevReportId = usePrevious(reportID); - const {preferredLocale} = useLocalize(); - const prevPreferredLocale = usePrevious(preferredLocale); - - useEffect(() => { - if (isLoadingOnyxValue(commentResult)) { - return; - } - // Value state does not have the same value as comment props when the comment gets changed from another tab. - // In this case, we should synchronize the value between tabs. - const shouldSyncComment = prevCommentProp !== comment && value !== comment && !isCommentPendingSaved.current; - - // As the report IDs change, make sure to update the composer comment as we need to make sure - // we do not show incorrect data in there (ie. draft of message from other report). - if (preferredLocale === prevPreferredLocale && reportID === prevReportId && !shouldSyncComment) { - return; - } - - updateComment(comment ?? ''); - }, [prevCommentProp, prevPreferredLocale, prevReportId, comment, preferredLocale, reportID, updateComment, value, commentRef, isCommentPendingSaved, commentResult]); - - return null; -} - -SilentCommentUpdater.displayName = 'SilentCommentUpdater'; - -export default SilentCommentUpdater; diff --git a/src/pages/home/report/ReportActionCompose/SilentCommentUpdater/types.ts b/src/pages/home/report/ReportActionCompose/SilentCommentUpdater/types.ts deleted file mode 100644 index 2768dfc1250a..000000000000 --- a/src/pages/home/report/ReportActionCompose/SilentCommentUpdater/types.ts +++ /dev/null @@ -1,18 +0,0 @@ -type SilentCommentUpdaterProps = { - /** Updates the comment */ - updateComment: (comment: string) => void; - - /** The ID of the report associated with the comment */ - reportID: string; - - /** The value of the comment */ - value: string; - - /** The ref of the comment */ - commentRef: React.RefObject; - - /** The ref to check whether the comment saving is in progress */ - isCommentPendingSaved: React.RefObject; -}; - -export default SilentCommentUpdaterProps; diff --git a/src/pages/home/report/ReportActionCompose/SuggestionEmoji.tsx b/src/pages/home/report/ReportActionCompose/SuggestionEmoji.tsx index 180718a4a670..dda9a41879be 100644 --- a/src/pages/home/report/ReportActionCompose/SuggestionEmoji.tsx +++ b/src/pages/home/report/ReportActionCompose/SuggestionEmoji.tsx @@ -72,7 +72,7 @@ function SuggestionEmoji( const emojiCode = emojiObject?.types?.at(preferredSkinTone) && preferredSkinTone !== -1 ? emojiObject.types.at(preferredSkinTone) : emojiObject?.code; const commentAfterColonWithEmojiNameRemoved = value.slice(selection.end); - updateComment(`${commentBeforeColon}${emojiCode} ${trimLeadingSpace(commentAfterColonWithEmojiNameRemoved)}`, true); + updateComment(`${commentBeforeColon}${emojiCode} ${trimLeadingSpace(commentAfterColonWithEmojiNameRemoved)}`); // In some Android phones keyboard, the text to search for the emoji is not cleared // will be added after the user starts typing again on the keyboard. This package is diff --git a/src/pages/home/report/ReportActionCompose/SuggestionMention.tsx b/src/pages/home/report/ReportActionCompose/SuggestionMention.tsx index a094e27b8165..880f35979c9e 100644 --- a/src/pages/home/report/ReportActionCompose/SuggestionMention.tsx +++ b/src/pages/home/report/ReportActionCompose/SuggestionMention.tsx @@ -201,7 +201,7 @@ function SuggestionMention( suggestionValues.atSignIndex + Math.max(originalMention.length, suggestionValues.mentionPrefix.length + suggestionValues.prefixType.length), ); - updateComment(`${commentBeforeAtSign}${mentionCode} ${trimLeadingSpace(commentAfterMention)}`, true); + updateComment(`${commentBeforeAtSign}${mentionCode} ${trimLeadingSpace(commentAfterMention)}`); const selectionPosition = suggestionValues.atSignIndex + mentionCode.length + CONST.SPACE_LENGTH; setSelection({ start: selectionPosition, diff --git a/src/pages/home/report/ReportActionCompose/Suggestions.tsx b/src/pages/home/report/ReportActionCompose/Suggestions.tsx index 8b7171340b63..74efae903116 100644 --- a/src/pages/home/report/ReportActionCompose/Suggestions.tsx +++ b/src/pages/home/report/ReportActionCompose/Suggestions.tsx @@ -21,7 +21,7 @@ type SuggestionProps = { setSelection: (newSelection: TextSelection) => void; /** Callback to update the comment draft */ - updateComment: (newComment: string, shouldDebounceSaveComment?: boolean) => void; + updateComment: (newComment: string) => void; /** Measures the parent container's position and dimensions. Also add cursor coordinates */ measureParentContainerAndReportCursor: (callback: MeasureParentContainerAndCursorCallback) => void; diff --git a/src/types/onyx/DraftReportComments.ts b/src/types/onyx/DraftReportComments.ts new file mode 100644 index 000000000000..385a9623a36a --- /dev/null +++ b/src/types/onyx/DraftReportComments.ts @@ -0,0 +1,6 @@ +/** + * Map of reportID => comment + */ +type DraftReportComments = Record; + +export default DraftReportComments; diff --git a/src/types/onyx/index.ts b/src/types/onyx/index.ts index a0185b731e3a..c17b94663ed2 100644 --- a/src/types/onyx/index.ts +++ b/src/types/onyx/index.ts @@ -27,6 +27,7 @@ import type {ReportAttributesDerivedValue, ReportTransactionsAndViolationsDerive import type DismissedProductTraining from './DismissedProductTraining'; import type DismissedReferralBanners from './DismissedReferralBanners'; import type Download from './Download'; +import type DraftReportComments from './DraftReportComments'; import type ExpensifyCardBankAccountMetadata from './ExpensifyCardBankAccountMetadata'; import type ExpensifyCardSettings from './ExpensifyCardSettings'; import type FrequentlyUsedEmoji from './FrequentlyUsedEmoji'; @@ -270,4 +271,5 @@ export type { BillingReceiptDetails, IntegrationServerExportTemplate, HybridApp, + DraftReportComments, }; diff --git a/tests/ui/LHNItemsPresence.tsx b/tests/ui/LHNItemsPresence.tsx index 95374059dd7f..4e01be0ceafc 100644 --- a/tests/ui/LHNItemsPresence.tsx +++ b/tests/ui/LHNItemsPresence.tsx @@ -177,7 +177,7 @@ describe('SidebarLinksData', () => { await waitForBatchedUpdatesWithAct(); // And a draft message is added to the report. - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${draftReport.reportID}`, 'draft report message'); + await Onyx.merge(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, {[draftReport.reportID]: 'draft report message'}); // Then the sidebar should display the draft report. expect(getDisplayNames()).toHaveLength(1); diff --git a/tests/unit/DebugUtilsTest.ts b/tests/unit/DebugUtilsTest.ts index b734111741c7..a0798a14b274 100644 --- a/tests/unit/DebugUtilsTest.ts +++ b/tests/unit/DebugUtilsTest.ts @@ -739,7 +739,7 @@ describe('DebugUtils', () => { expect(reason).toBeNull(); }); it('returns correct reason when report has a valid draft comment', async () => { - await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}1`, 'Hello world!'); + await Onyx.set(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, {[baseReport.reportID]: 'Hello world!'}); const reason = DebugUtils.getReasonForShowingRowInLHN(baseReport, chatReportR14932); expect(reason).toBe('debug.reasonVisibleInLHN.hasDraftComment'); }); diff --git a/tests/unit/ReportUtilsTest.ts b/tests/unit/ReportUtilsTest.ts index af4603de155a..4b68ad91e379 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -2295,7 +2295,7 @@ describe('ReportUtils', () => { const isInFocusMode = false; const betas = [CONST.BETAS.DEFAULT_ROOMS]; - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report.reportID}`, 'fake draft'); + await Onyx.merge(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, {[report.reportID]: 'fake draft'}); expect( shouldReportBeInOptionList({ diff --git a/tests/unit/SidebarFilterTest.ts b/tests/unit/SidebarFilterTest.ts index f2b3078994c1..7e35b7108cb0 100644 --- a/tests/unit/SidebarFilterTest.ts +++ b/tests/unit/SidebarFilterTest.ts @@ -27,6 +27,7 @@ const ONYXKEYS = { REPORT_DRAFT_COMMENT: 'reportDraftComment_', }, NETWORK: 'network', + NVP_DRAFT_REPORT_COMMENTS: 'nvp_draftReportComments', } as const; // We need to fix this test as a follow up. There seems to be some problems with memory after filtering got more complicated. @@ -120,7 +121,7 @@ xdescribe('Sidebar', () => { Onyx.multiSet({ [ONYXKEYS.PERSONAL_DETAILS_LIST]: LHNTestUtils.fakePersonalDetails, [ONYXKEYS.IS_LOADING_APP]: false, - [`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report.reportID}`]: 'This is a draft message', + [ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS]: {[report.reportID]: 'This is a draft message'}, ...reportCollectionDataSet, }), ) @@ -495,7 +496,7 @@ xdescribe('Sidebar', () => { [ONYXKEYS.NVP_PRIORITY_MODE]: CONST.PRIORITY_MODE.GSD, [ONYXKEYS.PERSONAL_DETAILS_LIST]: LHNTestUtils.fakePersonalDetails, [ONYXKEYS.IS_LOADING_APP]: false, - [`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${draftReport.reportID}`]: 'draft report message', + [ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS]: {[draftReport.reportID]: 'draft report message'}, ...reportCollectionDataSet, }), ) @@ -719,7 +720,7 @@ xdescribe('Sidebar', () => { [ONYXKEYS.PERSONAL_DETAILS_LIST]: LHNTestUtils.fakePersonalDetails, [ONYXKEYS.IS_LOADING_APP]: false, [`${ONYXKEYS.COLLECTION.POLICY}${policy.policyID}`]: policy, - [`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report2.reportID}`]: hasDraft ? 'report2 draft' : null, + [ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS]: {[report2.reportID]: hasDraft ? 'report2 draft' : null}, ...reportCollectionDataSet, }), ) diff --git a/tests/unit/SidebarOrderTest.ts b/tests/unit/SidebarOrderTest.ts index 84f967b5a1db..0efe35d485a5 100644 --- a/tests/unit/SidebarOrderTest.ts +++ b/tests/unit/SidebarOrderTest.ts @@ -187,7 +187,7 @@ describe('Sidebar', () => { [ONYXKEYS.NVP_PRIORITY_MODE]: CONST.PRIORITY_MODE.DEFAULT, [ONYXKEYS.PERSONAL_DETAILS_LIST]: LHNTestUtils.fakePersonalDetails, [ONYXKEYS.IS_LOADING_APP]: false, - [`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report1.reportID}`]: 'report1 draft', + [ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS]: {[report1.reportID]: 'report1 draft'}, ...reportCollectionDataSet, }), ) @@ -501,7 +501,7 @@ describe('Sidebar', () => { [ONYXKEYS.NVP_PRIORITY_MODE]: CONST.PRIORITY_MODE.DEFAULT, [ONYXKEYS.PERSONAL_DETAILS_LIST]: LHNTestUtils.fakePersonalDetails, [ONYXKEYS.IS_LOADING_APP]: false, - [ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT + report2.reportID]: 'This is a draft', + [ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS]: {[report2.reportID]: 'This is a draft'}, ...reportCollectionDataSet, }), ) @@ -548,7 +548,7 @@ describe('Sidebar', () => { [ONYXKEYS.NVP_PRIORITY_MODE]: CONST.PRIORITY_MODE.DEFAULT, [ONYXKEYS.PERSONAL_DETAILS_LIST]: LHNTestUtils.fakePersonalDetails, [ONYXKEYS.IS_LOADING_APP]: false, - [`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report.reportID}`]: 'This is a draft', + [ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS]: {[report.reportID]: 'This is a draft'}, ...reportCollectionDataSet, }), ) @@ -559,7 +559,7 @@ describe('Sidebar', () => { }) // When the draft is removed - .then(() => Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report.reportID}`, null)) + .then(() => Onyx.merge(ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, {[report.reportID]: null})) // Then the pencil icon goes away .then(() => { @@ -674,7 +674,7 @@ describe('Sidebar', () => { [ONYXKEYS.NVP_PRIORITY_MODE]: CONST.PRIORITY_MODE.DEFAULT, [ONYXKEYS.PERSONAL_DETAILS_LIST]: LHNTestUtils.fakePersonalDetails, [ONYXKEYS.IS_LOADING_APP]: false, - [`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report2.reportID}`]: 'Report2 draft comment', + [ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS]: {[report2.reportID]: 'Report2 draft comment'}, ...reportCollectionDataSet, }), ) @@ -785,9 +785,11 @@ describe('Sidebar', () => { }; const reportDraftCommentCollectionDataSet = { - [`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report1.reportID}`]: 'report1 draft', - [`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report2.reportID}`]: 'report2 draft', - [`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report3.reportID}`]: 'report3 draft', + [ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS]: { + [report1.reportID]: 'report1 draft', + [report2.reportID]: 'report2 draft', + [report3.reportID]: 'report3 draft', + }, }; return ( @@ -819,7 +821,7 @@ describe('Sidebar', () => { .then(() => Onyx.multiSet({ ...reportDraftCommentCollectionDataSet, - [`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report4.reportID}`]: 'report4 draft', + [ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS]: {[report4.reportID]: 'report4 draft'}, [`${ONYXKEYS.COLLECTION.REPORT}${report4.reportID}`]: report4, ...reportCollectionDataSet, }), From 63304f4c36c281a23200918aa98731f6a5cccf1f Mon Sep 17 00:00:00 2001 From: Chuck Dries Date: Thu, 31 Jul 2025 14:24:42 -0700 Subject: [PATCH 2/4] Rename comment->reportComment in SaveReportDraftCommentParams --- src/libs/API/parameters/SaveReportDraftCommentParams.ts | 2 +- src/libs/actions/Report.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/API/parameters/SaveReportDraftCommentParams.ts b/src/libs/API/parameters/SaveReportDraftCommentParams.ts index 6b89e5c3ac05..583742b7488f 100644 --- a/src/libs/API/parameters/SaveReportDraftCommentParams.ts +++ b/src/libs/API/parameters/SaveReportDraftCommentParams.ts @@ -1,6 +1,6 @@ type SaveReportDraftCommentParams = { reportID: string; - comment: string; + reportComment: string; }; export default SaveReportDraftCommentParams; diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 1ef72b987257..1382582ba29d 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -1767,9 +1767,9 @@ function saveReportDraftComment(reportID: string, comment: string | null, callba WRITE_COMMANDS.SAVE_REPORT_DRAFT_COMMENT, { reportID, - // comment is quoted to intentionally preserve trailing whitespace as the user types + // reportComment is quoted to intentionally preserve trailing whitespace as the user types // otherwise it will be trimmed by the WAF _and_ Auth's SParseHTTP function - comment: `"${comment}"`, + reportComment: `"${comment}"`, }, {optimisticData: [{onyxMethod: Onyx.METHOD.MERGE, key: ONYXKEYS.NVP_DRAFT_REPORT_COMMENTS, value: {[reportID]: prepareDraftComment(comment)}}]}, { From e38a31eb2402f884fe2e7b05f95c4d1b02ca5488 Mon Sep 17 00:00:00 2001 From: Chuck Dries Date: Mon, 11 Aug 2025 16:12:19 -0700 Subject: [PATCH 3/4] Call setIsCommentEmpty and set commentRef when drafts sync from other devices --- .../ComposerWithSuggestions/ComposerWithSuggestions.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx b/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx index 9303433213c3..500052c5091b 100644 --- a/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx +++ b/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx @@ -254,11 +254,13 @@ function ComposerWithSuggestions( return draftReportComment ?? ''; }); + const commentRef = useRef(value); + useEffect(() => { setValue(draftReportComment ?? ''); - }, [draftReportComment]); - - const commentRef = useRef(value); + setIsCommentEmpty(!draftReportComment || !!draftReportComment.match(CONST.REGEX.EMPTY_COMMENT)); + commentRef.current = draftReportComment ?? ''; + }, [draftReportComment, setIsCommentEmpty]); const [modal] = useOnyx(ONYXKEYS.MODAL, {canBeMissing: true}); const [preferredSkinTone = CONST.EMOJI_DEFAULT_SKIN_TONE] = useOnyx(ONYXKEYS.PREFERRED_EMOJI_SKIN_TONE, {selector: getPreferredSkinToneIndex, canBeMissing: true}); From 895945be65b8040efb04fb07c3c639e4f0a0a12b Mon Sep 17 00:00:00 2001 From: Chuck Dries Date: Thu, 14 Aug 2025 15:07:21 -0700 Subject: [PATCH 4/4] Update SidebarUtilTests for new drafts --- tests/unit/SidebarUtilsTest.ts | 54 +++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/tests/unit/SidebarUtilsTest.ts b/tests/unit/SidebarUtilsTest.ts index 1bdf5483fae9..aa7622bedb05 100644 --- a/tests/unit/SidebarUtilsTest.ts +++ b/tests/unit/SidebarUtilsTest.ts @@ -1478,14 +1478,15 @@ describe('SidebarUtils', () => { describe('sortReportsToDisplayInLHN', () => { describe('categorizeReportsForLHN', () => { it('should categorize reports into correct groups', () => { - // Given hasValidDraftComment is mocked to return true for report '2' - const {hasValidDraftComment} = require('@libs/DraftCommentUtils') as {hasValidDraftComment: jest.Mock}; - hasValidDraftComment.mockImplementation((reportID: string) => reportID === '2'); - const {reports, reportNameValuePairs, reportAttributes} = createSidebarTestData(); + // Given draftReportComments with a draft for report '2' + const draftReportComments = { + '2': 'This is a draft comment', + }; + // When the reports are categorized - const result = SidebarUtils.categorizeReportsForLHN(reports, reportNameValuePairs, reportAttributes); + const result = SidebarUtils.categorizeReportsForLHN(reports, reportNameValuePairs, reportAttributes, draftReportComments); // Then the reports are categorized into the correct groups expect(result.pinnedAndGBRReports).toHaveLength(1); @@ -1521,7 +1522,7 @@ describe('SidebarUtils', () => { }; // When the reports are categorized - const result = SidebarUtils.categorizeReportsForLHN(reports, undefined, reportAttributes); + const result = SidebarUtils.categorizeReportsForLHN(reports, undefined, reportAttributes, undefined); // Then the reports are categorized into the correct groups expect(result.pinnedAndGBRReports).toHaveLength(1); @@ -1548,7 +1549,7 @@ describe('SidebarUtils', () => { }; // When the reports are categorized - const result = SidebarUtils.categorizeReportsForLHN(reports); + const result = SidebarUtils.categorizeReportsForLHN(reports, undefined, undefined, undefined); // Then the reports are categorized into the correct groups expect(result.pinnedAndGBRReports).toHaveLength(0); @@ -1562,7 +1563,7 @@ describe('SidebarUtils', () => { it('should handle empty reports object', () => { // Given the reports are empty - const result = SidebarUtils.categorizeReportsForLHN({}); + const result = SidebarUtils.categorizeReportsForLHN({}, undefined, undefined, undefined); // Then the reports are categorized into the correct groups expect(result.pinnedAndGBRReports).toHaveLength(0); @@ -1571,6 +1572,37 @@ describe('SidebarUtils', () => { expect(result.nonArchivedReports).toHaveLength(0); expect(result.archivedReports).toHaveLength(0); }); + + it('should categorize draft reports using draftReportComments parameter', () => { + // Given reports with no drafts initially + const reports = createSidebarReportsCollection([ + { + reportName: 'Report 1', + isPinned: false, + hasErrorsOtherThanFailedReceipt: false, + }, + { + reportName: 'Report 2', + isPinned: false, + hasErrorsOtherThanFailedReceipt: false, + }, + ]); + + // Given draftReportComments with drafts for both reports + const draftReportComments = { + '0': 'Draft comment for report 1', + '1': 'Draft comment for report 2', + }; + + // When the reports are categorized + const result = SidebarUtils.categorizeReportsForLHN(reports, undefined, undefined, draftReportComments); + + // Then both reports should be categorized as draft reports + expect(result.draftReports).toHaveLength(2); + expect(result.draftReports.at(0)?.reportID).toBe('0'); + expect(result.draftReports.at(1)?.reportID).toBe('1'); + expect(result.nonArchivedReports).toHaveLength(0); + }); }); describe('sortCategorizedReports', () => { @@ -1799,7 +1831,7 @@ describe('SidebarUtils', () => { const priorityMode = CONST.PRIORITY_MODE.DEFAULT; // When the reports are sorted - const result = SidebarUtils.sortReportsToDisplayInLHN(reports, priorityMode, mockLocaleCompare); + const result = SidebarUtils.sortReportsToDisplayInLHN(reports, priorityMode, mockLocaleCompare, undefined, undefined, undefined); // Then the reports are sorted in the correct order expect(result).toEqual(['0', '1', '2']); // Pinned first, Error second, Normal third @@ -1825,10 +1857,10 @@ describe('SidebarUtils', () => { const mockLocaleCompare = (a: string, b: string) => a.localeCompare(b); // When the reports are sorted in default mode - const defaultResult = SidebarUtils.sortReportsToDisplayInLHN(reports, CONST.PRIORITY_MODE.DEFAULT, mockLocaleCompare); + const defaultResult = SidebarUtils.sortReportsToDisplayInLHN(reports, CONST.PRIORITY_MODE.DEFAULT, mockLocaleCompare, undefined, undefined, undefined); // When the reports are sorted in GSD mode - const gsdResult = SidebarUtils.sortReportsToDisplayInLHN(reports, CONST.PRIORITY_MODE.GSD, mockLocaleCompare); + const gsdResult = SidebarUtils.sortReportsToDisplayInLHN(reports, CONST.PRIORITY_MODE.GSD, mockLocaleCompare, undefined, undefined, undefined); // Then the reports are sorted in the correct order expect(defaultResult).toEqual(['1', '0']); // Most recent first (index 1 has later date)