Line data Source code
1 : import { StorageSerializers, createGlobalState, useStorage } from '@vueuse/core'
2 :
3 : import type { ServerFeatureFlag } from '@/platform/remoteConfig/serverFeatureFlag'
4 :
5 : /**
6 : * Remote configuration service
7 : *
8 : * Fetches configuration from the server at runtime, enabling:
9 : * - Feature flags without rebuilding
10 : * - Server-side feature discovery
11 : * - Version compatibility management
12 : * - Avoiding vendor lock-in for native apps
13 : *
14 : * This module is tree-shaken in OSS builds.
15 : */
16 :
17 : import { computed, ref } from 'vue'
18 :
19 : import type { RemoteConfig } from './types'
20 :
21 : /**
22 : * Load state for remote configuration.
23 : * - 'unloaded': No config loaded yet
24 : * - 'anonymous': Config loaded without auth (bootstrap)
25 : * - 'authenticated': Config loaded with auth (user-specific flags available)
26 : * - 'error': Failed to load config
27 : */
28 : type RemoteConfigState = 'unloaded' | 'anonymous' | 'authenticated' | 'error'
29 : export type AuthenticatedRemoteConfigState =
30 : | 'unloaded'
31 : | 'loading'
32 : | 'authenticated'
33 : | 'error'
34 :
35 : /**
36 : * Current load state of remote configuration
37 : */
38 2356 : export const remoteConfigState = ref<RemoteConfigState>('unloaded')
39 2356 : export const authenticatedRemoteConfigState =
40 : ref<AuthenticatedRemoteConfigState>('unloaded')
41 2356 : export const remoteConfigRevision = ref(0)
42 :
43 2356 : export const remoteConfigErrorStatus = ref<number | null>(null)
44 :
45 : /** Whether the authenticated config has been loaded. */
46 2356 : export const isAuthenticatedConfigLoaded = computed(
47 : () => authenticatedRemoteConfigState.value === 'authenticated'
48 : )
49 :
50 : /**
51 : * Reactive remote configuration
52 : * Updated whenever config is loaded from the server
53 : */
54 2356 : export const remoteConfig = ref<RemoteConfig>({})
55 :
56 9354 : export function configValueOrDefault<K extends keyof RemoteConfig>(
57 : remoteConfig: RemoteConfig,
58 : key: K,
59 : defaultValue: NonNullable<RemoteConfig[K]>
60 : ): NonNullable<RemoteConfig[K]> {
61 : const configValue = remoteConfig[key]
62 9354 : return configValue || defaultValue
63 : }
64 :
65 : /** Last authenticated agent-allowlist answer, kept in memory for this session and never persisted. */
66 2356 : export const sessionAgentGrant = ref<boolean | undefined>()
67 2356 : export const sessionAgentGrantValidUntil = ref<number | undefined>()
68 :
69 0 : function createRemoteConfigCache() {
70 0 : return {
71 : cachedBillingControlEnabled: useStorage<boolean | undefined>(
72 : 'billing_control_enabled' satisfies `${ServerFeatureFlag.BILLING_CONTROL_ENABLED}`,
73 : undefined,
74 : undefined,
75 : { serializer: StorageSerializers.boolean }
76 : ),
77 : cachedV1PaymentRecovery: useStorage<boolean | undefined>(
78 : 'v1_payment_recovery' satisfies `${ServerFeatureFlag.V1_PAYMENT_RECOVERY}`,
79 : undefined,
80 : undefined,
81 : { serializer: StorageSerializers.boolean }
82 : )
83 : }
84 : }
85 :
86 : /** Persisted server-flag cache, created on first use and shared by every caller. */
87 2356 : export const useRemoteConfigCache = createGlobalState(createRemoteConfigCache)
|