From e2a7965e438db9f70d76d2d7a911aa4c4a42c122 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 13 Dec 2024 17:14:41 +0000 Subject: Gate bitdrift integration (#7088) * Move Statsig init call earlier * Gate Bitdrift init call * Remove IS_TEST env constant * Mock statsig --- src/App.native.tsx | 7 +------ src/env.ts | 1 - src/lib/bitdrift.ts | 19 ++++++++++++++++--- src/lib/statsig/statsig.tsx | 5 ++--- src/logger/README.md | 2 +- src/logger/__tests__/logger.test.ts | 1 - src/logger/index.ts | 6 +++--- src/state/session/__tests__/session-test.ts | 9 +++++++++ src/view/com/util/Toast.tsx | 5 +++-- 9 files changed, 35 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/App.native.tsx b/src/App.native.tsx index c22a66e82..f985b96a5 100644 --- a/src/App.native.tsx +++ b/src/App.native.tsx @@ -17,11 +17,7 @@ import {useLingui} from '@lingui/react' import {KeyboardControllerProvider} from '#/lib/hooks/useEnableKeyboardController' import {QueryProvider} from '#/lib/react-query' -import { - initialize, - Provider as StatsigProvider, - tryFetchGates, -} from '#/lib/statsig/statsig' +import {Provider as StatsigProvider, tryFetchGates} from '#/lib/statsig/statsig' import {s} from '#/lib/styles' import {ThemeProvider} from '#/lib/ThemeContext' import I18nProvider from '#/locale/i18nProvider' @@ -101,7 +97,6 @@ function InnerApp() { if (account) { await resumeSession(account) } else { - await initialize() await tryFetchGates(undefined, 'prefer-fresh-gates') } } catch (e) { diff --git a/src/env.ts b/src/env.ts index fbcc15f57..4d1b55a4e 100644 --- a/src/env.ts +++ b/src/env.ts @@ -1,4 +1,3 @@ -export const IS_TEST = process.env.EXPO_PUBLIC_ENV === 'test' export const IS_DEV = __DEV__ export const IS_PROD = !IS_DEV export const LOG_DEBUG = process.env.EXPO_PUBLIC_LOG_DEBUG || '' diff --git a/src/lib/bitdrift.ts b/src/lib/bitdrift.ts index 2b22155e7..02d074e76 100644 --- a/src/lib/bitdrift.ts +++ b/src/lib/bitdrift.ts @@ -1,7 +1,20 @@ import {init} from '@bitdrift/react-native' +import {Statsig} from 'statsig-react-native-expo' + +import {initPromise} from './statsig/statsig' const BITDRIFT_API_KEY = process.env.BITDRIFT_API_KEY -if (BITDRIFT_API_KEY) { - init(BITDRIFT_API_KEY, {url: 'https://api-bsky.bitdrift.io'}) -} +initPromise.then(() => { + let isEnabled = false + try { + if (Statsig.checkGate('enable_bitdrift')) { + isEnabled = true + } + } catch (e) { + // Statsig may complain about it being called too early. + } + if (isEnabled && BITDRIFT_API_KEY) { + init(BITDRIFT_API_KEY, {url: 'https://api-bsky.bitdrift.io'}) + } +}) diff --git a/src/lib/statsig/statsig.tsx b/src/lib/statsig/statsig.tsx index 51d7eb98e..21fa4bb57 100644 --- a/src/lib/statsig/statsig.tsx +++ b/src/lib/statsig/statsig.tsx @@ -16,6 +16,8 @@ import {Gate} from './gates' const SDK_KEY = 'client-SXJakO39w9vIhl3D44u8UupyzFl4oZ2qPIkjwcvuPsV' +export const initPromise = initialize() + type StatsigUser = { userID: string | undefined // TODO: Remove when enough users have custom.platform: @@ -219,9 +221,6 @@ export async function tryFetchGates( // Use this for less common operations where the user would be OK with a delay. timeoutMs = 1500 } - // Note: This condition is currently false the very first render because - // Statsig has not initialized yet. In the future, we can fix this by - // doing the initialization ourselves instead of relying on the provider. if (Statsig.initializeCalled()) { await Promise.race([ timeout(timeoutMs), diff --git a/src/logger/README.md b/src/logger/README.md index 1dfd5da23..17dd61cb3 100644 --- a/src/logger/README.md +++ b/src/logger/README.md @@ -18,7 +18,7 @@ logger.error(error[, metadata]) #### Modes The "modes" referred to here are inferred from the values exported from `#/env`. -Basically, the booleans `IS_DEV`, `IS_TEST`, and `IS_PROD`. +Basically, the booleans `IS_DEV` and `IS_PROD`. #### Log Levels diff --git a/src/logger/__tests__/logger.test.ts b/src/logger/__tests__/logger.test.ts index a3ccd037d..02039d26e 100644 --- a/src/logger/__tests__/logger.test.ts +++ b/src/logger/__tests__/logger.test.ts @@ -5,7 +5,6 @@ import {nanoid} from 'nanoid/non-secure' import {Logger, LogLevel, sentryTransport} from '#/logger' jest.mock('#/env', () => ({ - IS_TEST: true, IS_DEV: false, IS_PROD: false, /* diff --git a/src/logger/index.ts b/src/logger/index.ts index 02e5d5f25..d99bfeb13 100644 --- a/src/logger/index.ts +++ b/src/logger/index.ts @@ -173,7 +173,7 @@ export class Logger { protected debugContextRegexes: RegExp[] = [] constructor({ - enabled = !env.IS_TEST, + enabled = process.env.NODE_ENV !== 'test', level = env.LOG_LEVEL as LogLevel, debug = env.LOG_DEBUG || '', }: { @@ -266,11 +266,11 @@ export class Logger { */ export const logger = new Logger() -if (!env.IS_TEST) { +if (process.env.NODE_ENV !== 'test') { logger.addTransport(createBitdriftTransport()) } -if (env.IS_DEV && !env.IS_TEST) { +if (env.IS_DEV && process.env.NODE_ENV !== 'test') { logger.addTransport(consoleTransport) /* diff --git a/src/state/session/__tests__/session-test.ts b/src/state/session/__tests__/session-test.ts index 4c58c6323..7a5ddfa97 100644 --- a/src/state/session/__tests__/session-test.ts +++ b/src/state/session/__tests__/session-test.ts @@ -4,6 +4,15 @@ import {describe, expect, it, jest} from '@jest/globals' import {agentToSessionAccountOrThrow} from '../agent' import {Action, getInitialState, reducer, State} from '../reducer' +jest.mock('statsig-react-native-expo', () => ({ + Statsig: { + initialize() {}, + initializeCalled() { + return false + }, + }, +})) + jest.mock('jwt-decode', () => ({ jwtDecode(_token: string) { return {} diff --git a/src/view/com/util/Toast.tsx b/src/view/com/util/Toast.tsx index b57e676ae..7dc6837e2 100644 --- a/src/view/com/util/Toast.tsx +++ b/src/view/com/util/Toast.tsx @@ -25,7 +25,6 @@ import { import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback' import {atoms as a, useTheme} from '#/alf' import {Text} from '#/components/Typography' -import {IS_TEST} from '#/env' const TIMEOUT = 2e3 @@ -33,7 +32,9 @@ export function show( message: string, icon: FontAwesomeProps['icon'] = 'check', ) { - if (IS_TEST) return + if (process.env.NODE_ENV === 'test') { + return + } AccessibilityInfo.announceForAccessibility(message) const item = new RootSiblings( item.destroy()} />, -- cgit 1.4.1