diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index d550a3bf6993..722b495dc71b 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -553,6 +553,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_', @@ -586,7 +589,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_', @@ -984,6 +988,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; @@ -1204,6 +1209,7 @@ type OnyxValuesMapping = { [ONYXKEYS.TRANSACTION_THREAD_NAVIGATION_REPORT_IDS]: string[]; [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 6d52ab979b31..3ccb3156c04b 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); @@ -284,7 +283,6 @@ function LHNOptionsList({style, contentContainerStyles, data, onSelectRow, optio policy, personalDetails, data.length, - draftComments, optionMode, preferredLocale, transactions, @@ -301,7 +299,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/useSidebarOrderedReports.tsx b/src/hooks/useSidebarOrderedReports.tsx index ed55aacb5918..253c59ed9bb7 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 useOnyx from './useOnyx'; import usePrevious from './usePrevious'; import useResponsiveLayout from './useResponsiveLayout'; @@ -61,7 +63,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({}); @@ -94,8 +97,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 ?? {}) @@ -146,10 +149,10 @@ function SidebarOrderedReportsContextProvider({ derivedCurrentReportID, priorityMode === CONST.PRIORITY_MODE.GSD, betas, - policies, transactionViolations, reportNameValuePairs, reportAttributes, + drafts, ); } else { reportsToDisplay = SidebarUtils.getReportsToDisplayInLHN( @@ -161,19 +164,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, reportNameValuePairs, reportAttributes), + () => SidebarUtils.sortReportsToDisplayInLHN(reportsToDisplayInLHN, priorityMode, 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], 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 1627158a2f6b..d47152254516 100644 --- a/src/libs/API/parameters/index.ts +++ b/src/libs/API/parameters/index.ts @@ -408,3 +408,4 @@ export type {default as ExportMultiLevelTagsSpreadSheetParams} from './ExportMul 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 SaveReportDraftCommentParams} from './SaveReportDraftCommentParams'; diff --git a/src/libs/API/types.ts b/src/libs/API/types.ts index 480e753087fc..06c785a5b058 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', @@ -842,6 +843,7 @@ type WriteCommandParameters = { [WRITE_COMMANDS.COMPLETE_CONCIERGE_CALL]: Parameters.CompleteConciergeCallParams; [WRITE_COMMANDS.FINISH_CORPAY_BANK_ACCOUNT_ONBOARDING]: Parameters.FinishCorpayBankAccountOnboardingParams; [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 3fa05841c51a..e380c03205be 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -5,7 +5,16 @@ import type {ValueOf} from 'type-fest'; import type {PartialPolicyForSidebar, ReportsToDisplayInLHN} from '@hooks/useSidebarOrderedReports'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {PersonalDetails, PersonalDetailsList, ReportActions, ReportAttributesDerivedValue, ReportNameValuePairs, Transaction, TransactionViolation} from '@src/types/onyx'; +import type { + 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 +24,6 @@ import type Report from '@src/types/onyx/Report'; import type ReportAction from '@src/types/onyx/ReportAction'; import {getExpensifyCardFromReportAction} from './CardMessageUtils'; import {extractCollectionItemID} from './CollectionUtils'; -import {hasValidDraftComment} from './DraftCommentUtils'; import localeCompare from './LocaleCompare'; import {translateLocal} from './Localize'; import {getLastActorDisplayName, getLastMessageTextForReport, getPersonalDetailsForAccountIDs, shouldShowLastActorDisplayName} from './OptionsListUtils'; @@ -207,6 +215,7 @@ function shouldDisplayReportInLHN( transactionViolations: OnyxCollection, reportNameValuePairs?: OnyxCollection, reportAttributes?: ReportAttributesDerivedValue['reports'], + draftReportComments?: DraftReportComments, ) { if (!report) { return {shouldDisplay: false}; @@ -238,7 +247,12 @@ function shouldDisplayReportInLHN( const isSystemChat = isSystemChatUtil(report); const isReportArchived = isArchivedReport(reportNameValuePairs); 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}; @@ -269,6 +283,7 @@ function getReportsToDisplayInLHN( transactionViolations: OnyxCollection, reportNameValuePairs?: OnyxCollection, reportAttributes?: ReportAttributesDerivedValue['reports'], + draftReportComments?: DraftReportComments, ) { const isInFocusMode = priorityMode === CONST.PRIORITY_MODE.GSD; const allReportsDictValues = reports ?? {}; @@ -288,6 +303,7 @@ function getReportsToDisplayInLHN( transactionViolations, reportNameValuePairs, reportAttributes, + draftReportComments, ); if (shouldDisplay) { @@ -305,10 +321,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) => { @@ -326,6 +342,7 @@ function updateReportsToDisplayInLHN( transactionViolations, reportNameValuePairs, reportAttributes, + draftReportComments, ); if (shouldDisplay) { @@ -346,6 +363,7 @@ function sortReportsToDisplayInLHN( priorityMode: OnyxEntry, reportNameValuePairs?: OnyxCollection, reportAttributes?: ReportAttributesDerivedValue['reports'], + draftReportComments?: DraftReportComments, ): string[] { Performance.markStart(CONST.TIMING.GET_ORDERED_REPORT_IDS); const isInFocusMode = priorityMode === CONST.PRIORITY_MODE.GSD; @@ -382,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/IOU.ts b/src/libs/actions/IOU.ts index 0829bc535080..34c9a293bd13 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -751,17 +751,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/Category.ts b/src/libs/actions/Policy/Category.ts index ea506ab923c3..232645ee2936 100644 --- a/src/libs/actions/Policy/Category.ts +++ b/src/libs/actions/Policy/Category.ts @@ -52,17 +52,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 21abeeec3c99..466abf0300e4 100644 --- a/src/libs/actions/Policy/DistanceRate.ts +++ b/src/libs/actions/Policy/DistanceRate.ts @@ -36,17 +36,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 fc4c83cb92b5..4a7ce7573db3 100644 --- a/src/libs/actions/Policy/Member.ts +++ b/src/libs/actions/Policy/Member.ts @@ -62,17 +62,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/PerDiem.ts b/src/libs/actions/Policy/PerDiem.ts index c6994c24cbb5..e969782a431a 100644 --- a/src/libs/actions/Policy/PerDiem.ts +++ b/src/libs/actions/Policy/PerDiem.ts @@ -34,17 +34,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 e9cb1e8de9e3..54adade0f799 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -168,17 +168,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 004ad36fda5a..77512cd48af3 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -169,6 +169,7 @@ import INPUT_IDS from '@src/types/form/NewRoomForm'; import type { Account, DismissedProductTraining, + DraftReportComments, IntroSelected, InvitedEmailsToAccountIDs, NewGroupChatDraft, @@ -397,10 +398,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), }); @@ -423,16 +423,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; } @@ -1751,10 +1753,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. */ @@ -1807,7 +1821,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}`)) { @@ -1829,7 +1843,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 081e610b7907..14e0c6e1c792 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 fed7945d9b74..d4d2be8705b5 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 1fb8fbb5f6f4..3c73a96ec9f6 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'; @@ -264,4 +265,5 @@ export type { ValidateUserAndGetAccessiblePolicies, BillingReceiptDetails, 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 837928f3458e..cceb7a728270 100644 --- a/tests/unit/ReportUtilsTest.ts +++ b/tests/unit/ReportUtilsTest.ts @@ -2187,7 +2187,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 57d35d757786..c80f2d173fcb 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, }),