Skip to content

Commit

Permalink
Merge pull request #32272 from kubabutkiewicz/ts-migration/UnreadIndi…
Browse files Browse the repository at this point in the history
…catorUpdater/lib

[TS migration] Migrate 'UnreadIndicatorUpdater' lib to TypeScript
  • Loading branch information
neil-marcellini authored Dec 8, 2023
2 parents 5f1a8b2 + 9b9b991 commit adbeba1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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<Report> = {};

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({
Expand Down
2 changes: 0 additions & 2 deletions src/libs/UnreadIndicatorUpdater/updateUnread/index.android.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/libs/UnreadIndicatorUpdater/updateUnread/index.android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import UpdateUnread from './types';

// Android does not yet implement this
const updateUnread: UpdateUnread = () => {};

export default updateUnread;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/libs/UnreadIndicatorUpdater/updateUnread/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type UpdateUnread = (totalCount: number) => void;

export default UpdateUnread;

0 comments on commit adbeba1

Please sign in to comment.