Line data Source code
1 : import {
2 : createSharedComposable,
3 : useAsyncState,
4 : useDocumentVisibility
5 : } from '@vueuse/core'
6 : import { computed, watch } from 'vue'
7 :
8 : import { deriveAutoTopUpCard } from '@comfyorg/account-core/billing'
9 : import type { AutoTopUpCardState } from '@comfyorg/account-core/billing'
10 : import { reportError } from '@comfyorg/telemetry/reportError'
11 : import { billingApi } from '@/platform/billing/api/billingApi'
12 : import type { AutoReloadSettings } from '@comfyorg/billing-core'
13 : import { useWorkspaceIdentity } from '@/platform/workspace/ports/workspaceIdentity'
14 : import { useApiKeyAuthStore } from '@/stores/apiKeyAuthStore'
15 : import { useAuthStore } from '@/stores/authStore'
16 :
17 : interface ScopedRead {
18 : scope: string
19 : settings: AutoReloadSettings | null
20 : }
21 :
22 9 : function useBillingAutoTopUpCardInternal() {
23 9 : const authStore = useAuthStore()
24 9 : const apiKeyAuthStore = useApiKeyAuthStore()
25 9 : const workspaceStore = useWorkspaceIdentity()
26 :
27 9 : const scope = computed(() => {
28 9 : const actorId = authStore.userId ?? apiKeyAuthStore.currentUser?.id
29 9 : const workspaceId = workspaceStore.activeWorkspaceId
30 9 : return actorId && workspaceId ? `${actorId}:${workspaceId}` : null
31 : })
32 :
33 9 : let requested = false
34 9 : let refreshSerial = 0
35 9 : let inFlightScope: string | null = null
36 9 : let committed: ScopedRead | null = null
37 :
38 2 : async function readCurrentScope(): Promise<ScopedRead | null> {
39 2 : const requestScope = scope.value
40 2 : if (requestScope === null) return null
41 2 : const serial = ++refreshSerial
42 2 : inFlightScope = requestScope
43 2 : try {
44 2 : const next = {
45 : scope: requestScope,
46 : settings: await billingApi.getBillingAutoReload()
47 : }
48 2 : if (serial !== refreshSerial) return committed
49 2 : committed = next
50 2 : return next
51 : } catch (error) {
52 0 : reportError(error, {
53 : errorType: 'auto_top_up_read_failed',
54 : surface: 'billing'
55 : })
56 0 : if (serial !== refreshSerial) return committed
57 0 : if (committed?.scope === requestScope && committed.settings) {
58 0 : return committed
59 : }
60 0 : const failed = { scope: requestScope, settings: null }
61 0 : committed = failed
62 0 : return failed
63 : } finally {
64 2 : if (inFlightScope === requestScope) inFlightScope = null
65 : }
66 : }
67 :
68 9 : const read = useAsyncState(readCurrentScope, null, {
69 : immediate: false,
70 : resetOnExecute: false
71 : })
72 :
73 9 : const answer = computed(() => {
74 11 : const current = read.state.value
75 11 : return current?.scope === scope.value ? current : null
76 : })
77 :
78 9 : const state = computed<AutoTopUpCardState>(() =>
79 : deriveAutoTopUpCard({
80 : isLoading: read.isLoading.value,
81 : settings: answer.value?.settings ?? null
82 : })
83 : )
84 :
85 9 : const isReady = computed(() => answer.value !== null && !read.isLoading.value)
86 :
87 4 : function refresh() {
88 4 : if (scope.value === null || inFlightScope === scope.value) return
89 2 : void read.execute()
90 : }
91 :
92 4 : function ensureRead() {
93 4 : requested = true
94 4 : if (scope.value === null) return
95 4 : if (answer.value !== null) return
96 4 : refresh()
97 : }
98 :
99 0 : async function retry() {
100 0 : await read.execute()
101 : }
102 :
103 9 : watch(scope, () => {
104 0 : if (requested) ensureRead()
105 : })
106 :
107 9 : watch(useDocumentVisibility(), (visibility) => {
108 0 : if (visibility === 'visible' && requested) refresh()
109 : })
110 :
111 9 : return { scope, state, isReady, ensureRead, retry }
112 : }
113 :
114 2356 : export const useBillingAutoTopUpCard = createSharedComposable(
115 : useBillingAutoTopUpCardInternal
116 : )
|