diff --git a/src/CONST/index.ts b/src/CONST/index.ts index b4ea8d45db10c..58cc8c0b116c7 100755 --- a/src/CONST/index.ts +++ b/src/CONST/index.ts @@ -1613,6 +1613,8 @@ const CONST = { }, TELEMETRY: { CONTEXT_FULLSTORY: 'Fullstory', + CONTEXT_POLICIES: 'Policies', + TAG_ACTIVE_POLICY: 'active_policy_id', }, PRIORITY_MODE: { GSD: 'gsd', diff --git a/src/Expensify.tsx b/src/Expensify.tsx index 65227bac9d888..670dbfa89afd6 100644 --- a/src/Expensify.tsx +++ b/src/Expensify.tsx @@ -44,6 +44,8 @@ import './libs/registerPaginationConfig'; import setCrashlyticsUserId from './libs/setCrashlyticsUserId'; import StartupTimer from './libs/StartupTimer'; // This lib needs to be imported, but it has nothing to export since all it contains is an Onyx connection +import './libs/TelemetrySynchronizer'; +// This lib needs to be imported, but it has nothing to export since all it contains is an Onyx connection import './libs/UnreadIndicatorUpdater'; import Visibility from './libs/Visibility'; import ONYXKEYS from './ONYXKEYS'; diff --git a/src/libs/TelemetrySynchronizer.ts b/src/libs/TelemetrySynchronizer.ts new file mode 100644 index 0000000000000..4a87200b3b71d --- /dev/null +++ b/src/libs/TelemetrySynchronizer.ts @@ -0,0 +1,62 @@ +/** + * This file contains the logic for sending additional data to Sentry. + * + * It uses Onyx.connectWithoutView as nothing here is related to the UI. We only send data to the external provider and want to keep this outside of the render loop. + */ +import * as Sentry from '@sentry/react-native'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; +import Onyx from 'react-native-onyx'; +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Policy, Session} from '@src/types/onyx'; +import {getActivePolicies} from './PolicyUtils'; + +/** + * Connect to Onyx to retrieve information about the user's active policies. + */ +let session: OnyxEntry; +let activePolicyID: OnyxEntry; +let policies: OnyxCollection; + +Onyx.connectWithoutView({ + key: ONYXKEYS.NVP_ACTIVE_POLICY_ID, + callback: (value) => { + if (!value) { + return; + } + activePolicyID = value; + sendPoliciesContext(); + }, +}); + +Onyx.connectWithoutView({ + key: ONYXKEYS.SESSION, + callback: (value) => { + if (!value?.email) { + return; + } + session = value; + sendPoliciesContext(); + }, +}); + +Onyx.connectWithoutView({ + key: ONYXKEYS.COLLECTION.POLICY, + waitForCollectionCallback: true, + callback: (value) => { + if (!value) { + return; + } + policies = value; + sendPoliciesContext(); + }, +}); + +function sendPoliciesContext() { + if (!policies || !session?.email || !activePolicyID) { + return; + } + const activePolicies = getActivePolicies(policies, session.email).map((policy) => policy.id); + Sentry.setTag(CONST.TELEMETRY.TAG_ACTIVE_POLICY, activePolicyID); + Sentry.setContext(CONST.TELEMETRY.CONTEXT_POLICIES, {activePolicyID, activePolicies}); +}