Skip to content

Commit

Permalink
Merge pull request #18637 from gedu/monil-fixNewMarkerLine
Browse files Browse the repository at this point in the history
Refactor code to improve unread marker logic
  • Loading branch information
MonilBhavsar authored Aug 17, 2023
2 parents 8d11238 + 5d3edd4 commit 28be10b
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 257 deletions.
39 changes: 33 additions & 6 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ Onyx.connect({
},
});

const currentReportData = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
callback: (data, key) => {
if (!key || !data) {
return;
}
const reportID = CollectionUtils.extractCollectionItemID(key);
currentReportData[reportID] = data;
},
});

let isNetworkOffline = false;
Onyx.connect({
key: ONYXKEYS.NETWORK,
Expand Down Expand Up @@ -373,6 +385,10 @@ function addComment(reportID, text) {
addActions(reportID, text);
}

function reportActionsExist(reportID) {
return allReportActions[reportID] !== undefined;
}

/**
* Gets the latest page of report actions and updates the last read message
* If a chat with the passed reportID is not found, we will create a chat based on the passed participantList
Expand All @@ -388,11 +404,13 @@ function openReport(reportID, participantLoginList = [], newReportObject = {}, p
const optimisticReportData = {
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
isLoadingReportActions: true,
isLoadingMoreReportActions: false,
lastReadTime: DateUtils.getDBTime(),
},
value: reportActionsExist(reportID)
? {}
: {
isLoadingReportActions: true,
isLoadingMoreReportActions: false,
reportName: lodashGet(allReports, [reportID, 'reportName'], CONST.REPORT.DEFAULT_REPORT_NAME),
},
};
const reportSuccessData = {
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -520,6 +538,8 @@ function openReport(reportID, participantLoginList = [], newReportObject = {}, p
}
}

params.clientLastReadTime = lodashGet(currentReportData, [reportID, 'lastReadTime'], '');

if (isFromDeepLink) {
// eslint-disable-next-line rulesdir/no-api-side-effects-method
API.makeRequestWithSideEffects('OpenReport', params, onyxData).finally(() => {
Expand Down Expand Up @@ -720,18 +740,20 @@ function expandURLPreview(reportID, reportActionID) {
* @param {String} reportID
*/
function readNewestAction(reportID) {
const lastReadTime = DateUtils.getDBTime();
API.write(
'ReadNewestAction',
{
reportID,
lastReadTime,
},
{
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
value: {
lastReadTime: DateUtils.getDBTime(),
lastReadTime,
},
},
],
Expand Down Expand Up @@ -1749,6 +1771,10 @@ function openReportFromDeepLink(url, isAuthenticated) {
});
}

function getCurrentUserAccountID() {
return currentUserAccountID;
}

/**
* Leave a report by setting the state to submitted and closed
*
Expand Down Expand Up @@ -1948,6 +1974,7 @@ export {
hasAccountIDEmojiReacted,
shouldShowReportActionNotification,
leaveRoom,
getCurrentUserAccountID,
setLastOpenedPublicRoom,
flagComment,
openLastOpenedPublicRoom,
Expand Down
1 change: 0 additions & 1 deletion src/pages/home/ReportScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ class ReportScreen extends React.Component {
if (this.props.report.reportID && this.props.report.reportID === getReportID(this.props.route)) {
return;
}

Report.openReport(reportIDFromPath);
}

Expand Down
Loading

0 comments on commit 28be10b

Please sign in to comment.