diff --git a/src/libs/UnreadIndicatorUpdater/index.js b/src/libs/UnreadIndicatorUpdater/index.ts similarity index 60% rename from src/libs/UnreadIndicatorUpdater/index.js rename to src/libs/UnreadIndicatorUpdater/index.ts index ee686444abfd..da45057bc46d 100644 --- a/src/libs/UnreadIndicatorUpdater/index.js +++ b/src/libs/UnreadIndicatorUpdater/index.ts @@ -1,18 +1,20 @@ -import Onyx from 'react-native-onyx'; -import _ from 'underscore'; +import Onyx, {OnyxCollection} from 'react-native-onyx'; import * as ReportUtils from '@libs/ReportUtils'; import Navigation, {navigationRef} from '@navigation/Navigation'; import ONYXKEYS from '@src/ONYXKEYS'; -import updateUnread from './updateUnread/index'; +import {Report} from '@src/types/onyx'; +import updateUnread from './updateUnread'; -let allReports = []; +let allReports: OnyxCollection = {}; 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(allReports, (report) => ReportUtils.isUnread(report) && ReportUtils.shouldReportBeInOptionList(report, currentReportID)); - updateUnread(_.size(unreadReports)); + const unreadReports = Object.values(allReports ?? {}).filter( + (report) => ReportUtils.isUnread(report) && ReportUtils.shouldReportBeInOptionList(report, currentReportID ?? '', false, [], {}), + ); + updateUnread(unreadReports.length); }; Onyx.connect({ diff --git a/src/libs/UnreadIndicatorUpdater/updateUnread/index.android.js b/src/libs/UnreadIndicatorUpdater/updateUnread/index.android.js deleted file mode 100644 index 731b8058a9a7..000000000000 --- a/src/libs/UnreadIndicatorUpdater/updateUnread/index.android.js +++ /dev/null @@ -1,2 +0,0 @@ -// Android does not yet implement this -export default () => {}; diff --git a/src/libs/UnreadIndicatorUpdater/updateUnread/index.android.ts b/src/libs/UnreadIndicatorUpdater/updateUnread/index.android.ts new file mode 100644 index 000000000000..bd7b2cbc4752 --- /dev/null +++ b/src/libs/UnreadIndicatorUpdater/updateUnread/index.android.ts @@ -0,0 +1,6 @@ +import UpdateUnread from './types'; + +// Android does not yet implement this +const updateUnread: UpdateUnread = () => {}; + +export default updateUnread; diff --git a/src/libs/UnreadIndicatorUpdater/updateUnread/index.desktop.js b/src/libs/UnreadIndicatorUpdater/updateUnread/index.desktop.ts similarity index 77% rename from src/libs/UnreadIndicatorUpdater/updateUnread/index.desktop.js rename to src/libs/UnreadIndicatorUpdater/updateUnread/index.desktop.ts index b9a689e89c42..e5d2fca43bf4 100644 --- a/src/libs/UnreadIndicatorUpdater/updateUnread/index.desktop.js +++ b/src/libs/UnreadIndicatorUpdater/updateUnread/index.desktop.ts @@ -1,14 +1,14 @@ import ELECTRON_EVENTS from '../../../../desktop/ELECTRON_EVENTS'; +import UpdateUnread from './types'; /** * Set the badge on desktop * - * @param {Number} totalCount */ -function updateUnread(totalCount) { +const updateUnread: UpdateUnread = (totalCount) => { // Ask the main Electron process to update our // badge count in the Mac OS dock icon window.electron.send(ELECTRON_EVENTS.REQUEST_UPDATE_BADGE_COUNT, totalCount); -} +}; export default updateUnread; diff --git a/src/libs/UnreadIndicatorUpdater/updateUnread/index.ios.js b/src/libs/UnreadIndicatorUpdater/updateUnread/index.ios.ts similarity index 69% rename from src/libs/UnreadIndicatorUpdater/updateUnread/index.ios.js rename to src/libs/UnreadIndicatorUpdater/updateUnread/index.ios.ts index 62ee00828566..d6fac7c7771d 100644 --- a/src/libs/UnreadIndicatorUpdater/updateUnread/index.ios.js +++ b/src/libs/UnreadIndicatorUpdater/updateUnread/index.ios.ts @@ -1,13 +1,12 @@ import Airship from '@ua/react-native-airship'; +import UpdateUnread from './types'; /** * Set the App Icon badge with the number of * unread messages on iOS - * - * @param {Number} totalCount */ -function updateUnread(totalCount) { +const updateUnread: UpdateUnread = (totalCount) => { Airship.push.iOS.setBadgeNumber(totalCount); -} +}; export default updateUnread; diff --git a/src/libs/UnreadIndicatorUpdater/updateUnread/index.website.js b/src/libs/UnreadIndicatorUpdater/updateUnread/index.ts similarity index 76% rename from src/libs/UnreadIndicatorUpdater/updateUnread/index.website.js rename to src/libs/UnreadIndicatorUpdater/updateUnread/index.ts index 56aa7f02509d..46015f4e5e03 100644 --- a/src/libs/UnreadIndicatorUpdater/updateUnread/index.website.js +++ b/src/libs/UnreadIndicatorUpdater/updateUnread/index.ts @@ -2,14 +2,13 @@ * Web browsers have a tab title and favicon which can be updated to show there are unread comments */ import CONFIG from '@src/CONFIG'; +import UpdateUnread from './types'; let unreadTotalCount = 0; /** * Set the page title on web - * - * @param {Number} totalCount */ -function updateUnread(totalCount) { +const updateUnread: UpdateUnread = (totalCount) => { const hasUnread = totalCount !== 0; unreadTotalCount = totalCount; // This setTimeout is required because due to how react rendering messes with the DOM, the document title can't be modified synchronously, and we must wait until all JS is done @@ -19,9 +18,12 @@ function updateUnread(totalCount) { // seems to improve this issue. document.title = ''; document.title = hasUnread ? `(${totalCount}) ${CONFIG.SITE_TITLE}` : CONFIG.SITE_TITLE; - document.getElementById('favicon').href = hasUnread ? CONFIG.FAVICON.UNREAD : CONFIG.FAVICON.DEFAULT; + const favicon = document.getElementById('favicon'); + if (favicon instanceof HTMLLinkElement) { + favicon.href = hasUnread ? CONFIG.FAVICON.UNREAD : CONFIG.FAVICON.DEFAULT; + } }, 0); -} +}; window.addEventListener('popstate', () => { updateUnread(unreadTotalCount); diff --git a/src/libs/UnreadIndicatorUpdater/updateUnread/types.ts b/src/libs/UnreadIndicatorUpdater/updateUnread/types.ts new file mode 100644 index 000000000000..afbb2741a454 --- /dev/null +++ b/src/libs/UnreadIndicatorUpdater/updateUnread/types.ts @@ -0,0 +1,3 @@ +type UpdateUnread = (totalCount: number) => void; + +export default UpdateUnread;