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: archived reports are unable to mark as read #45240

Merged
merged 7 commits into from
Jul 29, 2024
37 changes: 26 additions & 11 deletions src/pages/home/report/ReportActionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ function ReportActionsList({
const userActiveSince = useRef<string | null>(null);
const lastMessageTime = useRef<string | null>(null);

const [isVisible, setIsVisible] = useState(false);
const [isVisible, setIsVisible] = useState(Visibility.isVisible());
const hasCalledReadNewestAction = useRef(false);
const isFocused = useIsFocused();

useEffect(() => {
Expand Down Expand Up @@ -261,6 +262,9 @@ function ReportActionsList({
if (!userActiveSince.current || report.reportID !== prevReportID) {
return;
}
if (hasCalledReadNewestAction.current) {
return;
}
if (ReportUtils.isUnread(report)) {
// On desktop, when the notification center is displayed, Visibility.isVisible() will return false.
// Currently, there's no programmatic way to dismiss the notification center panel.
Expand All @@ -269,6 +273,7 @@ function ReportActionsList({
if ((Visibility.isVisible() || isFromNotification) && scrollingVerticalOffset.current < MSG_VISIBLE_THRESHOLD) {
Report.readNewestAction(report.reportID);
Navigation.setParams({referrer: undefined});
hasCalledReadNewestAction.current = true;
} else {
readActionSkipped.current = true;
}
Expand Down Expand Up @@ -516,6 +521,10 @@ function ReportActionsList({
return;
}

if (hasCalledReadNewestAction.current) {
return;
}

if (!isVisible || !isFocused) {
if (!lastMessageTime.current) {
lastMessageTime.current = sortedVisibleReportActions[0]?.created ?? '';
Expand All @@ -527,24 +536,30 @@ function ReportActionsList({
// show marker based on report.lastReadTime
const newMessageTimeReference = lastMessageTime.current && report.lastReadTime && lastMessageTime.current > report.lastReadTime ? userActiveSince.current : report.lastReadTime;
lastMessageTime.current = null;
if (
scrollingVerticalOffset.current >= MSG_VISIBLE_THRESHOLD ||
!sortedVisibleReportActions.some(
(reportAction) =>
newMessageTimeReference &&
newMessageTimeReference < reportAction.created &&
(ReportActionsUtils.isReportPreviewAction(reportAction) ? reportAction.childLastActorAccountID : reportAction.actorAccountID) !== Report.getCurrentUserAccountID(),
)
) {
const areSomeReportActionsUnread = sortedVisibleReportActions.some((reportAction) => {
/**
* In case of archived reports, we have `newMessageTimeReference` equal to `reportAction.created`.
* So the condition should be `<=` to mark the report as read. We will only use this condition when
* the report is archived to not introduce any regression for other reports.
*/
const isTimeLesserOrEqual = ReportUtils.isArchivedRoom(report)
? newMessageTimeReference && newMessageTimeReference <= reportAction.created
: newMessageTimeReference && newMessageTimeReference < reportAction.created;
return (
isTimeLesserOrEqual &&
(ReportActionsUtils.isReportPreviewAction(reportAction) ? reportAction.childLastActorAccountID : reportAction.actorAccountID) !== Report.getCurrentUserAccountID()
);
});
if (scrollingVerticalOffset.current >= MSG_VISIBLE_THRESHOLD || !areSomeReportActionsUnread) {
return;
}

Report.readNewestAction(report.reportID);
userActiveSince.current = DateUtils.getDBTime();
lastReadTimeRef.current = newMessageTimeReference;
setCurrentUnreadMarker(null);
cacheUnreadMarkers.delete(report.reportID);
calculateUnreadMarker();
hasCalledReadNewestAction.current = true;

// This effect logic to `mark as read` will only run when the report focused has new messages and the App visibility
// is changed to visible(meaning user switched to app/web, while user was previously using different tab or application).
Expand Down
Loading