-
Notifications
You must be signed in to change notification settings - Fork 672
Expand file tree
/
Copy pathusePricingTableUrlLoader.ts
More file actions
219 lines (197 loc) · 6.25 KB
/
Copy pathusePricingTableUrlLoader.ts
File metadata and controls
219 lines (197 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { useRoute, useRouter } from 'vue-router'
import { useBillingContext } from '@/composables/billing/useBillingContext'
import {
isEnterprisePlanSlug,
isEnterpriseTier
} from '@/platform/cloud/subscription/constants/tierPricing'
import { useSubscriptionDialog } from '@/platform/cloud/subscription/composables/useSubscriptionDialog'
import {
clearPreservedQuery,
hydratePreservedQuery,
mergePreservedQueryIntoQuery
} from '@/platform/navigation/preservedQueryManager'
import { PRESERVED_QUERY_NAMESPACES } from '@/platform/navigation/preservedQueryNamespaces'
import type { TeamCreditStops } from '@/platform/workspace/api/workspaceApi'
import type { SubscriptionCheckoutSelection } from '@/platform/workspace/composables/useSubscriptionCheckout'
import { useWorkspaceUI } from '@/platform/workspace/composables/useWorkspaceUI'
const NAMESPACE = PRESERVED_QUERY_NAMESPACES.PRICING
function isCheckoutTier(
value: string
): value is Extract<
SubscriptionCheckoutSelection,
{ planMode: 'personal' }
>['tierKey'] {
return value === 'standard' || value === 'creator' || value === 'pro'
}
function isBillingCycle(
value: unknown
): value is SubscriptionCheckoutSelection['billingCycle'] {
return value === 'monthly' || value === 'yearly'
}
function getTeamCheckoutRequest(
pricing: string,
stop: unknown,
cycle: unknown
):
| {
stop: string
billingCycle: SubscriptionCheckoutSelection['billingCycle']
}
| undefined {
if (
pricing !== 'team' ||
typeof stop !== 'string' ||
!stop ||
!isBillingCycle(cycle)
) {
return
}
return { stop, billingCycle: cycle }
}
function getCheckoutSelection(
pricing: string,
stop: unknown,
cycle: unknown,
teamCreditStops: TeamCreditStops | null
): SubscriptionCheckoutSelection | undefined {
if (isCheckoutTier(pricing)) {
if (stop !== undefined || !isBillingCycle(cycle)) return
return {
planMode: 'personal',
tierKey: pricing,
billingCycle: cycle
}
}
const teamCheckoutRequest = getTeamCheckoutRequest(pricing, stop, cycle)
if (!teamCheckoutRequest) return
const catalogStop = teamCreditStops?.stops.find(
(candidate) => candidate.id === teamCheckoutRequest.stop
)
if (!catalogStop) return
return {
planMode: 'team',
stop: {
id: catalogStop.id,
credits: catalogStop.credits,
usd: catalogStop[teamCheckoutRequest.billingCycle].list_price_cents / 100,
discountedUsd:
catalogStop[teamCheckoutRequest.billingCycle].price_cents / 100
},
billingCycle: teamCheckoutRequest.billingCycle
}
}
/**
* Opens the pricing table from a `?pricing=` deep link, to send pilot users
* straight to subscribe. Values: `1` (default tab), `team`, `personal`, or a
* selected personal tier or Team credit stop with a billing cycle to open its
* confirmation.
*
* Gated to workspace owners (`canManageSubscription`); a member is a silent
* no-op with the param stripped. Survives the login redirect via the
* preserved-query system, like the invite URL loader.
*/
export function usePricingTableUrlLoader() {
const route = useRoute()
const router = useRouter()
const subscriptionDialog = useSubscriptionDialog()
const { subscription, teamCreditStops, fetchPlans } = useBillingContext()
const { permissions } = useWorkspaceUI()
/** Reads `?pricing=`, strips it, and opens the table when the gate allows. */
async function loadPricingTableFromUrl() {
hydratePreservedQuery(NAMESPACE)
const query =
mergePreservedQueryIntoQuery(NAMESPACE, route.query) ?? route.query
const param = query.pricing
if (
param === undefined &&
query.stop === undefined &&
query.cycle === undefined
) {
return
}
// Strip any present pricing param (even ineligible or malformed values) and
// write the clean URL in a single replace before any await, so a clean URL
// is guaranteed even if the replace rejects or the gate later denies.
const cleanQuery = { ...query }
delete cleanQuery.pricing
delete cleanQuery.stop
delete cleanQuery.cycle
router.replace({ query: cleanQuery }).catch((error) => {
console.warn(
'[usePricingTableUrlLoader] Failed to clean URL params:',
error
)
})
clearPreservedQuery(NAMESPACE)
// Only a non-empty string value opens the table; an empty/array param just
// gets stripped above.
if (typeof param !== 'string' || !param) return
if (!permissions.value.canManageSubscription) return
// Enterprise is sales-managed: the pricing table never opens for it, even
// from a deep link. The param was already stripped above.
if (
isEnterpriseTier(subscription.value?.tier) ||
isEnterprisePlanSlug(subscription.value?.planSlug)
) {
return
}
const teamCheckoutRequest = getTeamCheckoutRequest(
param,
query.stop,
query.cycle
)
if (teamCheckoutRequest && !teamCreditStops.value) {
try {
await fetchPlans()
} catch (error) {
console.error(
'[usePricingTableUrlLoader] Failed to load Team pricing plans:',
error
)
}
if (!permissions.value.canManageSubscription) return
if (!teamCreditStops.value) {
subscriptionDialog.showPricingTable({
reason: 'deep_link',
planMode: 'team'
})
return
}
}
const initialCheckout = getCheckoutSelection(
param,
query.stop,
query.cycle,
teamCreditStops.value
)
if (isCheckoutTier(param) && !initialCheckout) return
if (teamCheckoutRequest && !initialCheckout) {
subscriptionDialog.showPricingTable({
reason: 'deep_link',
planMode: 'team'
})
return
}
if (
!initialCheckout &&
(query.stop !== undefined || query.cycle !== undefined)
) {
return
}
const planMode = initialCheckout
? initialCheckout.planMode
: param === 'team' || param === 'personal'
? param
: undefined
if (!initialCheckout && !['1', 'team', 'personal'].includes(param)) return
if (!permissions.value.canManageSubscription) return
subscriptionDialog.showPricingTable({
reason: 'deep_link',
planMode,
initialCheckout
})
}
return {
loadPricingTableFromUrl
}
}