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 'PushNotification' lib to TypeScript #32180

Merged
merged 14 commits into from
Dec 6, 2023
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
2 changes: 1 addition & 1 deletion __mocks__/@ua/react-native-airship.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const Airship = {
},
contact: {
identify: jest.fn(),
getNamedUserId: jest.fn(),
getNamedUserId: () => Promise.resolve(undefined),
reset: jest.fn(),
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Airship from '@ua/react-native-airship';
import shouldShowPushNotification from '@libs/Notification/PushNotification/shouldShowPushNotification';
import ForegroundNotificationsModule from './types';

function configureForegroundNotifications() {
Airship.push.android.setForegroundDisplayPredicate((pushPayload) => Promise.resolve(shouldShowPushNotification(pushPayload)));
Expand All @@ -9,7 +10,9 @@ function disableForegroundNotifications() {
Airship.push.android.setForegroundDisplayPredicate(() => Promise.resolve(false));
}

export default {
const ForegroundNotifications: ForegroundNotificationsModule = {
configureForegroundNotifications,
disableForegroundNotifications,
};

export default ForegroundNotifications;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Airship, {iOS} from '@ua/react-native-airship';
import shouldShowPushNotification from '@libs/Notification/PushNotification/shouldShowPushNotification';
import ForegroundNotificationsModule from './types';

function configureForegroundNotifications() {
// Set our default iOS foreground presentation to be loud with a banner
Expand All @@ -20,7 +21,9 @@ function disableForegroundNotifications() {
Airship.push.iOS.setForegroundPresentationOptionsCallback(() => Promise.resolve([]));
}

export default {
const ForegroundNotifications: ForegroundNotificationsModule = {
configureForegroundNotifications,
disableForegroundNotifications,
};

export default ForegroundNotifications;
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ForegroundNotificationsModule from './types';

/**
* Configures notification handling while in the foreground on iOS and Android. This is a no-op on other platforms.
*/
export default {
const ForegroundNotifications: ForegroundNotificationsModule = {
configureForegroundNotifications: () => {},
disableForegroundNotifications: () => {},
};

export default ForegroundNotifications;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type ForegroundNotificationsModule = {
configureForegroundNotifications: () => void;
disableForegroundNotifications: () => void;
};

export default ForegroundNotificationsModule;
7 changes: 0 additions & 7 deletions src/libs/Notification/PushNotification/NotificationType.js

This file was deleted.

28 changes: 28 additions & 0 deletions src/libs/Notification/PushNotification/NotificationType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {OnyxServerUpdate} from '@src/types/onyx/OnyxUpdatesFromServer';

const NotificationType = {
REPORT_COMMENT: 'reportComment',
} as const;

type NotificationDataMap = {
[NotificationType.REPORT_COMMENT]: ReportCommentNotificationData;
};

type NotificationData = ReportCommentNotificationData;
fabioh8010 marked this conversation as resolved.
Show resolved Hide resolved

type ReportCommentNotificationData = {
title: string;
type: typeof NotificationType.REPORT_COMMENT;
reportID: number;
reportActionID: string;
shouldScrollToLastUnread?: boolean;
roomName?: string;
onyxData?: OnyxServerUpdate[];
};

/**
* See https://github.com/Expensify/Web-Expensify/blob/main/lib/MobilePushNotifications.php for the various
* types of push notifications sent by our API.
*/
export default NotificationType;
export type {NotificationDataMap, NotificationData, ReportCommentNotificationData};
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import Visibility from '@libs/Visibility';
import * as App from '@userActions/App';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import BackgroundRefresh from './types';

function getLastOnyxUpdateID() {
function getLastOnyxUpdateID(): Promise<number | null> {
return new Promise((resolve) => {
const connectionID = Onyx.connect({
key: ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT,
Expand All @@ -23,7 +24,7 @@ function getLastOnyxUpdateID() {
* We use this to refresh the app in the background after receiving a push notification (native only). Since the full app
* wakes on iOS and by extension runs reconnectApp already, this is a no-op on everything but Android.
*/
export default function backgroundRefresh() {
const backgroundRefresh: BackgroundRefresh = () => {
if (Visibility.isVisible()) {
return;
}
Expand All @@ -38,9 +39,11 @@ export default function backgroundRefresh() {
* See more here: https://reactnative.dev/docs/headless-js-android
*/
App.confirmReadyToOpenApp();
App.reconnectApp(lastUpdateIDAppliedToClient);
App.reconnectApp(lastUpdateIDAppliedToClient ?? undefined);
})
.catch((error) => {
Log.alert(`${CONST.ERROR.ENSURE_BUGBOT} [PushNotification] backgroundRefresh failed. This should never happen.`, {error});
});
}
};

export default backgroundRefresh;
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import BackgroundRefresh from './types';

/**
* Runs our reconnectApp action if the app is in the background.
*
* We use this to refresh the app in the background after receiving a push notification (native only). Since the full app
* wakes on iOS and by extension runs reconnectApp already, this is a no-op on everything but Android.
*/
export default function backgroundRefresh() {}
const backgroundRefresh: BackgroundRefresh = () => {};

export default backgroundRefresh;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type BackgroundRefresh = () => void;

export default BackgroundRefresh;
Loading
Loading