Line data Source code
1 : import { useIntervalFn } from '@vueuse/core'
2 : import { defineStore } from 'pinia'
3 : import { computed, watch } from 'vue'
4 : import { z } from 'zod'
5 :
6 : import { getComfyCloudBaseUrl } from '@/config/comfyApi'
7 : import {
8 : desktopHostTokenRecoveries,
9 : desktopHostUser
10 : } from '@/platform/auth/desktopHost/desktopHostSession'
11 : import {
12 : localBillingWebEnabled,
13 : localSurface,
14 : setLocalBillingNewUxEnabled,
15 : setLocalBillingWebEnabled
16 : } from '@/platform/billing/localBillingWebEnabled'
17 : import { isCloud } from '@/platform/distribution/types'
18 : import { useWorkspaceIdentity } from '@/platform/workspace/ports/workspaceIdentity'
19 : import { useAuthStore } from '@/stores/authStore'
20 : import { getDevOverride } from '@/utils/devFeatureFlagOverride'
21 :
22 2355 : const REREAD_INTERVAL_MS = 10 * 60_000
23 2355 : const READ_TIMEOUT_MS = 10_000
24 :
25 2355 : const zLocalBillingWeb = z.object({ local_billing_web: z.literal(true) })
26 2355 : const zBillingNewUx = z.object({ billing_new_ux_enabled: z.literal(true) })
27 :
28 : interface LocalCloudFlags {
29 : readonly localBillingWeb: boolean
30 : readonly billingNewUx: boolean
31 : }
32 :
33 2355 : const ALL_OFF: LocalCloudFlags = { localBillingWeb: false, billingNewUx: false }
34 :
35 25 : async function readServedFlags(): Promise<LocalCloudFlags> {
36 25 : try {
37 25 : const token = await useAuthStore().getIdToken()
38 25 : if (!token) return ALL_OFF
39 25 : const response = await fetch(
40 : new URL('/api/features', getComfyCloudBaseUrl()),
41 : {
42 : cache: 'no-store',
43 : headers: {
44 : Authorization: `Bearer ${token}`,
45 : 'X-Comfy-Surface': localSurface()
46 : },
47 : signal: AbortSignal.timeout(READ_TIMEOUT_MS)
48 : }
49 : )
50 25 : if (!response.ok) return ALL_OFF
51 25 : const served: unknown = await response.json()
52 24 : return {
53 : localBillingWeb: zLocalBillingWeb.safeParse(served).success,
54 : billingNewUx: zBillingNewUx.safeParse(served).success
55 : }
56 : } catch {
57 0 : return ALL_OFF
58 : }
59 : }
60 :
61 25 : async function readLocalCloudFlags(): Promise<LocalCloudFlags> {
62 25 : const override = getDevOverride<boolean>('local_billing_web')
63 25 : if (typeof override === 'boolean')
64 0 : return { localBillingWeb: override, billingNewUx: false }
65 25 : return readServedFlags()
66 : }
67 :
68 2355 : let generation = 0
69 :
70 2355 : export const useLocalBillingWebFlagStore = defineStore(
71 : 'localBillingWebFlag',
72 : () => {
73 2390 : function apply(flags: LocalCloudFlags): void {
74 2390 : setLocalBillingWebEnabled(flags.localBillingWeb)
75 2390 : setLocalBillingNewUxEnabled(flags.billingNewUx)
76 : }
77 :
78 2367 : function clear(): void {
79 2367 : generation++
80 2367 : apply(ALL_OFF)
81 : }
82 :
83 25 : async function reread(): Promise<void> {
84 25 : const current = ++generation
85 25 : const flags = await readLocalCloudFlags()
86 25 : if (current === generation) apply(flags)
87 : }
88 :
89 : if (!isCloud) {
90 2355 : const authStore = useAuthStore()
91 2355 : const workspaceIdentity = useWorkspaceIdentity()
92 :
93 2355 : watch(
94 : [
95 : () => authStore.userId ?? null,
96 : () =>
97 : workspaceIdentity.activeWorkspaceId ??
98 : desktopHostUser.value?.workspaceId ??
99 : null
100 : ],
101 : ([userId]) => {
102 2367 : clear()
103 2367 : if (userId) void reread()
104 : },
105 : { immediate: true }
106 : )
107 :
108 2355 : watch(desktopHostUser, (user, previous) => {
109 1 : if (
110 : user &&
111 : user.id === previous?.id &&
112 : user.workspaceId === previous.workspaceId
113 : )
114 0 : void reread()
115 : })
116 :
117 2355 : watch(desktopHostTokenRecoveries, () => {
118 0 : if (authStore.userId) void reread()
119 : })
120 :
121 2355 : useIntervalFn(() => {
122 0 : if (authStore.userId) void reread()
123 : }, REREAD_INTERVAL_MS)
124 : }
125 :
126 2355 : return { enabled: computed(() => localBillingWebEnabled.value) }
127 : }
128 : )
|