Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - Mark the chat as "read" immediately upon switching to the tab #34537

Merged
7 changes: 6 additions & 1 deletion src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ function expandURLPreview(reportID: string, reportActionID: string) {
}

/** Marks the new report actions as read */
function readNewestAction(reportID: string) {
function readNewestAction(reportID: string, shouldEmitEvent = true) {
const lastReadTime = DateUtils.getDBTime();

const optimisticData: OnyxUpdate[] = [
Expand All @@ -942,6 +942,11 @@ function readNewestAction(reportID: string) {
};

API.write('ReadNewestAction', parameters, {optimisticData});

if (!shouldEmitEvent) {
return;
}

DeviceEventEmitter.emit(`readNewestAction_${reportID}`, lastReadTime);
}

Expand Down
29 changes: 28 additions & 1 deletion src/pages/home/report/ReportActionsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ function ReportActionsList({
const reportActionSize = useRef(sortedVisibleReportActions.length);

const previousLastIndex = useRef(lastActionIndex);
const visibilityCallback = useRef(() => {});

const linkedReportActionID = lodashGet(route, 'params.reportActionID', '');

Expand Down Expand Up @@ -386,7 +387,7 @@ function ReportActionsList({
[currentUnreadMarker, sortedVisibleReportActions, report.reportID, messageManuallyMarkedUnread],
);

useEffect(() => {
const calculateUnreadMarker = () => {
// Iterate through the report actions and set appropriate unread marker.
// This is to avoid a warning of:
// Cannot update a component (ReportActionsList) while rendering a different component (CellRenderer).
Expand All @@ -404,8 +405,34 @@ function ReportActionsList({
if (!markerFound) {
setCurrentUnreadMarker(null);
}
};

useEffect(() => {
calculateUnreadMarker();

/* eslint-disable-next-line react-hooks/exhaustive-deps */
FitseTLT marked this conversation as resolved.
Show resolved Hide resolved
}, [sortedVisibleReportActions, report.lastReadTime, report.reportID, messageManuallyMarkedUnread, shouldDisplayNewMarker, currentUnreadMarker]);

visibilityCallback.current = () => {
if (!Visibility.isVisible() || scrollingVerticalOffset.current >= MSG_VISIBLE_THRESHOLD || !ReportUtils.isUnread(report) || messageManuallyMarkedUnread) {
return;
}

Report.readNewestAction(report.reportID, false);
userActiveSince.current = DateUtils.getDBTime();
setCurrentUnreadMarker(null);
cacheUnreadMarkers.delete(report.reportID);
calculateUnreadMarker();
};

useEffect(() => {
const unsubscribeVisibilityListener = Visibility.onVisibilityChange(() => visibilityCallback.current());

return unsubscribeVisibilityListener;

/* eslint-disable-next-line react-hooks/exhaustive-deps */
}, [report.reportID]);

const renderItem = useCallback(
({item: reportAction, index}) => (
<ReportActionsListItemRenderer
Expand Down
Loading