Skip to content

Commit b8e5d1f

Browse files
refactor subscription composable (#6365)
Some refactors of subscription composable: - Swapped cloud subscription response shapes to `type` aliases to emphasize fixed API payloads. - Formatted renewal/end dates with the user’s locale instead of hard-coding `en-US`. - Reused the single `useFirebaseAuthActions()` instance and passed `fetchSubscriptionStatus` directly into `wrapWithErrorHandlingAsync` to trim duplicate wrappers. ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-6365-refactor-subscription-composable-29b6d73d365081b1b002d52abde8d195) by [Unito](https://www.unito.io)
1 parent bde5244 commit b8e5d1f

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed

src/platform/cloud/subscription/composables/useSubscription.ts

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import {
1414
useFirebaseAuthStore
1515
} from '@/stores/firebaseAuthStore'
1616

17-
interface CloudSubscriptionCheckoutResponse {
17+
type CloudSubscriptionCheckoutResponse = {
1818
checkout_url: string
1919
}
2020

21-
interface CloudSubscriptionStatusResponse {
21+
type CloudSubscriptionStatusResponse = {
2222
is_active: boolean
2323
subscription_id: string
2424
renewal_date: string | null
@@ -27,7 +27,7 @@ interface CloudSubscriptionStatusResponse {
2727

2828
const subscriptionStatus = ref<CloudSubscriptionStatusResponse | null>(null)
2929

30-
const isActiveSubscription = computed(() => {
30+
const isSubscribedOrIsNotCloud = computed(() => {
3131
if (!isCloud || !window.__CONFIG__?.subscription_required) return true
3232

3333
return subscriptionStatus.value?.is_active ?? false
@@ -36,12 +36,11 @@ const isActiveSubscription = computed(() => {
3636
let isWatchSetup = false
3737

3838
export function useSubscription() {
39-
const authActions = useFirebaseAuthActions()
39+
const { reportError, accessBillingPortal } = useFirebaseAuthActions()
4040
const dialogService = useDialogService()
4141

4242
const { getAuthHeader } = useFirebaseAuthStore()
4343
const { wrapWithErrorHandlingAsync } = useErrorHandling()
44-
const { reportError } = useFirebaseAuthActions()
4544

4645
const { isLoggedIn } = useCurrentUser()
4746

@@ -54,7 +53,7 @@ export function useSubscription() {
5453

5554
const renewalDate = new Date(subscriptionStatus.value.renewal_date)
5655

57-
return renewalDate.toLocaleDateString('en-US', {
56+
return renewalDate.toLocaleDateString(undefined, {
5857
month: 'short',
5958
day: 'numeric',
6059
year: 'numeric'
@@ -66,7 +65,7 @@ export function useSubscription() {
6665

6766
const endDate = new Date(subscriptionStatus.value.end_date)
6867

69-
return endDate.toLocaleDateString('en-US', {
68+
return endDate.toLocaleDateString(undefined, {
7069
month: 'short',
7170
day: 'numeric',
7271
year: 'numeric'
@@ -77,9 +76,10 @@ export function useSubscription() {
7776
() => `$${MONTHLY_SUBSCRIPTION_PRICE.toFixed(0)}`
7877
)
7978

80-
const fetchStatus = wrapWithErrorHandlingAsync(async () => {
81-
return await fetchSubscriptionStatus()
82-
}, reportError)
79+
const fetchStatus = wrapWithErrorHandlingAsync(
80+
fetchSubscriptionStatus,
81+
reportError
82+
)
8383

8484
const subscribe = wrapWithErrorHandlingAsync(async () => {
8585
const response = await initiateSubscriptionCheckout()
@@ -104,13 +104,13 @@ export function useSubscription() {
104104
}
105105

106106
const manageSubscription = async () => {
107-
await authActions.accessBillingPortal()
107+
await accessBillingPortal()
108108
}
109109

110110
const requireActiveSubscription = async (): Promise<void> => {
111111
await fetchSubscriptionStatus()
112112

113-
if (!isActiveSubscription.value) {
113+
if (!isSubscribedOrIsNotCloud.value) {
114114
showSubscriptionDialog()
115115
}
116116
}
@@ -124,46 +124,43 @@ export function useSubscription() {
124124
}
125125

126126
const handleInvoiceHistory = async () => {
127-
await authActions.accessBillingPortal()
127+
await accessBillingPortal()
128128
}
129129

130130
/**
131131
* Fetch the current cloud subscription status for the authenticated user
132132
* @returns Subscription status or null if no subscription exists
133133
*/
134-
const fetchSubscriptionStatus =
135-
async (): Promise<CloudSubscriptionStatusResponse | null> => {
136-
const authHeader = await getAuthHeader()
137-
if (!authHeader) {
138-
throw new FirebaseAuthStoreError(
139-
t('toastMessages.userNotAuthenticated')
140-
)
141-
}
134+
async function fetchSubscriptionStatus(): Promise<CloudSubscriptionStatusResponse | null> {
135+
const authHeader = await getAuthHeader()
136+
if (!authHeader) {
137+
throw new FirebaseAuthStoreError(t('toastMessages.userNotAuthenticated'))
138+
}
142139

143-
const response = await fetch(
144-
`${COMFY_API_BASE_URL}/customers/cloud-subscription-status`,
145-
{
146-
headers: {
147-
...authHeader,
148-
'Content-Type': 'application/json'
149-
}
140+
const response = await fetch(
141+
`${COMFY_API_BASE_URL}/customers/cloud-subscription-status`,
142+
{
143+
headers: {
144+
...authHeader,
145+
'Content-Type': 'application/json'
150146
}
151-
)
152-
153-
if (!response.ok) {
154-
const errorData = await response.json()
155-
throw new FirebaseAuthStoreError(
156-
t('toastMessages.failedToFetchSubscription', {
157-
error: errorData.message
158-
})
159-
)
160147
}
148+
)
161149

162-
const statusData = await response.json()
163-
subscriptionStatus.value = statusData
164-
return statusData
150+
if (!response.ok) {
151+
const errorData = await response.json()
152+
throw new FirebaseAuthStoreError(
153+
t('toastMessages.failedToFetchSubscription', {
154+
error: errorData.message
155+
})
156+
)
165157
}
166158

159+
const statusData = await response.json()
160+
subscriptionStatus.value = statusData
161+
return statusData
162+
}
163+
167164
if (!isWatchSetup) {
168165
isWatchSetup = true
169166
watch(
@@ -213,7 +210,7 @@ export function useSubscription() {
213210

214211
return {
215212
// State
216-
isActiveSubscription,
213+
isActiveSubscription: isSubscribedOrIsNotCloud,
217214
isCancelled,
218215
formattedRenewalDate,
219216
formattedEndDate,

0 commit comments

Comments
 (0)