Bluesky app fork with some witchin' additions 馃挮
witchsky.app
bluesky
fork
client
1import {MMKV} from 'react-native-mmkv'
2import {setPolyfills} from '@growthbook/growthbook'
3import {GrowthBook} from '@growthbook/growthbook-react'
4
5import {getNavigationMetadata, type Metadata} from '#/analytics/metadata'
6import * as env from '#/env'
7
8export {Features} from '#/analytics/features/types'
9
10const CACHE = new MMKV({id: 'bsky_features_cache'})
11
12setPolyfills({
13 localStorage: {
14 getItem: key => {
15 return CACHE.getString(key) ?? null
16 },
17 setItem: async (key, value) => {
18 CACHE.set(key, value)
19 },
20 },
21})
22
23/**
24 * We vary the amount of time we wait for GrowthBook to fetch feature
25 * gates based on the strategy specified.
26 */
27export type FeatureFetchStrategy = 'prefer-low-latency' | 'prefer-fresh-gates'
28
29export const features = new GrowthBook({
30 apiHost: env.GROWTHBOOK_API_HOST,
31 clientKey: env.GROWTHBOOK_CLIENT_KEY,
32})
33
34/**
35 * Kept as a resolved promise so existing startup code can await it without
36 * triggering any remote GrowthBook fetches.
37 */
38export const init = Promise.resolve()
39
40/**
41 * Refresh feature gates from GrowthBook. Updates attributes based on the
42 * provided account, if any.
43 */
44export async function refresh(_: {strategy: FeatureFetchStrategy}) {}
45
46/**
47 * Converts our metadata into GrowthBook attributes and sets them. GrowthBook
48 * attributes are manually configured in the GrowthBook dashboard. So these
49 * values need to match exactly. Therefore, let's add them here manually to and
50 * not spread them to avoid mistakes.
51 */
52export function setAttributes({
53 base,
54 geolocation,
55 session,
56 preferences,
57}: Metadata) {
58 features.setAttributes({
59 deviceId: base.deviceId,
60 sessionId: base.sessionId,
61 platform: base.platform,
62 appVersion: base.appVersion,
63 countryCode: geolocation.countryCode,
64 regionCode: geolocation.regionCode,
65 did: session?.did,
66 isBskyPds: session?.isBskyPds,
67 appLanguage: preferences?.appLanguage,
68 contentLanguages: preferences?.contentLanguages,
69 currentScreen: getNavigationMetadata()?.currentScreen,
70 })
71}