From 71e6a2fd7d2f3489d9531246718ffae07e6d14ab Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Mon, 25 Sep 2023 16:13:49 +0200 Subject: [PATCH 1/3] ref: move LocalNotification to TS --- ...tifications.js => BrowserNotifications.ts} | 44 ++++++------------- .../{index.desktop.js => index.desktop.ts} | 5 ++- .../LocalNotification/focusApp/index.ts | 6 +++ .../focusApp/index.website.js | 2 - .../LocalNotification/focusApp/types.ts | 3 ++ .../{index.desktop.js => index.desktop.ts} | 8 ++-- .../{index.native.js => index.native.ts} | 0 .../{index.website.js => index.ts} | 7 ++- .../Notification/LocalNotification/types.ts | 24 ++++++++++ 9 files changed, 61 insertions(+), 38 deletions(-) rename src/libs/Notification/LocalNotification/{BrowserNotifications.js => BrowserNotifications.ts} (77%) rename src/libs/Notification/LocalNotification/focusApp/{index.desktop.js => index.desktop.ts} (59%) create mode 100644 src/libs/Notification/LocalNotification/focusApp/index.ts delete mode 100644 src/libs/Notification/LocalNotification/focusApp/index.website.js create mode 100644 src/libs/Notification/LocalNotification/focusApp/types.ts rename src/libs/Notification/LocalNotification/{index.desktop.js => index.desktop.ts} (57%) rename src/libs/Notification/LocalNotification/{index.native.js => index.native.ts} (100%) rename src/libs/Notification/LocalNotification/{index.website.js => index.ts} (68%) create mode 100644 src/libs/Notification/LocalNotification/types.ts diff --git a/src/libs/Notification/LocalNotification/BrowserNotifications.js b/src/libs/Notification/LocalNotification/BrowserNotifications.ts similarity index 77% rename from src/libs/Notification/LocalNotification/BrowserNotifications.js rename to src/libs/Notification/LocalNotification/BrowserNotifications.ts index e55c0430fe17..fcf795c6769c 100644 --- a/src/libs/Notification/LocalNotification/BrowserNotifications.js +++ b/src/libs/Notification/LocalNotification/BrowserNotifications.ts @@ -1,18 +1,16 @@ // Web and desktop implementation only. Do not import for direct use. Use LocalNotification. -import _ from 'underscore'; -import focusApp from './focusApp'; import * as AppUpdate from '../../actions/AppUpdate'; import EXPENSIFY_ICON_URL from '../../../../assets/images/expensify-logo-round-clearspace.png'; import * as ReportUtils from '../../ReportUtils'; +import {PushParams, ReportCommentParams} from './types'; +import focusApp from './focusApp'; const DEFAULT_DELAY = 4000; /** * Checks if the user has granted permission to show browser notifications - * - * @return {Promise} */ -function canUseBrowserNotifications() { +function canUseBrowserNotifications(): Promise { return new Promise((resolve) => { // They have no browser notifications so we can't use this feature if (!window.Notification) { @@ -36,18 +34,9 @@ function canUseBrowserNotifications() { /** * Light abstraction around browser push notifications. * Checks for permission before determining whether to send. - * - * @param {Object} params - * @param {String} params.title - * @param {String} params.body - * @param {String} [params.icon] Path to icon - * @param {Number} [params.delay] - * @param {Function} [params.onClick] - * @param {String} [params.tag] - * - * @return {Promise} - resolves with Notification object or undefined + * @return resolves with Notification object or undefined */ -function push({title, body, delay = DEFAULT_DELAY, onClick = () => {}, tag = '', icon}) { +function push({title, body, delay = DEFAULT_DELAY, onClick = () => {}, tag = '', icon}: PushParams): Promise { return new Promise((resolve) => { if (!title || !body) { throw new Error('BrowserNotification must include title and body parameter.'); @@ -62,7 +51,7 @@ function push({title, body, delay = DEFAULT_DELAY, onClick = () => {}, tag = '', const notification = new Notification(title, { body, tag, - icon, + icon: String(icon), }); // If we pass in a delay param greater than 0 the notification @@ -91,27 +80,22 @@ function push({title, body, delay = DEFAULT_DELAY, onClick = () => {}, tag = '', export default { /** * Create a report comment notification - * - * @param {Object} params - * @param {Object} params.report - * @param {Object} params.reportAction - * @param {Function} params.onClick - * @param {Boolean} usesIcon true if notification uses right circular icon + * @param usesIcon true if notification uses right circular icon */ - pushReportCommentNotification({report, reportAction, onClick}, usesIcon = false) { - let title; - let body; + pushReportCommentNotification({report, reportAction, onClick}: ReportCommentParams, usesIcon = false) { + let title: string | undefined; + let body: string | undefined; const isChatRoom = ReportUtils.isChatRoom(report); const {person, message} = reportAction; - const plainTextPerson = _.map(person, (f) => f.text).join(); + const plainTextPerson = person?.map((f) => f.text).join(); // Specifically target the comment part of the message - const plainTextMessage = (_.find(message, (f) => f.type === 'COMMENT') || {}).text; + const plainTextMessage = message?.find((f) => f.type === 'COMMENT')?.text; if (isChatRoom) { - const roomName = _.get(report, 'displayName', ''); + const roomName = report.displayName ?? ''; title = roomName; body = `${plainTextPerson}: ${plainTextMessage}`; } else { @@ -120,7 +104,7 @@ export default { } push({ - title, + title: title ?? '', body, delay: 0, onClick, diff --git a/src/libs/Notification/LocalNotification/focusApp/index.desktop.js b/src/libs/Notification/LocalNotification/focusApp/index.desktop.ts similarity index 59% rename from src/libs/Notification/LocalNotification/focusApp/index.desktop.js rename to src/libs/Notification/LocalNotification/focusApp/index.desktop.ts index f00c72dc1af8..2b6f01457f44 100644 --- a/src/libs/Notification/LocalNotification/focusApp/index.desktop.js +++ b/src/libs/Notification/LocalNotification/focusApp/index.desktop.ts @@ -1,5 +1,8 @@ import ELECTRON_EVENTS from '../../../../../desktop/ELECTRON_EVENTS'; +import FocusApp from './types'; -export default () => { +const focusApp: FocusApp = () => { window.electron.send(ELECTRON_EVENTS.REQUEST_FOCUS_APP); }; + +export default focusApp; diff --git a/src/libs/Notification/LocalNotification/focusApp/index.ts b/src/libs/Notification/LocalNotification/focusApp/index.ts new file mode 100644 index 000000000000..8504fde1bcb2 --- /dev/null +++ b/src/libs/Notification/LocalNotification/focusApp/index.ts @@ -0,0 +1,6 @@ +import FocusApp from './types'; + +// On web this is up to the browser that shows the notifications +const focusApp: FocusApp = () => {}; + +export default focusApp; diff --git a/src/libs/Notification/LocalNotification/focusApp/index.website.js b/src/libs/Notification/LocalNotification/focusApp/index.website.js deleted file mode 100644 index ce7439d94cc3..000000000000 --- a/src/libs/Notification/LocalNotification/focusApp/index.website.js +++ /dev/null @@ -1,2 +0,0 @@ -// On web this is up to the browser that shows the notifications -export default () => {}; diff --git a/src/libs/Notification/LocalNotification/focusApp/types.ts b/src/libs/Notification/LocalNotification/focusApp/types.ts new file mode 100644 index 000000000000..534b7500fc71 --- /dev/null +++ b/src/libs/Notification/LocalNotification/focusApp/types.ts @@ -0,0 +1,3 @@ +type FocusApp = () => void; + +export default FocusApp; diff --git a/src/libs/Notification/LocalNotification/index.desktop.js b/src/libs/Notification/LocalNotification/index.desktop.ts similarity index 57% rename from src/libs/Notification/LocalNotification/index.desktop.js rename to src/libs/Notification/LocalNotification/index.desktop.ts index 2bef51cea0a6..acc3b8857c0f 100644 --- a/src/libs/Notification/LocalNotification/index.desktop.js +++ b/src/libs/Notification/LocalNotification/index.desktop.ts @@ -1,6 +1,7 @@ -import BrowserNotifications from './BrowserNotifications'; +import BrowserNotifications from './BrowserNotifications.js'; +import {LocalNotificationModule, ReportCommentParams} from './types.js'; -function showCommentNotification({report, reportAction, onClick}) { +function showCommentNotification({report, reportAction, onClick}: ReportCommentParams) { BrowserNotifications.pushReportCommentNotification({report, reportAction, onClick}); } @@ -8,7 +9,8 @@ function showUpdateAvailableNotification() { BrowserNotifications.pushUpdateAvailableNotification(); } -export default { +const LocalNotification: LocalNotificationModule = { showCommentNotification, showUpdateAvailableNotification, }; +export default LocalNotification; diff --git a/src/libs/Notification/LocalNotification/index.native.js b/src/libs/Notification/LocalNotification/index.native.ts similarity index 100% rename from src/libs/Notification/LocalNotification/index.native.js rename to src/libs/Notification/LocalNotification/index.native.ts diff --git a/src/libs/Notification/LocalNotification/index.website.js b/src/libs/Notification/LocalNotification/index.ts similarity index 68% rename from src/libs/Notification/LocalNotification/index.website.js rename to src/libs/Notification/LocalNotification/index.ts index 3410b3144caf..c0182d156d46 100644 --- a/src/libs/Notification/LocalNotification/index.website.js +++ b/src/libs/Notification/LocalNotification/index.ts @@ -1,6 +1,7 @@ import BrowserNotifications from './BrowserNotifications'; +import {LocalNotificationModule, ReportCommentParams} from './types'; -function showCommentNotification({report, reportAction, onClick}) { +function showCommentNotification({report, reportAction, onClick}: ReportCommentParams) { BrowserNotifications.pushReportCommentNotification({report, reportAction, onClick}, true); } @@ -8,7 +9,9 @@ function showUpdateAvailableNotification() { BrowserNotifications.pushUpdateAvailableNotification(); } -export default { +const LocalNotification: LocalNotificationModule = { showCommentNotification, showUpdateAvailableNotification, }; + +export default LocalNotification; diff --git a/src/libs/Notification/LocalNotification/types.ts b/src/libs/Notification/LocalNotification/types.ts new file mode 100644 index 000000000000..576297467155 --- /dev/null +++ b/src/libs/Notification/LocalNotification/types.ts @@ -0,0 +1,24 @@ +import {ImageSourcePropType} from 'react-native'; +import {ReportAction, Report} from '../../../types/onyx'; + +type PushParams = { + title: string; + body?: string; + icon?: string | ImageSourcePropType; + delay?: number; + onClick?: () => void; + tag?: string; +}; + +type ReportCommentParams = { + report: Report; + reportAction: ReportAction; + onClick: () => void; +}; + +type LocalNotificationModule = { + showCommentNotification: (reportCommentParams: ReportCommentParams) => void; + showUpdateAvailableNotification: () => void; +}; + +export type {PushParams, ReportCommentParams, LocalNotificationModule}; From 15900690cc7fd28edc452548144fa65be4303c0a Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Tue, 28 Nov 2023 10:34:59 +0100 Subject: [PATCH 2/3] fix: resolve types issues --- .../Notification/LocalNotification/BrowserNotifications.ts | 6 +++--- src/libs/Notification/LocalNotification/index.desktop.ts | 4 ++-- src/libs/Notification/LocalNotification/types.ts | 3 ++- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/libs/Notification/LocalNotification/BrowserNotifications.ts b/src/libs/Notification/LocalNotification/BrowserNotifications.ts index 8f54b8097d81..dd07ddbb5e33 100644 --- a/src/libs/Notification/LocalNotification/BrowserNotifications.ts +++ b/src/libs/Notification/LocalNotification/BrowserNotifications.ts @@ -1,9 +1,9 @@ // Web and desktop implementation only. Do not import for direct use. Use LocalNotification. -import _ from 'underscore'; import EXPENSIFY_ICON_URL from '@assets/images/expensify-logo-round-clearspace.png'; import * as ReportUtils from '@libs/ReportUtils'; import * as AppUpdate from '@userActions/AppUpdate'; import focusApp from './focusApp'; +import {PushParams, ReportCommentParams} from './types'; const DEFAULT_DELAY = 4000; @@ -112,9 +112,9 @@ export default { }); }, - pushModifiedExpenseNotification({reportAction, onClick}, usesIcon = false) { + pushModifiedExpenseNotification({reportAction, onClick}: ReportCommentParams, usesIcon = false) { push({ - title: _.map(reportAction.person, (f) => f.text).join(', '), + title: reportAction.person?.map((f) => f.text).join(', ') ?? '', body: ReportUtils.getModifiedExpenseMessage(reportAction), delay: 0, onClick, diff --git a/src/libs/Notification/LocalNotification/index.desktop.ts b/src/libs/Notification/LocalNotification/index.desktop.ts index a3e6c7289f1b..5ed6e607684c 100644 --- a/src/libs/Notification/LocalNotification/index.desktop.ts +++ b/src/libs/Notification/LocalNotification/index.desktop.ts @@ -1,5 +1,5 @@ -import BrowserNotifications from './BrowserNotifications.js'; -import {LocalNotificationModule, ReportCommentParams} from './types.js'; +import BrowserNotifications from './BrowserNotifications'; +import {LocalNotificationModule, ReportCommentParams} from './types'; function showCommentNotification({report, reportAction, onClick}: ReportCommentParams) { BrowserNotifications.pushReportCommentNotification({report, reportAction, onClick}); diff --git a/src/libs/Notification/LocalNotification/types.ts b/src/libs/Notification/LocalNotification/types.ts index 576297467155..25db8b080e2b 100644 --- a/src/libs/Notification/LocalNotification/types.ts +++ b/src/libs/Notification/LocalNotification/types.ts @@ -1,5 +1,5 @@ import {ImageSourcePropType} from 'react-native'; -import {ReportAction, Report} from '../../../types/onyx'; +import {Report, ReportAction} from '@src/types/onyx'; type PushParams = { title: string; @@ -19,6 +19,7 @@ type ReportCommentParams = { type LocalNotificationModule = { showCommentNotification: (reportCommentParams: ReportCommentParams) => void; showUpdateAvailableNotification: () => void; + showModifiedExpenseNotification: (reportCommentParams: ReportCommentParams) => void; }; export type {PushParams, ReportCommentParams, LocalNotificationModule}; From 0efc59e649176302ce3f6e8f0bfac23cd632c707 Mon Sep 17 00:00:00 2001 From: Jakub Butkiewicz Date: Tue, 28 Nov 2023 16:19:34 +0100 Subject: [PATCH 3/3] fix: resolve comments --- src/libs/Notification/LocalNotification/index.desktop.ts | 1 + src/libs/Notification/LocalNotification/index.native.ts | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/libs/Notification/LocalNotification/index.desktop.ts b/src/libs/Notification/LocalNotification/index.desktop.ts index 5ed6e607684c..1576fefe2118 100644 --- a/src/libs/Notification/LocalNotification/index.desktop.ts +++ b/src/libs/Notification/LocalNotification/index.desktop.ts @@ -18,4 +18,5 @@ const LocalNotification: LocalNotificationModule = { showUpdateAvailableNotification, showModifiedExpenseNotification, }; + export default LocalNotification; diff --git a/src/libs/Notification/LocalNotification/index.native.ts b/src/libs/Notification/LocalNotification/index.native.ts index 1f1771e619a5..b23378b6875a 100644 --- a/src/libs/Notification/LocalNotification/index.native.ts +++ b/src/libs/Notification/LocalNotification/index.native.ts @@ -1,6 +1,10 @@ +import {LocalNotificationModule} from './types'; + // Local Notifications are not currently supported on mobile so we'll just noop here. -export default { +const LocalNotification: LocalNotificationModule = { showCommentNotification: () => {}, showUpdateAvailableNotification: () => {}, showModifiedExpenseNotification: () => {}, }; + +export default LocalNotification;