Skip to content

Commit

Permalink
Merge pull request #24213 from bernhardoj/fix/24136-revert-last-message
Browse files Browse the repository at this point in the history
Fix red dot is not cleared after close add message error and revert last message when the add message fails
  • Loading branch information
madmax330 authored Aug 9, 2023
2 parents ab99bf4 + 7052c5b commit 5b3acf7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
21 changes: 18 additions & 3 deletions src/components/LHNOptionsList/OptionRowLHNData.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,18 @@ const defaultProps = {
* The OptionRowLHN component is memoized, so it will only
* re-render if the data really changed.
*/
function OptionRowLHNData({shouldDisableFocusOptions, currentReportID, fullReport, personalDetails, preferredLocale, comment, policies, parentReportActions, ...propsToForward}) {
function OptionRowLHNData({
shouldDisableFocusOptions,
currentReportID,
fullReport,
reportActions,
personalDetails,
preferredLocale,
comment,
policies,
parentReportActions,
...propsToForward
}) {
const reportID = propsToForward.reportID;
// We only want to pass a boolean to the memoized component,
// instead of a changing number (so we prevent unnecessary re-renders).
Expand All @@ -79,15 +90,15 @@ function OptionRowLHNData({shouldDisableFocusOptions, currentReportID, fullRepor
const optionItemRef = useRef();
const optionItem = useMemo(() => {
// Note: ideally we'd have this as a dependent selector in onyx!
const item = SidebarUtils.getOptionData(fullReport, personalDetails, preferredLocale, policy);
const item = SidebarUtils.getOptionData(fullReport, reportActions, personalDetails, preferredLocale, policy);
if (deepEqual(item, optionItemRef.current)) {
return optionItemRef.current;
}
optionItemRef.current = item;
return item;
// Listen parentReportAction to update title of thread report when parentReportAction changed
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fullReport, personalDetails, preferredLocale, policy, parentReportAction]);
}, [fullReport, reportActions, personalDetails, preferredLocale, policy, parentReportAction]);

useEffect(() => {
if (!optionItem || optionItem.hasDraftComment || !comment || comment.length <= 0 || isFocused) {
Expand Down Expand Up @@ -154,6 +165,10 @@ export default React.memo(
fullReport: {
key: (props) => ONYXKEYS.COLLECTION.REPORT + props.reportID,
},
reportActions: {
key: ({reportID}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
canEvict: false,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
selector: personalDetailsSelector,
Expand Down
4 changes: 1 addition & 3 deletions src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,8 @@ function getSearchText(report, reportName, personalDetailList, isChatRoomOrPolic
function getAllReportErrors(report, reportActions) {
const reportErrors = report.errors || {};
const reportErrorFields = report.errorFields || {};
const reportID = report.reportID;
const reportsActions = reportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`] || {};
const reportActionErrors = _.reduce(
reportsActions,
reportActions,
(prevReportActionErrors, action) => (!action || _.isEmpty(action.errors) ? prevReportActionErrors : _.extend(prevReportActionErrors, action.errors)),
{},
);
Expand Down
6 changes: 2 additions & 4 deletions src/libs/SidebarUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import * as PersonalDetailsUtils from './PersonalDetailsUtils';

const visibleReportActionItems = {};
const lastReportActions = {};
const reportActions = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
callback: (actions, key) => {
Expand All @@ -35,8 +34,6 @@ Onyx.connect({
(reportAction, actionKey) => ReportActionsUtils.shouldReportActionBeVisible(reportAction, actionKey) && reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.CREATED,
);
visibleReportActionItems[reportID] = _.last(reportActionsForDisplay);

reportActions[key] = actions;
},
});

Expand Down Expand Up @@ -175,12 +172,13 @@ function getOrderedReportIDs(currentReportId, allReportsDict, betas, policies, p
* Gets all the data necessary for rendering an OptionRowLHN component
*
* @param {Object} report
* @param {Object} reportActions
* @param {Object} personalDetails
* @param {String} preferredLocale
* @param {Object} [policy]
* @returns {Object}
*/
function getOptionData(report, personalDetails, preferredLocale, policy) {
function getOptionData(report, reportActions, personalDetails, preferredLocale, policy) {
// When a user signs out, Onyx is cleared. Due to the lazy rendering with a virtual list, it's possible for
// this method to be called after the Onyx data has been cleared out. In that case, it's fine to do
// a null check here and return early.
Expand Down
27 changes: 16 additions & 11 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import * as PersonalDetailsUtils from '../PersonalDetailsUtils';
import SidebarUtils from '../SidebarUtils';
import * as OptionsListUtils from '../OptionsListUtils';
import * as Environment from '../Environment/Environment';
import * as Localize from '../Localize';

let currentUserAccountID;
Onyx.connect({
Expand Down Expand Up @@ -243,14 +242,6 @@ function addActions(reportID, text = '', file) {

const currentTime = DateUtils.getDBTime();

const lastVisibleMessage = ReportActionsUtils.getLastVisibleMessage(reportID);
let prevVisibleMessageText;
if (lastVisibleMessage.lastMessageTranslationKey) {
prevVisibleMessageText = Localize.translateLocal(lastVisibleMessage.lastMessageTranslationKey);
} else {
prevVisibleMessageText = lastVisibleMessage.lastMessageText;
}

const lastCommentText = ReportUtils.formatReportLastMessageText(lastAction.message[0].text);

const optimisticReport = {
Expand Down Expand Up @@ -301,9 +292,23 @@ function addActions(reportID, text = '', file) {
},
];

const failureReport = {
lastMessageText: prevVisibleMessageText,
let failureReport = {
lastMessageTranslationKey: '',
lastMessageText: '',
lastVisibleActionCreated: '',
};
const {lastMessageText = '', lastMessageTranslationKey = ''} = ReportActionsUtils.getLastVisibleMessage(reportID);
if (lastMessageText || lastMessageTranslationKey) {
const lastVisibleAction = ReportActionsUtils.getLastVisibleAction(reportID);
const lastVisibleActionCreated = lastVisibleAction.created;
const lastActorAccountID = lastVisibleAction.actorAccountID;
failureReport = {
lastMessageTranslationKey,
lastMessageText,
lastVisibleActionCreated,
lastActorAccountID,
};
}
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down

0 comments on commit 5b3acf7

Please sign in to comment.