Skip to content
22 changes: 22 additions & 0 deletions src/libs/Notification/triggerNotifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type {OnyxCollection} from 'react-native-onyx';
import {showReportActionNotification} from '@libs/actions/Report';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReportAction} from '@src/types/onyx';
import type {OnyxServerUpdate} from '@src/types/onyx/OnyxUpdatesFromServer';

export default function triggerNotifications(onyxUpdates: OnyxServerUpdate[]): void {
for (const update of onyxUpdates) {
if (!update.shouldNotify && !update.shouldShowPushNotification) {
continue;
}

const reportID = update.key.replace(ONYXKEYS.COLLECTION.REPORT_ACTIONS, '');
const reportActions = Object.values((update.value as OnyxCollection<ReportAction>) ?? {});

for (const action of reportActions) {
if (action) {
showReportActionNotification(reportID, action);
}
}
}
}
7 changes: 7 additions & 0 deletions src/libs/actions/OnyxUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {Platform} from 'react-native';
import type {OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {Merge} from 'type-fest';
import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import {isMobile} from '@libs/Browser';
import Log from '@libs/Log';
import triggerNotifications from '@libs/Notification/triggerNotifications';
import Performance from '@libs/Performance';
import PusherUtils from '@libs/PusherUtils';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -53,6 +56,10 @@ function applyHTTPSOnyxUpdates(request: Request, response: Response, lastUpdateI

return onyxDataUpdatePromise
.then(() => {
// Trigger notifications only on successful responses.
if (Platform.OS === 'web' && !isMobile() && response.jsonCode === 200 && response.onyxData?.length) {
triggerNotifications(response.onyxData);
}
Comment thread
marufsharifi marked this conversation as resolved.
Resolved
// Handle the request's success/failure data (client-side data)
if (response.jsonCode === 200 && request.successData) {
return updateHandler(request.successData);
Expand Down
19 changes: 1 addition & 18 deletions src/libs/actions/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import Navigation from '@libs/Navigation/Navigation';
import {isOffline} from '@libs/Network/NetworkStore';
import * as SequentialQueue from '@libs/Network/SequentialQueue';
import triggerNotifications from '@libs/Notification/triggerNotifications';
import * as NumberUtils from '@libs/NumberUtils';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import Pusher from '@libs/Pusher';
Expand All @@ -57,13 +58,12 @@
import {reconnectApp} from './App';
import applyOnyxUpdatesReliably from './applyOnyxUpdatesReliably';
import {openOldDotLink} from './Link';
import {showReportActionNotification} from './Report';
import {resendValidateCode as sessionResendValidateCode} from './Session';
import Timing from './Timing';

let currentUserAccountID = -1;
let currentEmail = '';
Onyx.connect({

Check warning on line 66 in src/libs/actions/User.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
currentUserAccountID = value?.accountID ?? CONST.DEFAULT_NUMBER_ID;
Expand All @@ -72,7 +72,7 @@
});

let myPersonalDetails: OnyxEntry<OnyxPersonalDetails>;
Onyx.connect({

Check warning on line 75 in src/libs/actions/User.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
if (!value || currentUserAccountID === -1) {
Expand All @@ -84,7 +84,7 @@
});

let allPolicies: OnyxCollection<Policy>;
Onyx.connect({

Check warning on line 87 in src/libs/actions/User.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
waitForCollectionCallback: true,
callback: (value) => (allPolicies = value),
Expand Down Expand Up @@ -660,26 +660,9 @@
return isBefore(new Date(), new Date(blockedFromConciergeNVP.expiresAt));
}

function triggerNotifications(onyxUpdates: OnyxServerUpdate[]) {
for (const update of onyxUpdates) {
if (!update.shouldNotify && !update.shouldShowPushNotification) {
continue;
}

const reportID = update.key.replace(ONYXKEYS.COLLECTION.REPORT_ACTIONS, '');
const reportActions = Object.values((update.value as OnyxCollection<ReportAction>) ?? {});

for (const action of reportActions) {
if (action) {
showReportActionNotification(reportID, action);
}
}
}
}

const isChannelMuted = (reportId: string) =>
new Promise((resolve) => {
const connection = Onyx.connect({

Check warning on line 665 in src/libs/actions/User.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: `${ONYXKEYS.COLLECTION.REPORT}${reportId}`,
callback: (report) => {
Onyx.disconnect(connection);
Expand Down
Loading