Skip to content

Commit

Permalink
Merge pull request #46569 from callstack-internal/hur/fix-45228
Browse files Browse the repository at this point in the history
fix: archived reports are unable to mark as read
  • Loading branch information
grgia authored Aug 9, 2024
2 parents 55ea2f8 + 2e2de70 commit b1c5b51
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/pages/home/report/ReportActionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,17 @@ 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 isFocused = useIsFocused();
const hasCalledReadNewestAction = useRef(false);

useEffect(() => {
/**
* This allows to mark the report as read, when
* report is opened and new report actions are received.
*/
hasCalledReadNewestAction.current = false;
}, [reportActions]);

useEffect(() => {
const unsubscriber = Visibility.onVisibilityChange(() => {
Expand Down Expand Up @@ -267,13 +276,17 @@ 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.
// To handle this, we use the 'referrer' parameter to check if the current navigation is triggered from a notification.
const isFromNotification = route?.params?.referrer === CONST.REFERRER.NOTIFICATION;
if ((Visibility.isVisible() || isFromNotification) && scrollingVerticalOffset.current < MSG_VISIBLE_THRESHOLD) {
Report.readNewestAction(report.reportID);
hasCalledReadNewestAction.current = true;
if (isFromNotification) {
Navigation.setParams({referrer: undefined});
}
Expand Down Expand Up @@ -523,7 +536,9 @@ function ReportActionsList({
if (!userActiveSince.current || report.reportID !== prevReportID) {
return;
}

if (hasCalledReadNewestAction.current) {
return;
}
if (!isVisible || !isFocused) {
if (!lastMessageTime.current) {
lastMessageTime.current = sortedVisibleReportActions[0]?.created ?? '';
Expand All @@ -535,15 +550,19 @@ 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) => {
/**
* The archived reports should not be marked as unread. So we are checking if the report is archived or not.
* If the report is archived, we will mark the report as read.
*/
const isArchivedReport = ReportUtils.isArchivedRoom(report);
const isUnread = isArchivedReport || (newMessageTimeReference && newMessageTimeReference < reportAction.created);
return (
isUnread && (ReportActionsUtils.isReportPreviewAction(reportAction) ? reportAction.childLastActorAccountID : reportAction.actorAccountID) !== Report.getCurrentUserAccountID()
);
});

if (scrollingVerticalOffset.current >= MSG_VISIBLE_THRESHOLD || !areSomeReportActionsUnread) {
return;
}

Expand All @@ -553,6 +572,7 @@ function ReportActionsList({
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

0 comments on commit b1c5b51

Please sign in to comment.