Skip to content

Commit 886aa24

Browse files
authored
Merge pull request #5317 from meetmangukiya/lhn-draft-flashes
fix: do not update sidebar for active report drafts
2 parents a6c9b6a + 7d53d84 commit 886aa24

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/pages/home/sidebar/SidebarLinks.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,67 @@ const defaultProps = {
9595
};
9696

9797
class SidebarLinks extends React.Component {
98+
shouldComponentUpdate(nextProps) {
99+
// We do not want to re-order reports in the LHN if the only change is the draft comment in the
100+
// current report.
101+
102+
// We don't need to limit draft comment flashing for small screen widths as LHN is not visible.
103+
if (nextProps.isSmallScreenWidth) {
104+
return true;
105+
}
106+
107+
const didActiveReportChange = this.props.currentlyViewedReportID !== nextProps.currentlyViewedReportID;
108+
109+
// Always re-order the list whenever the active report is changed
110+
if (didActiveReportChange) {
111+
return true;
112+
}
113+
114+
const previousDraftComments = this.props.draftComments;
115+
const nextDraftComments = nextProps.draftComments;
116+
117+
const previousDraftReports = Object.keys(previousDraftComments);
118+
const nextDraftReports = Object.keys(nextDraftComments);
119+
120+
const reportsWithNewDraftComments = nextDraftReports.filter((report) => {
121+
const isNewDraftComment = !previousDraftReports.includes(report);
122+
const wasNonEmptyDraftComment = previousDraftComments[report] === '';
123+
const hasDraftCommentChanged = previousDraftComments[report] !== nextDraftComments[report];
124+
125+
return isNewDraftComment || (hasDraftCommentChanged && wasNonEmptyDraftComment);
126+
});
127+
const reportsWithRemovedDraftComments = previousDraftReports.filter((report) => {
128+
const isRemovedDraftComment = !nextDraftReports.includes(report);
129+
const isEmptyDraftComment = nextDraftComments[report] === '';
130+
const hasDraftCommentChanged = previousDraftComments[report] !== nextDraftComments[report];
131+
132+
return isRemovedDraftComment || (hasDraftCommentChanged && isEmptyDraftComment);
133+
});
134+
const reportsWithEditedDraftComments = nextDraftReports.filter((report) => {
135+
const didDraftCommentExistPreviously = previousDraftReports.includes(report);
136+
const hasDraftCommentChanged = previousDraftComments[report] !== nextDraftComments[report];
137+
const isNotANewDraftComment = !reportsWithNewDraftComments.includes(report);
138+
const isNotARemovedDraftComment = !reportsWithRemovedDraftComments.includes(report);
139+
140+
return didDraftCommentExistPreviously && hasDraftCommentChanged && isNotANewDraftComment && isNotARemovedDraftComment;
141+
});
142+
143+
const allReportsWithDraftCommentChanges = [
144+
...reportsWithNewDraftComments,
145+
...reportsWithRemovedDraftComments,
146+
...reportsWithEditedDraftComments,
147+
];
148+
149+
const activeReportID = this.props.currentlyViewedReportID;
150+
const reportKey = `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${activeReportID}`;
151+
152+
// Do not re-order reports if draft comment changes are only in the current report.
153+
if (allReportsWithDraftCommentChanges.length === 1 && allReportsWithDraftCommentChanges.includes(reportKey)) {
154+
return false;
155+
}
156+
return true;
157+
}
158+
98159
showSearchPage() {
99160
Navigation.navigate(ROUTES.SEARCH);
100161
}

0 commit comments

Comments
 (0)