@@ -1948,23 +1948,14 @@ function deleteReportComment(reportID: string | undefined, reportAction: ReportA
19481948
19491949 // If we are deleting the last visible message, let's find the previous visible one (or set an empty one if there are none) and update the lastMessageText in the LHN.
19501950 // Similarly, if we are deleting the last read comment we will want to update the lastVisibleActionCreated to use the previous visible message.
1951- let optimisticReport : Partial < Report > = {
1952- lastMessageText : '' ,
1953- lastVisibleActionCreated : '' ,
1954- } ;
1955- const { lastMessageText = '' } = getLastVisibleMessage ( originalReportID , isOriginalReportArchived , optimisticReportActions as ReportActions ) ;
19561951 const report = allReports ?. [ `${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` ] ;
19571952 const canUserPerformWriteAction = canUserPerformWriteActionReportUtils ( report , isReportArchived ) ;
1958- if ( lastMessageText ) {
1959- const lastVisibleAction = ReportActionsUtils . getLastVisibleAction ( originalReportID , canUserPerformWriteAction , optimisticReportActions as ReportActions ) ;
1960- const lastVisibleActionCreated = lastVisibleAction ?. created ;
1961- const lastActorAccountID = lastVisibleAction ?. actorAccountID ;
1962- optimisticReport = {
1963- lastMessageText,
1964- lastVisibleActionCreated,
1965- lastActorAccountID,
1966- } ;
1967- }
1953+ const optimisticLastReportData = optimisticReportLastData ( originalReportID , optimisticReportActions as ReportActions , canUserPerformWriteAction , isOriginalReportArchived ) ;
1954+
1955+ const optimisticReport : Partial < Report > = {
1956+ ...optimisticLastReportData ,
1957+ } ;
1958+
19681959 const didCommentMentionCurrentUser = ReportActionsUtils . didMessageMentionCurrentUser ( reportAction ) ;
19691960 if ( didCommentMentionCurrentUser && reportAction . created === report ?. lastMentionedTime ) {
19701961 const reportActionsForReport = allReportActions ?. [ reportID ] ;
@@ -4115,6 +4106,21 @@ function removeFromGroupChat(reportID: string, accountIDList: number[]) {
41154106 removeFromRoom ( reportID , accountIDList ) ;
41164107}
41174108
4109+ function optimisticReportLastData (
4110+ reportID : string ,
4111+ optimisticReportActions : Record < string , NullishDeep < ReportAction > | null > = { } ,
4112+ canUserPerformWriteAction ?: boolean ,
4113+ isReportArchived ?: boolean ,
4114+ ) {
4115+ const lastMessageText = getLastVisibleMessage ( reportID , isReportArchived , optimisticReportActions ) . lastMessageText ?? '' ;
4116+ const lastVisibleAction = ReportActionsUtils . getLastVisibleAction ( reportID , canUserPerformWriteAction , optimisticReportActions ) ;
4117+ return {
4118+ lastMessageText,
4119+ lastVisibleActionCreated : lastVisibleAction ?. created ?? '' ,
4120+ lastActorAccountID : lastMessageText ? lastVisibleAction ?. actorAccountID : CONST . DEFAULT_NUMBER_ID ,
4121+ } ;
4122+ }
4123+
41184124/** Flag a comment as offensive */
41194125function flagComment ( reportAction : OnyxEntry < ReportAction > , severity : string , originalReport : OnyxEntry < Report > | undefined , isOriginalReportArchived = false ) {
41204126 const originalReportID = originalReport ?. reportID ;
@@ -4168,28 +4174,21 @@ function flagComment(reportAction: OnyxEntry<ReportAction>, severity: string, or
41684174 } ,
41694175 ] ;
41704176
4171- let optimisticReport : Partial < Report > = {
4172- lastMessageText : '' ,
4173- lastVisibleActionCreated : '' ,
4174- } ;
4175-
4176- const { lastMessageText = '' } = getLastVisibleMessage ( originalReportID , isOriginalReportArchived , {
4177- [ reportActionID ] : { ...reportAction , message : [ updatedMessage ] , pendingAction : CONST . RED_BRICK_ROAD_PENDING_ACTION . UPDATE } ,
4178- } as ReportActions ) ;
4177+ const report = allReports ?. [ `${ ONYXKEYS . COLLECTION . REPORT } ${ originalReportID } ` ] ;
4178+ const canUserPerformWriteAction = canUserPerformWriteActionReportUtils ( report ) ;
41794179
4180- const canUserPerformWriteAction = canUserPerformWriteActionReportUtils ( originalReport , isOriginalReportArchived ) ;
4181- if ( lastMessageText ) {
4182- const lastVisibleAction = ReportActionsUtils . getLastVisibleAction ( originalReportID , canUserPerformWriteAction , {
4180+ const optimisticLastReportData = optimisticReportLastData (
4181+ originalReportID ?? String ( CONST . DEFAULT_NUMBER_ID ) ,
4182+ {
41834183 [ reportActionID ] : { ...reportAction , message : [ updatedMessage ] , pendingAction : CONST . RED_BRICK_ROAD_PENDING_ACTION . UPDATE } ,
4184- } as ReportActions ) ;
4185- const lastVisibleActionCreated = lastVisibleAction ?. created ;
4186- const lastActorAccountID = lastVisibleAction ?. actorAccountID ;
4187- optimisticReport = {
4188- lastMessageText,
4189- lastVisibleActionCreated,
4190- lastActorAccountID,
4191- } ;
4192- }
4184+ } as ReportActions ,
4185+ canUserPerformWriteAction ,
4186+ isOriginalReportArchived ,
4187+ ) ;
4188+
4189+ const optimisticReport : Partial < Report > = {
4190+ ...optimisticLastReportData ,
4191+ } ;
41934192
41944193 if ( shouldHideMessage ) {
41954194 optimisticData . push ( {
@@ -6271,5 +6270,6 @@ export {
62716270 removeFailedReport ,
62726271 createTransactionThreadReport ,
62736272 openUnreportedExpense ,
6273+ optimisticReportLastData ,
62746274 setOptimisticTransactionThread ,
62756275} ;
0 commit comments