11import { computed , ref , watch } from 'vue'
2+ import { createSharedComposable } from '@vueuse/core'
23
34import { useCurrentUser } from '@/composables/auth/useCurrentUser'
45import { useFirebaseAuthActions } from '@/composables/auth/useFirebaseAuthActions'
@@ -25,17 +26,14 @@ type CloudSubscriptionStatusResponse = {
2526 end_date ?: string | null
2627}
2728
28- const subscriptionStatus = ref < CloudSubscriptionStatusResponse | null > ( null )
29+ function useSubscriptionInternal ( ) {
30+ const subscriptionStatus = ref < CloudSubscriptionStatusResponse | null > ( null )
2931
30- const isSubscribedOrIsNotCloud = computed ( ( ) => {
31- if ( ! isCloud || ! window . __CONFIG__ ?. subscription_required ) return true
32+ const isSubscribedOrIsNotCloud = computed ( ( ) => {
33+ if ( ! isCloud || ! window . __CONFIG__ ?. subscription_required ) return true
3234
33- return subscriptionStatus . value ?. is_active ?? false
34- } )
35-
36- let isWatchSetup = false
37-
38- export function useSubscription ( ) {
35+ return subscriptionStatus . value ?. is_active ?? false
36+ } )
3937 const { reportError, accessBillingPortal } = useFirebaseAuthActions ( )
4038 const dialogService = useDialogService ( )
4139
@@ -161,20 +159,17 @@ export function useSubscription() {
161159 return statusData
162160 }
163161
164- if ( ! isWatchSetup ) {
165- isWatchSetup = true
166- watch (
167- ( ) => isLoggedIn . value ,
168- async ( loggedIn ) => {
169- if ( loggedIn ) {
170- await fetchSubscriptionStatus ( )
171- } else {
172- subscriptionStatus . value = null
173- }
174- } ,
175- { immediate : true }
176- )
177- }
162+ watch (
163+ ( ) => isLoggedIn . value ,
164+ async ( loggedIn ) => {
165+ if ( loggedIn ) {
166+ await fetchSubscriptionStatus ( )
167+ } else {
168+ subscriptionStatus . value = null
169+ }
170+ } ,
171+ { immediate : true }
172+ )
178173
179174 const initiateSubscriptionCheckout =
180175 async ( ) : Promise < CloudSubscriptionCheckoutResponse > => {
@@ -227,3 +222,5 @@ export function useSubscription() {
227222 handleInvoiceHistory
228223 }
229224}
225+
226+ export const useSubscription = createSharedComposable ( useSubscriptionInternal )
0 commit comments