mirror of
https://github.com/stablyai/orca.git
synced 2026-11-17 03:15:01 +00:00
Merge pull request #747 from nwparker/cloud/plans-followups
feat(cloud): plans dialog passes the picked plan to the console; blocker copy after Compare plans
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
import { ipcMain } from 'electron'
|
||||
import {
|
||||
isOrcaCloudConsoleSection,
|
||||
parseOrcaCloudConsoleBillingHint,
|
||||
type OrcaCloudConsoleOpenResult
|
||||
} from '../../../shared/orca-cloud-console'
|
||||
import { openOrcaCloudConsole } from '../../orca-profiles/cloud-console/cloud-console-link'
|
||||
@@ -18,7 +19,14 @@ export function registerOrcaCloudConsoleHandlers(): void {
|
||||
if (!isOrcaCloudConsoleSection(section)) {
|
||||
return { status: 'failed', error: 'invalid_orca_cloud_console_section' }
|
||||
}
|
||||
return openOrcaCloudConsole(getProfileUserDataPath(), section)
|
||||
const billingHint =
|
||||
section === 'billing' &&
|
||||
typeof rawArgs === 'object' &&
|
||||
rawArgs !== null &&
|
||||
'billingHint' in rawArgs
|
||||
? parseOrcaCloudConsoleBillingHint(rawArgs.billingHint)
|
||||
: null
|
||||
return openOrcaCloudConsole(getProfileUserDataPath(), section, { billingHint })
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,11 @@ import { describe, expect, it, vi } from 'vitest'
|
||||
vi.mock('electron', () => ({ app: { isPackaged: false }, shell: { openExternal: vi.fn() } }))
|
||||
|
||||
import { CLOUD_CONSOLE_TICKET_PATH, cloudConsoleOrigin } from './cloud-console-config'
|
||||
import { acceptedTicketUrl, requestCloudConsoleTicket } from './cloud-console-link'
|
||||
import {
|
||||
acceptedTicketUrl,
|
||||
cloudConsoleTicketBody,
|
||||
requestCloudConsoleTicket
|
||||
} from './cloud-console-link'
|
||||
|
||||
const config = { apiBaseUrl: 'https://login.onorca.dev' }
|
||||
|
||||
@@ -59,6 +63,23 @@ describe('console ticket', () => {
|
||||
expect(JSON.parse(String(seen[0]?.init?.body))).toEqual({ page: 'billing' })
|
||||
})
|
||||
|
||||
it('sends the billing hint only to an auth that advertises it, and only for billing', () => {
|
||||
const hint = { plan: 'team', interval: 'year' as const }
|
||||
const advertised = { capabilities: { flags: { 'console.billing_hint': true } } }
|
||||
expect(
|
||||
cloudConsoleTicketBody(advertised, { section: 'billing', orgId: 'org_1', billingHint: hint })
|
||||
).toEqual({ page: 'billing', orgId: 'org_1', plan: 'team', interval: 'year' })
|
||||
expect(
|
||||
cloudConsoleTicketBody(
|
||||
{ capabilities: { flags: {} } },
|
||||
{ section: 'billing', orgId: 'org_1', billingHint: hint }
|
||||
)
|
||||
).toEqual({ page: 'billing', orgId: 'org_1' })
|
||||
expect(
|
||||
cloudConsoleTicketBody(advertised, { section: 'team', orgId: 'org_1', billingHint: hint })
|
||||
).toEqual({ page: 'team', orgId: 'org_1' })
|
||||
})
|
||||
|
||||
it('throws on a refusal so the console opens without a ticket', async () => {
|
||||
await expect(
|
||||
requestCloudConsoleTicket(
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
// URL from auth, so the browser lands signed in; when that fails for any reason the console opens
|
||||
// without one and the browser asks the person to sign in. The ticket is never logged or kept.
|
||||
import { app, shell } from 'electron'
|
||||
import type {
|
||||
OrcaCloudConsoleOpenResult,
|
||||
OrcaCloudConsoleSection
|
||||
import {
|
||||
ORCA_CLOUD_CONSOLE_BILLING_HINT_CAPABILITY,
|
||||
type OrcaCloudConsoleBillingHint,
|
||||
type OrcaCloudConsoleOpenResult,
|
||||
type OrcaCloudConsoleSection
|
||||
} from '../../../shared/orca-cloud-console'
|
||||
import {
|
||||
getOrcaCloudAuthConfig,
|
||||
@@ -45,11 +47,40 @@ export function acceptedTicketUrl(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The ticket request body. The billing hint goes only to an auth that advertises it: an older
|
||||
* ticket route takes a strict body and would refuse the extra fields, losing the signed-in landing.
|
||||
*/
|
||||
export function cloudConsoleTicketBody(
|
||||
session: { capabilities?: { flags: Record<string, boolean> } },
|
||||
args: {
|
||||
section: OrcaCloudConsoleSection
|
||||
orgId: string | null
|
||||
billingHint?: OrcaCloudConsoleBillingHint | null
|
||||
}
|
||||
): Record<string, string> {
|
||||
const hint =
|
||||
args.section === 'billing' &&
|
||||
args.billingHint &&
|
||||
session.capabilities?.flags[ORCA_CLOUD_CONSOLE_BILLING_HINT_CAPABILITY] === true
|
||||
? args.billingHint
|
||||
: null
|
||||
return {
|
||||
page: args.section,
|
||||
...(args.orgId ? { orgId: args.orgId } : {}),
|
||||
...(hint ? { plan: hint.plan, interval: hint.interval } : {})
|
||||
}
|
||||
}
|
||||
|
||||
/** Asks auth for a single-use console ticket; answers its `url` unchecked (see acceptedTicketUrl). */
|
||||
export async function requestCloudConsoleTicket(
|
||||
config: Pick<OrcaCloudAuthConfig, 'apiBaseUrl'>,
|
||||
session: Pick<OrcaCloudSession, 'accessToken'>,
|
||||
args: { section: OrcaCloudConsoleSection; orgId: string | null },
|
||||
session: Pick<OrcaCloudSession, 'accessToken'> & Partial<Pick<OrcaCloudSession, 'capabilities'>>,
|
||||
args: {
|
||||
section: OrcaCloudConsoleSection
|
||||
orgId: string | null
|
||||
billingHint?: OrcaCloudConsoleBillingHint | null
|
||||
},
|
||||
fetchImpl: typeof fetch = fetch
|
||||
): Promise<unknown> {
|
||||
const response = await fetchImpl(cloudConsoleTicketUrl(config), {
|
||||
@@ -58,7 +89,7 @@ export async function requestCloudConsoleTicket(
|
||||
'content-type': 'application/json',
|
||||
authorization: `Bearer ${session.accessToken}`
|
||||
},
|
||||
body: JSON.stringify({ page: args.section, ...(args.orgId ? { orgId: args.orgId } : {}) }),
|
||||
body: JSON.stringify(cloudConsoleTicketBody(session, args)),
|
||||
// Never follow a redirect with the access token attached.
|
||||
redirect: 'error',
|
||||
signal: AbortSignal.timeout(CLOUD_CONSOLE_TICKET_TIMEOUT_MS)
|
||||
@@ -73,7 +104,8 @@ export async function requestCloudConsoleTicket(
|
||||
async function mintTicketUrl(
|
||||
config: OrcaCloudAuthConfig,
|
||||
userDataPath: string,
|
||||
section: OrcaCloudConsoleSection
|
||||
section: OrcaCloudConsoleSection,
|
||||
billingHint: OrcaCloudConsoleBillingHint | null
|
||||
): Promise<unknown> {
|
||||
if (isOrcaCloudDevAuthEnabled()) {
|
||||
return null
|
||||
@@ -84,7 +116,7 @@ async function mintTicketUrl(
|
||||
}
|
||||
const orgId = active.profile.cloud.activeOrgId ?? null
|
||||
const result = await runOrgMemberCall(config, active, userDataPath, (session) =>
|
||||
requestCloudConsoleTicket(config, session, { section, orgId })
|
||||
requestCloudConsoleTicket(config, session, { section, orgId, billingHint })
|
||||
)
|
||||
return result.status === 'ok' ? result.value : null
|
||||
}
|
||||
@@ -92,8 +124,12 @@ async function mintTicketUrl(
|
||||
export async function openOrcaCloudConsole(
|
||||
userDataPath: string,
|
||||
section: OrcaCloudConsoleSection,
|
||||
openExternal: (url: string) => Promise<void> = (url) => shell.openExternal(url)
|
||||
options: {
|
||||
billingHint?: OrcaCloudConsoleBillingHint | null
|
||||
openExternal?: (url: string) => Promise<void>
|
||||
} = {}
|
||||
): Promise<OrcaCloudConsoleOpenResult> {
|
||||
const openExternal = options.openExternal ?? ((url: string) => shell.openExternal(url))
|
||||
const configState = getOrcaCloudAuthConfig()
|
||||
if (!configState.configured) {
|
||||
return { status: 'unconfigured' }
|
||||
@@ -101,7 +137,9 @@ export async function openOrcaCloudConsole(
|
||||
const { config } = configState
|
||||
const origin = cloudConsoleOrigin(config, process.env, !app.isPackaged)
|
||||
const ticketUrl = acceptedTicketUrl(
|
||||
await mintTicketUrl(config, userDataPath, section).catch(() => null),
|
||||
await mintTicketUrl(config, userDataPath, section, options.billingHint ?? null).catch(
|
||||
() => null
|
||||
),
|
||||
[origin, new URL(config.apiBaseUrl).origin]
|
||||
)
|
||||
try {
|
||||
|
||||
@@ -29,9 +29,9 @@ export function PlansDialogHost(): React.JSX.Element {
|
||||
catalog={catalog.result}
|
||||
canManage={canManage}
|
||||
opening={opening}
|
||||
onContinue={() => {
|
||||
onContinue={(hint) => {
|
||||
setOpening(true)
|
||||
void openCloudConsole('billing').finally(() => setOpening(false))
|
||||
void openCloudConsole('billing', hint).finally(() => setOpening(false))
|
||||
}}
|
||||
onContactSales={(url) => void window.api.shell.openUrl(url)}
|
||||
onRetry={() => {
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
type OrcaBillingStatusResult
|
||||
} from '../../../../../shared/orca-billing'
|
||||
import type { OrcaPlanCatalogResult } from '../../../../../shared/orca-billing-plans'
|
||||
import type { OrcaCloudConsoleBillingHint } from '../../../../../shared/orca-cloud-console'
|
||||
import { PaymentIssueNotice } from '../PaymentIssueNotice'
|
||||
import { isActivePlan } from '../plan-billing-format'
|
||||
import {
|
||||
@@ -35,7 +36,8 @@ export type PlansDialogViewProps = {
|
||||
canManage: boolean
|
||||
/** The console is being opened (a ticket is minted first). */
|
||||
opening: boolean
|
||||
onContinue: () => void
|
||||
/** `hint`: the plan and period picked here, for the console to preselect (never to buy). */
|
||||
onContinue: (hint?: OrcaCloudConsoleBillingHint) => void
|
||||
onContactSales: (url: string) => void
|
||||
onRetry: () => void
|
||||
}
|
||||
@@ -129,7 +131,7 @@ function PlansBody(props: PlansDialogViewProps): React.JSX.Element {
|
||||
<PaymentIssueNotice
|
||||
entitlement={{ ...entitlement, canManageBilling: props.canManage }}
|
||||
busy={props.opening}
|
||||
onUpdatePayment={props.onContinue}
|
||||
onUpdatePayment={() => props.onContinue()}
|
||||
/>
|
||||
<CurrentPlanStrip entitlement={entitlement} />
|
||||
</>
|
||||
@@ -161,7 +163,7 @@ function PlansBody(props: PlansDialogViewProps): React.JSX.Element {
|
||||
<Button
|
||||
type="button"
|
||||
aria-disabled={props.opening || undefined}
|
||||
onClick={props.opening ? undefined : props.onContinue}
|
||||
onClick={props.opening ? undefined : () => props.onContinue()}
|
||||
>
|
||||
{t('auto.components.billing.plans.continueInBrowser', 'Continue in browser')}
|
||||
<ExternalLink />
|
||||
@@ -201,7 +203,13 @@ function PlansBody(props: PlansDialogViewProps): React.JSX.Element {
|
||||
seats={subscribed && entitlement ? entitlement.seats : null}
|
||||
mode={mode}
|
||||
opening={props.opening}
|
||||
onContinue={mode === 'member' ? null : props.onContinue}
|
||||
onContinue={
|
||||
mode === 'member'
|
||||
? null
|
||||
: mode === 'choose'
|
||||
? () => props.onContinue({ plan: team.planId, interval })
|
||||
: () => props.onContinue()
|
||||
}
|
||||
/>
|
||||
{enterprise ? (
|
||||
<EnterprisePlanCard enterprise={enterprise} onContactSales={props.onContactSales} />
|
||||
|
||||
@@ -18,8 +18,9 @@ export type BillingBlockerPhase =
|
||||
| { kind: 'idle' }
|
||||
| { kind: 'saving' }
|
||||
| { kind: 'error'; message: string }
|
||||
/** A fix in the browser (portal): re-check on return, or with Check again. */
|
||||
| { kind: 'browser'; checking: boolean; stillBlocked: boolean }
|
||||
/** A fix elsewhere: the plans dialog (then the console) or the payment portal. Re-check on
|
||||
* return, or with Check again. */
|
||||
| { kind: 'browser'; checking: boolean; stillBlocked: boolean; via: 'plans' | 'portal' }
|
||||
|
||||
/** Why there is no plan, in billing's words: only a plan that existed has "ended" (EP-11). */
|
||||
function planBlockBody(
|
||||
@@ -175,24 +176,41 @@ export function VmBuildBillingBlockerView({
|
||||
inBrowser ? (
|
||||
<div className="space-y-2">
|
||||
<p className="text-muted-foreground">
|
||||
{phase.stillBlocked
|
||||
? t(
|
||||
'worktreeCreation.billingBlock.browserStill',
|
||||
'Not updated yet. Finish in your browser, then check again.'
|
||||
)
|
||||
: t('worktreeCreation.billingBlock.browser', 'Finish in your browser.')}
|
||||
{phase.via === 'plans'
|
||||
? phase.stillBlocked
|
||||
? t(
|
||||
'worktreeCreation.billingBlock.plansStill',
|
||||
'No plan yet. Choose one, then check again.'
|
||||
)
|
||||
: t(
|
||||
'worktreeCreation.billingBlock.plans',
|
||||
'Choose a plan, then check again. Orca checks again when you come back.'
|
||||
)
|
||||
: phase.stillBlocked
|
||||
? t(
|
||||
'worktreeCreation.billingBlock.browserStill',
|
||||
'Not updated yet. Finish in your browser, then check again.'
|
||||
)
|
||||
: t('worktreeCreation.billingBlock.browser', 'Finish in your browser.')}
|
||||
</p>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
aria-busy={phase.checking || undefined}
|
||||
aria-disabled={phase.checking || undefined}
|
||||
onClick={phase.checking ? undefined : onCheckAgain}
|
||||
>
|
||||
{phase.checking ? <Loader2 className="animate-spin" /> : null}
|
||||
{t('worktreeCreation.billingBlock.checkAgain', 'Check again')}
|
||||
</Button>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{phase.via === 'plans' ? (
|
||||
<Button type="button" size="sm" onClick={onComparePlans}>
|
||||
{t('worktreeCreation.billingBlock.seePlans', 'See plans')}
|
||||
</Button>
|
||||
) : null}
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
aria-busy={phase.checking || undefined}
|
||||
aria-disabled={phase.checking || undefined}
|
||||
onClick={phase.checking ? undefined : onCheckAgain}
|
||||
>
|
||||
{phase.checking ? <Loader2 className="animate-spin" /> : null}
|
||||
{t('worktreeCreation.billingBlock.checkAgain', 'Check again')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
) : kind === 'spendCap' ? (
|
||||
<form
|
||||
@@ -350,14 +368,26 @@ export function VmBuildBillingBlocker({
|
||||
}
|
||||
|
||||
const checkBilling = React.useCallback(async (): Promise<void> => {
|
||||
setPhase({ kind: 'browser', checking: true, stillBlocked: false })
|
||||
const via = (current: BillingBlockerPhase): 'plans' | 'portal' =>
|
||||
current.kind === 'browser' ? current.via : 'portal'
|
||||
setPhase((current) => ({
|
||||
kind: 'browser',
|
||||
checking: true,
|
||||
stillBlocked: false,
|
||||
via: via(current)
|
||||
}))
|
||||
const status = await window.api.orcaProfiles.billingStatus().catch(() => null)
|
||||
// The service's own verdict: a fixed payment makes new sandboxes startable again.
|
||||
if (status?.status === 'ok' && status.entitlement.canStartSandbox) {
|
||||
onContinue()
|
||||
return
|
||||
}
|
||||
setPhase({ kind: 'browser', checking: false, stillBlocked: true })
|
||||
setPhase((current) => ({
|
||||
kind: 'browser',
|
||||
checking: false,
|
||||
stillBlocked: true,
|
||||
via: via(current)
|
||||
}))
|
||||
}, [onContinue])
|
||||
|
||||
// Back from the browser: check once; if billing is fine the create continues on its own.
|
||||
@@ -385,11 +415,11 @@ export function VmBuildBillingBlocker({
|
||||
onTurnOnOverage={() => void saveOverage(defaultCapCents)}
|
||||
onRaiseCap={(dollars) => void saveOverage(dollars * 100)}
|
||||
onComparePlans={() => {
|
||||
setPhase({ kind: 'browser', checking: false, stillBlocked: false })
|
||||
setPhase({ kind: 'browser', checking: false, stillBlocked: false, via: 'plans' })
|
||||
openPlansDialog()
|
||||
}}
|
||||
onUpdatePayment={() => {
|
||||
setPhase({ kind: 'browser', checking: false, stillBlocked: false })
|
||||
setPhase({ kind: 'browser', checking: false, stillBlocked: false, via: 'portal' })
|
||||
void window.api.orcaProfiles.billingOpenPortal().catch(() => {})
|
||||
}}
|
||||
onCheckAgain={() => void checkBilling()}
|
||||
|
||||
@@ -29171,7 +29171,10 @@
|
||||
"checkAgain": "Check again",
|
||||
"raiseCapAndContinue": "Raise spend cap and continue",
|
||||
"backToDraft": "Back to draft",
|
||||
"saveFailed": "Couldn’t save that. Try again, or change it in Plan & billing."
|
||||
"saveFailed": "Couldn’t save that. Try again, or change it in Plan & billing.",
|
||||
"plansStill": "No plan yet. Choose one, then check again.",
|
||||
"plans": "Choose a plan, then check again. Orca checks again when you come back.",
|
||||
"seePlans": "See plans"
|
||||
},
|
||||
"cloneAccess": {
|
||||
"reachableTitle": "Couldn’t clone {{repo}}",
|
||||
|
||||
@@ -3,18 +3,27 @@
|
||||
// code the browser asks the person to sign in, so a failed request still opens the page.
|
||||
import { toast } from 'sonner'
|
||||
import { translate } from '@/i18n/i18n'
|
||||
import type { OrcaCloudConsoleSection } from '../../../../shared/orca-cloud-console'
|
||||
import type {
|
||||
OrcaCloudConsoleBillingHint,
|
||||
OrcaCloudConsoleSection
|
||||
} from '../../../../shared/orca-cloud-console'
|
||||
|
||||
let opening: OrcaCloudConsoleSection | null = null
|
||||
|
||||
export async function openCloudConsole(section: OrcaCloudConsoleSection): Promise<void> {
|
||||
/** `billingHint`: the plan and period picked in the plans dialog, for the billing page to preselect. */
|
||||
export async function openCloudConsole(
|
||||
section: OrcaCloudConsoleSection,
|
||||
billingHint?: OrcaCloudConsoleBillingHint
|
||||
): Promise<void> {
|
||||
// A double click mints one code, not two.
|
||||
if (opening === section) {
|
||||
return
|
||||
}
|
||||
opening = section
|
||||
try {
|
||||
const result = await window.api.orcaProfiles.openCloudConsole({ section })
|
||||
const result = await window.api.orcaProfiles.openCloudConsole(
|
||||
billingHint ? { section, billingHint } : { section }
|
||||
)
|
||||
if (result.status !== 'opened') {
|
||||
toast.error(
|
||||
translate('settings.cloudConsole.openFailed', 'Couldn’t open the browser. Try again.')
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// only links to it (Settings rows, account menu, "Fix in browser" buttons); main opens the page in
|
||||
// the system browser through a single-use ticket URL from auth.
|
||||
|
||||
import { ORCA_BILLING_INTERVALS, type OrcaBillingInterval } from './orca-billing'
|
||||
|
||||
/** Console pages the desktop can ask auth for (the ticket's `page`). */
|
||||
export const ORCA_CLOUD_CONSOLE_SECTIONS = [
|
||||
'home',
|
||||
@@ -12,7 +14,39 @@ export const ORCA_CLOUD_CONSOLE_SECTIONS = [
|
||||
] as const
|
||||
export type OrcaCloudConsoleSection = (typeof ORCA_CLOUD_CONSOLE_SECTIONS)[number]
|
||||
|
||||
export type OrcaCloudConsoleOpenArgs = { section: OrcaCloudConsoleSection }
|
||||
/**
|
||||
* The plan and billing period picked in the desktop's plans dialog, for the console's billing page
|
||||
* to preselect (a hint: the page never buys on its own). Sent only to an auth that advertises
|
||||
* ORCA_CLOUD_CONSOLE_BILLING_HINT_CAPABILITY, so an older one never sees fields it would refuse.
|
||||
*/
|
||||
export type OrcaCloudConsoleBillingHint = { plan: string; interval: OrcaBillingInterval }
|
||||
|
||||
/** Desktop capability flag auth sets once its console ticket accepts `plan` and `interval`. */
|
||||
export const ORCA_CLOUD_CONSOLE_BILLING_HINT_CAPABILITY = 'console.billing_hint'
|
||||
|
||||
export type OrcaCloudConsoleOpenArgs = {
|
||||
section: OrcaCloudConsoleSection
|
||||
/** Billing page only; ignored for every other section. */
|
||||
billingHint?: OrcaCloudConsoleBillingHint
|
||||
}
|
||||
|
||||
const PLAN_ID = /^[a-z][a-z0-9_]{0,39}$/
|
||||
|
||||
/** The hint as main accepts it from the renderer, or null for anything else. */
|
||||
export function parseOrcaCloudConsoleBillingHint(
|
||||
value: unknown
|
||||
): OrcaCloudConsoleBillingHint | null {
|
||||
if (typeof value !== 'object' || value === null) {
|
||||
return null
|
||||
}
|
||||
const plan = 'plan' in value ? value.plan : undefined
|
||||
const interval = 'interval' in value ? value.interval : undefined
|
||||
if (typeof plan !== 'string' || !PLAN_ID.test(plan)) {
|
||||
return null
|
||||
}
|
||||
const known = ORCA_BILLING_INTERVALS.find((entry) => entry === interval)
|
||||
return known ? { plan, interval: known } : null
|
||||
}
|
||||
|
||||
export type OrcaCloudConsoleOpenResult =
|
||||
/** `signedIn: false`: no ticket, so the browser asks the person to sign in. */
|
||||
|
||||
Reference in New Issue
Block a user