Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(cloud): stabilize billing hydration and membership recovery

Timothy Yin04f046ef0970
diff --git a/apps/cloud/app/composables/useWorkspaceBilling.ts b/apps/cloud/app/composables/useWorkspaceBilling.ts
index e0660c19..b75a58fb 100644
--- a/apps/cloud/app/composables/useWorkspaceBilling.ts
+++ b/apps/cloud/app/composables/useWorkspaceBilling.ts
@@ -2,62 +2,70 @@ export function useWorkspaceBilling(adminOnly = false) {
const route = useRoute(),
user = usePortalIdentity(),
fetcher = useRequestFetch(),
- listing = useFetch<{
- workspaces: { id: number; name: string; role: string }[]
- }>('/api/billing/workspaces', {
- key: 'billing-workspaces-' + user.value?.id,
- })
- const { data: list, error: listError } = listing
- const workspaces = computed(() =>
- (list.value?.workspaces ?? []).filter(
- (w) => !adminOnly || w.role === 'admin',
- ),
- )
- const selected = ref(Number(route.query.workspaceId) || 0)
- watch(
- workspaces,
- (available) => {
- if (!list.value) return
- if (!available.some((w) => w.id === selected.value))
- selected.value = available[0]?.id ?? 0
- },
- { immediate: true, flush: 'sync' },
- )
- // Register every composable synchronously while the page owns the Vue scope.
- // Waiting for the workspace list inside the handler also covers SSR cold loads.
+ requested = Number(route.query.workspaceId) || 0,
+ selected = ref(requested)
+ type Workspace = { id: number; name: string; role: string }
+ // A stable payload key covers both steps: the selected workspace is resolved
+ // by the server before hydration, rather than changing the async-data key mid-fetch.
const billing = useAsyncData(
- computed(
- () => 'workspace-billing-' + user.value?.id + '-' + selected.value,
- ),
+ `workspace-billing-${user.value?.id}-${adminOnly ? 'admin' : 'all'}-${requested}`,
async () => {
- await listing
- return selected.value
- ? fetcher<any>('/api/billing/plan', {
- query: { workspaceId: selected.value },
+ const list = await fetcher<{ workspaces: Workspace[] }>(
+ '/api/billing/workspaces',
+ )
+ const workspaces = list.workspaces.filter(
+ (w) => !adminOnly || w.role === 'admin',
+ )
+ const id = workspaces.some((w) => w.id === selected.value)
+ ? selected.value
+ : (workspaces[0]?.id ?? 0)
+ const plan = id
+ ? await fetcher<any>('/api/billing/plan', {
+ query: { workspaceId: id },
})
: null
+ return {
+ workspaces,
+ membershipCount: list.workspaces.length,
+ selected: id,
+ plan,
+ }
},
)
+ watch(
+ billing.data,
+ (value) => {
+ if (value) selected.value = value.selected
+ },
+ { immediate: true, flush: 'sync' },
+ )
+ watch(selected, (id) => {
+ if (id !== billing.data.value?.selected) void billing.refresh()
+ })
onMounted(async () => {
- await listing
- if (!workspaces.value.length && !listError.value) {
+ await billing
+ if (billing.data.value?.membershipCount === 0 && !billing.error.value) {
try {
- list.value = await $fetch('/api/billing/workspaces', { method: 'POST' })
+ await $fetch('/api/billing/workspaces', { method: 'POST' })
+ await billing.refresh()
} catch {
/* Do not restore revoked memberships. */
}
}
})
return {
- workspaces,
+ workspaces: computed(() => billing.data.value?.workspaces ?? []),
selected,
- ...billing,
- listError,
- retry: async () => {
- await listing.refresh()
- await nextTick()
- if (!listError.value) await billing.refresh()
- },
- ready: Promise.all([listing, billing]),
+ data: computed(() =>
+ billing.data.value?.selected === selected.value
+ ? billing.data.value.plan
+ : null,
+ ),
+ error: billing.error,
+ listError: billing.error,
+ status: billing.status,
+ refresh: billing.refresh,
+ retry: billing.refresh,
+ ready: billing,
}
}