Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate 'LocalNotification' lib to TypeScript #32094

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
// 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;

/**
* Checks if the user has granted permission to show browser notifications
*
* @return {Promise}
*/
function canUseBrowserNotifications() {
function canUseBrowserNotifications(): Promise<boolean> {
return new Promise((resolve) => {
// They have no browser notifications so we can't use this feature
if (!window.Notification) {
Expand All @@ -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<void | Notification> {
return new Promise((resolve) => {
if (!title || !body) {
throw new Error('BrowserNotification must include title and body parameter.');
Expand All @@ -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
Expand Down Expand Up @@ -91,24 +80,19 @@ 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 = ReportUtils.getReportName(report);
Expand All @@ -120,17 +104,17 @@ export default {
}

push({
title,
title: title ?? '',
body,
delay: 0,
onClick,
icon: usesIcon ? EXPENSIFY_ICON_URL : '',
});
},

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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions src/libs/Notification/LocalNotification/focusApp/index.ts
Original file line number Diff line number Diff line change
@@ -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;

This file was deleted.

3 changes: 3 additions & 0 deletions src/libs/Notification/LocalNotification/focusApp/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type FocusApp = () => void;

export default FocusApp;
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
import BrowserNotifications from './BrowserNotifications';
import {LocalNotificationModule, ReportCommentParams} from './types';

/**
* @param {Object} options
* @param {Object} options.report
* @param {Object} options.reportAction
* @param {Function} options.onClick
*/
function showCommentNotification({report, reportAction, onClick}) {
function showCommentNotification({report, reportAction, onClick}: ReportCommentParams) {
BrowserNotifications.pushReportCommentNotification({report, reportAction, onClick});
}

function showUpdateAvailableNotification() {
BrowserNotifications.pushUpdateAvailableNotification();
}

/**
* @param {Object} options
* @param {Object} options.report
* @param {Object} options.reportAction
* @param {Function} options.onClick
*/
function showModifiedExpenseNotification({report, reportAction, onClick}) {
function showModifiedExpenseNotification({report, reportAction, onClick}: ReportCommentParams) {
BrowserNotifications.pushModifiedExpenseNotification({report, reportAction, onClick});
}

export default {
const LocalNotification: LocalNotificationModule = {
showCommentNotification,
showUpdateAvailableNotification,
showModifiedExpenseNotification,
};

export default LocalNotification;
mountiny marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
import BrowserNotifications from './BrowserNotifications';
import {LocalNotificationModule, ReportCommentParams} from './types';

/**
* @param {Object} options
* @param {Object} options.report
* @param {Object} options.reportAction
* @param {Function} options.onClick
*/
function showCommentNotification({report, reportAction, onClick}) {
function showCommentNotification({report, reportAction, onClick}: ReportCommentParams) {
BrowserNotifications.pushReportCommentNotification({report, reportAction, onClick}, true);
}

function showUpdateAvailableNotification() {
BrowserNotifications.pushUpdateAvailableNotification();
}

/**
* @param {Object} options
* @param {Object} options.report
* @param {Object} options.reportAction
* @param {Function} options.onClick
*/
function showModifiedExpenseNotification({report, reportAction, onClick}) {
function showModifiedExpenseNotification({report, reportAction, onClick}: ReportCommentParams) {
BrowserNotifications.pushModifiedExpenseNotification({report, reportAction, onClick}, true);
}

export default {
const LocalNotification: LocalNotificationModule = {
showCommentNotification,
showUpdateAvailableNotification,
showModifiedExpenseNotification,
};

export default LocalNotification;
25 changes: 25 additions & 0 deletions src/libs/Notification/LocalNotification/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {ImageSourcePropType} from 'react-native';
import {Report, ReportAction} from '@src/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;
showModifiedExpenseNotification: (reportCommentParams: ReportCommentParams) => void;
};

export type {PushParams, ReportCommentParams, LocalNotificationModule};
Loading