From b0c9b955e8df53f51281b405aa235fb804c2a92b Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Sun, 22 Oct 2023 00:40:27 +0500 Subject: [PATCH 01/10] fix: unread logic --- src/libs/ReportUtils.js | 10 ++++++++++ src/libs/UnreadIndicatorUpdater/index.js | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 75ee6257caab..7b55903bedf4 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -2992,6 +2992,15 @@ function buildTransactionThread(reportAction, moneyRequestReportID) { ); } +/** + * @param {Object} report + * @returns {Boolean} + */ +function isEmptyReport(report) { + const lastVisibleMessage = ReportActionsUtils.getLastVisibleMessage(report.reportID); + return !report.lastMessageText && !report.lastMessageTranslationKey && !lastVisibleMessage.lastMessageText && !lastVisibleMessage.lastMessageTranslationKey; +} + /** * @param {Object} report * @returns {Boolean} @@ -4000,6 +4009,7 @@ export { navigateToDetailsPage, generateReportID, hasReportNameError, + isEmptyReport, isUnread, isUnreadWithMention, buildOptimisticWorkspaceChats, diff --git a/src/libs/UnreadIndicatorUpdater/index.js b/src/libs/UnreadIndicatorUpdater/index.js index 09fa82612314..983fc57c511c 100644 --- a/src/libs/UnreadIndicatorUpdater/index.js +++ b/src/libs/UnreadIndicatorUpdater/index.js @@ -8,7 +8,7 @@ Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, callback: (reportsFromOnyx) => { - const unreadReports = _.filter(reportsFromOnyx, ReportUtils.isUnread); + const unreadReports = _.filter(reportsFromOnyx, (report) => ReportUtils.unread(report) && ReportUtils.isEmptyReport(report)); updateUnread(_.size(unreadReports)); }, }); From 90c915b71daf148b34c3b8ea2282c7fbcff3de3c Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Sun, 22 Oct 2023 02:08:23 +0500 Subject: [PATCH 02/10] fix: empty condition --- src/libs/UnreadIndicatorUpdater/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/UnreadIndicatorUpdater/index.js b/src/libs/UnreadIndicatorUpdater/index.js index 983fc57c511c..be11b3cc3a62 100644 --- a/src/libs/UnreadIndicatorUpdater/index.js +++ b/src/libs/UnreadIndicatorUpdater/index.js @@ -8,7 +8,7 @@ Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, callback: (reportsFromOnyx) => { - const unreadReports = _.filter(reportsFromOnyx, (report) => ReportUtils.unread(report) && ReportUtils.isEmptyReport(report)); + const unreadReports = _.filter(reportsFromOnyx, (report) => ReportUtils.isUnread(report) && !ReportUtils.isEmptyReport(report)); updateUnread(_.size(unreadReports)); }, }); From 15294bed03d9df1331630eaeb4cc02a6bbef2aaa Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Mon, 23 Oct 2023 01:47:50 +0500 Subject: [PATCH 03/10] feat: mimic lhn --- src/libs/UnreadIndicatorUpdater/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/UnreadIndicatorUpdater/index.js b/src/libs/UnreadIndicatorUpdater/index.js index be11b3cc3a62..a2c523d082f3 100644 --- a/src/libs/UnreadIndicatorUpdater/index.js +++ b/src/libs/UnreadIndicatorUpdater/index.js @@ -1,5 +1,6 @@ import _ from 'underscore'; import Onyx from 'react-native-onyx'; +import Navigation from '../Navigation/Navigation'; import ONYXKEYS from '../../ONYXKEYS'; import updateUnread from './updateUnread/index'; import * as ReportUtils from '../ReportUtils'; @@ -8,7 +9,7 @@ Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, callback: (reportsFromOnyx) => { - const unreadReports = _.filter(reportsFromOnyx, (report) => ReportUtils.isUnread(report) && !ReportUtils.isEmptyReport(report)); + const unreadReports = _.filter(reportsFromOnyx, (report) => ReportUtils.isUnread(report) && ReportUtils.shouldReportBeInOptionList(report, Navigation.getTopmostReportId())); updateUnread(_.size(unreadReports)); }, }); From 7fba7598f009fd8cbae6961a04ccb822e75a118a Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Tue, 24 Oct 2023 19:26:28 +0500 Subject: [PATCH 04/10] fix: remove unused variable --- src/libs/ReportUtils.js | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/libs/ReportUtils.js b/src/libs/ReportUtils.js index 7b55903bedf4..93774afc0aef 100644 --- a/src/libs/ReportUtils.js +++ b/src/libs/ReportUtils.js @@ -2992,15 +2992,6 @@ function buildTransactionThread(reportAction, moneyRequestReportID) { ); } -/** - * @param {Object} report - * @returns {Boolean} - */ -function isEmptyReport(report) { - const lastVisibleMessage = ReportActionsUtils.getLastVisibleMessage(report.reportID); - return !report.lastMessageText && !report.lastMessageTranslationKey && !lastVisibleMessage.lastMessageText && !lastVisibleMessage.lastMessageTranslationKey; -} - /** * @param {Object} report * @returns {Boolean} @@ -3412,8 +3403,16 @@ function parseReportRouteParams(route) { } const pathSegments = parsingRoute.split('/'); + + const reportIDSegment = pathSegments[1]; + + // Check for "undefined" or any other unwanted string values + if (!reportIDSegment || reportIDSegment === 'undefined') { + return {reportID: '', isSubReportPageRoute: false}; + } + return { - reportID: pathSegments[1], + reportID: reportIDSegment, isSubReportPageRoute: pathSegments.length > 2, }; } @@ -3730,6 +3729,21 @@ function getPolicyExpenseChatReportIDByOwner(policyOwner) { return expenseChat.reportID; } +/** + * Check if the report can create the request with type is iouType + * @param {Object} report + * @param {Array} betas + * @param {String} iouType + * @returns {Boolean} + */ +function canCreateRequest(report, betas, iouType) { + const participantAccountIDs = lodashGet(report, 'participantAccountIDs', []); + if (shouldDisableWriteActions(report)) { + return false; + } + return getMoneyRequestOptions(report, participantAccountIDs, betas).includes(iouType); +} + /** * @param {String} policyID * @param {Array} accountIDs @@ -4009,7 +4023,6 @@ export { navigateToDetailsPage, generateReportID, hasReportNameError, - isEmptyReport, isUnread, isUnreadWithMention, buildOptimisticWorkspaceChats, @@ -4060,6 +4073,7 @@ export { getCommentLength, getParsedComment, getMoneyRequestOptions, + canCreateRequest, hasIOUWaitingOnCurrentUserBankAccount, canRequestMoney, getWhisperDisplayNames, From 04119fd866b8e051a8557bcb9190fd8a4eddd9b6 Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Fri, 17 Nov 2023 00:59:31 +0500 Subject: [PATCH 05/10] fix lint errors --- src/libs/UnreadIndicatorUpdater/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/UnreadIndicatorUpdater/index.js b/src/libs/UnreadIndicatorUpdater/index.js index 29e7c9de4bbd..b96a949da7db 100644 --- a/src/libs/UnreadIndicatorUpdater/index.js +++ b/src/libs/UnreadIndicatorUpdater/index.js @@ -1,7 +1,7 @@ import Onyx from 'react-native-onyx'; -import Navigation from '@navigation/Navigation'; import _ from 'underscore'; import * as ReportUtils from '@libs/ReportUtils'; +import Navigation from '@navigation/Navigation'; import ONYXKEYS from '@src/ONYXKEYS'; import updateUnread from './updateUnread/index'; From 1cdb1748c2c6f4a580aeaa1c4f6407b03b08d901 Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Thu, 30 Nov 2023 04:07:41 +0500 Subject: [PATCH 06/10] lint fix --- src/libs/UnreadIndicatorUpdater/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/UnreadIndicatorUpdater/index.js b/src/libs/UnreadIndicatorUpdater/index.js index c79d280611d5..b96a949da7db 100644 --- a/src/libs/UnreadIndicatorUpdater/index.js +++ b/src/libs/UnreadIndicatorUpdater/index.js @@ -5,7 +5,6 @@ import Navigation from '@navigation/Navigation'; import ONYXKEYS from '@src/ONYXKEYS'; import updateUnread from './updateUnread/index'; - Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, From 50580cbd49e5d0410c7404a816c08864b397c3a1 Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Thu, 30 Nov 2023 05:12:32 +0500 Subject: [PATCH 07/10] fix: sync unread count with lhn --- src/components/withCurrentReportID.tsx | 7 ++++++- src/libs/UnreadIndicatorUpdater/index.js | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/components/withCurrentReportID.tsx b/src/components/withCurrentReportID.tsx index 3ce9eeae37b5..947f03ccbf76 100644 --- a/src/components/withCurrentReportID.tsx +++ b/src/components/withCurrentReportID.tsx @@ -1,8 +1,9 @@ import {NavigationState} from '@react-navigation/native'; import PropTypes from 'prop-types'; -import React, {ComponentType, createContext, ForwardedRef, forwardRef, RefAttributes, useCallback, useMemo, useState} from 'react'; +import React, {ComponentType, createContext, ForwardedRef, forwardRef, RefAttributes, useCallback, useEffect, useMemo, useState} from 'react'; import getComponentDisplayName from '@libs/getComponentDisplayName'; import Navigation from '@libs/Navigation/Navigation'; +import triggerUnreadUpdate from '@libs/UnreadIndicatorUpdater'; type CurrentReportIDContextValue = { updateCurrentReportID: (state: NavigationState) => void; @@ -55,6 +56,10 @@ function CurrentReportIDContextProvider(props: CurrentReportIDContextProviderPro [updateCurrentReportID, currentReportID], ); + useEffect(() => { + triggerUnreadUpdate(); + }, [currentReportID]); + return {props.children}; } diff --git a/src/libs/UnreadIndicatorUpdater/index.js b/src/libs/UnreadIndicatorUpdater/index.js index b96a949da7db..ca081fe921bf 100644 --- a/src/libs/UnreadIndicatorUpdater/index.js +++ b/src/libs/UnreadIndicatorUpdater/index.js @@ -1,15 +1,25 @@ import Onyx from 'react-native-onyx'; import _ from 'underscore'; import * as ReportUtils from '@libs/ReportUtils'; -import Navigation from '@navigation/Navigation'; +import Navigation, {navigationRef} from '@navigation/Navigation'; import ONYXKEYS from '@src/ONYXKEYS'; import updateUnread from './updateUnread/index'; +let allReports = []; + +const triggerUnreadUpdate = (reports = allReports) => { + const currentReportID = navigationRef.isReady() ? Navigation.getTopmostReportId() : ''; + const unreadReports = _.filter(reports, (report) => ReportUtils.isUnread(report) && ReportUtils.shouldReportBeInOptionList(report, currentReportID)); + updateUnread(_.size(unreadReports)); +}; + Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, callback: (reportsFromOnyx) => { - const unreadReports = _.filter(reportsFromOnyx, (report) => ReportUtils.isUnread(report) && ReportUtils.shouldReportBeInOptionList(report, Navigation.getTopmostReportId())); - updateUnread(_.size(unreadReports)); + allReports = reportsFromOnyx; + triggerUnreadUpdate(); }, }); + +export default triggerUnreadUpdate; From 5bff1bd2ddfcefd2d8494d6ab98291471e373f92 Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Thu, 30 Nov 2023 05:22:57 +0500 Subject: [PATCH 08/10] fix: unit tests --- src/components/withCurrentReportID.tsx | 7 +------ src/libs/UnreadIndicatorUpdater/index.js | 4 +++- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/components/withCurrentReportID.tsx b/src/components/withCurrentReportID.tsx index 947f03ccbf76..3ce9eeae37b5 100644 --- a/src/components/withCurrentReportID.tsx +++ b/src/components/withCurrentReportID.tsx @@ -1,9 +1,8 @@ import {NavigationState} from '@react-navigation/native'; import PropTypes from 'prop-types'; -import React, {ComponentType, createContext, ForwardedRef, forwardRef, RefAttributes, useCallback, useEffect, useMemo, useState} from 'react'; +import React, {ComponentType, createContext, ForwardedRef, forwardRef, RefAttributes, useCallback, useMemo, useState} from 'react'; import getComponentDisplayName from '@libs/getComponentDisplayName'; import Navigation from '@libs/Navigation/Navigation'; -import triggerUnreadUpdate from '@libs/UnreadIndicatorUpdater'; type CurrentReportIDContextValue = { updateCurrentReportID: (state: NavigationState) => void; @@ -56,10 +55,6 @@ function CurrentReportIDContextProvider(props: CurrentReportIDContextProviderPro [updateCurrentReportID, currentReportID], ); - useEffect(() => { - triggerUnreadUpdate(); - }, [currentReportID]); - return {props.children}; } diff --git a/src/libs/UnreadIndicatorUpdater/index.js b/src/libs/UnreadIndicatorUpdater/index.js index ca081fe921bf..d7f4f7d1c08f 100644 --- a/src/libs/UnreadIndicatorUpdater/index.js +++ b/src/libs/UnreadIndicatorUpdater/index.js @@ -22,4 +22,6 @@ Onyx.connect({ }, }); -export default triggerUnreadUpdate; +navigationRef.addListener('state', () => { + triggerUnreadUpdate(); +}); From 59cfe6fd08c434042299da857cd857b6a330a5f1 Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Thu, 30 Nov 2023 05:26:02 +0500 Subject: [PATCH 09/10] fix: added comment --- src/libs/UnreadIndicatorUpdater/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libs/UnreadIndicatorUpdater/index.js b/src/libs/UnreadIndicatorUpdater/index.js index d7f4f7d1c08f..a18d37613bf3 100644 --- a/src/libs/UnreadIndicatorUpdater/index.js +++ b/src/libs/UnreadIndicatorUpdater/index.js @@ -9,6 +9,8 @@ let allReports = []; const triggerUnreadUpdate = (reports = allReports) => { const currentReportID = navigationRef.isReady() ? Navigation.getTopmostReportId() : ''; + + // We want to keep notification count consistent with what can be accessed from the LHN list const unreadReports = _.filter(reports, (report) => ReportUtils.isUnread(report) && ReportUtils.shouldReportBeInOptionList(report, currentReportID)); updateUnread(_.size(unreadReports)); }; From bbe1a983c42850379f806f2c33694c3270ec3693 Mon Sep 17 00:00:00 2001 From: Sibtain Ali Date: Thu, 30 Nov 2023 16:28:17 +0500 Subject: [PATCH 10/10] fix: remove report param since its stale --- src/libs/UnreadIndicatorUpdater/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/UnreadIndicatorUpdater/index.js b/src/libs/UnreadIndicatorUpdater/index.js index a18d37613bf3..ee686444abfd 100644 --- a/src/libs/UnreadIndicatorUpdater/index.js +++ b/src/libs/UnreadIndicatorUpdater/index.js @@ -7,11 +7,11 @@ import updateUnread from './updateUnread/index'; let allReports = []; -const triggerUnreadUpdate = (reports = allReports) => { +const triggerUnreadUpdate = () => { const currentReportID = navigationRef.isReady() ? Navigation.getTopmostReportId() : ''; // We want to keep notification count consistent with what can be accessed from the LHN list - const unreadReports = _.filter(reports, (report) => ReportUtils.isUnread(report) && ReportUtils.shouldReportBeInOptionList(report, currentReportID)); + const unreadReports = _.filter(allReports, (report) => ReportUtils.isUnread(report) && ReportUtils.shouldReportBeInOptionList(report, currentReportID)); updateUnread(_.size(unreadReports)); };