@@ -85,11 +85,21 @@ export default function WorkspacePage() {
8585 const urlParams = new URLSearchParams ( window . location . search )
8686 const redirectWorkflowId = urlParams . get ( 'redirect_workflow' )
8787 const redirectTarget = urlParams . get ( 'redirect' )
88+ const rawReason = urlParams . get ( UPGRADE_REASON_PARAM )
89+
90+ // `?redirect=upgrade` is how a caller that cannot know a workspace id — a
91+ // self-hosted deployment, an email — reaches the plan picker. It has to
92+ // survive workspace creation too: a first-time visitor has no workspace to
93+ // resolve, and dropping the intent lands them on home with no explanation.
94+ const destinationFor = ( id : string ) =>
95+ redirectTarget === 'upgrade'
96+ ? buildUpgradeHref ( id , isUpgradeReason ( rawReason ) ? rawReason : undefined )
97+ : `/workspace/${ id } /home`
8898
8999 const { workspaces, lastActiveWorkspaceId, creationPolicy } = data
90100
91101 if ( workspaces . length === 0 ) {
92- handleNoWorkspaces ( router , creationPolicy )
102+ handleNoWorkspaces ( router , creationPolicy , destinationFor )
93103 return
94104 }
95105
@@ -105,21 +115,8 @@ export default function WorkspacePage() {
105115 return
106116 }
107117
108- // `?redirect=upgrade` is how a caller that cannot know a workspace id — a
109- // self-hosted deployment, an email — reaches the plan picker.
110- if ( redirectTarget === 'upgrade' ) {
111- const rawReason = urlParams . get ( UPGRADE_REASON_PARAM )
112- const href = buildUpgradeHref (
113- targetWorkspace . id ,
114- isUpgradeReason ( rawReason ) ? rawReason : undefined
115- )
116- logger . info ( `Redirecting to upgrade: ${ targetWorkspace . id } ` )
117- router . replace ( href )
118- return
119- }
120-
121118 logger . info ( `Redirecting to workspace: ${ targetWorkspace . id } ` )
122- router . replace ( `/workspace/ ${ targetWorkspace . id } /home` )
119+ router . replace ( destinationFor ( targetWorkspace . id ) )
123120 } , [ session , isSessionPending , sessionError , isWorkspacesLoading , workspacesError , data , router ] )
124121
125122 const failedToLoad =
@@ -193,7 +190,8 @@ async function handleWorkflowRedirect(
193190
194191async function handleNoWorkspaces (
195192 router : ReturnType < typeof useRouter > ,
196- creationPolicy : WorkspaceCreationPolicy | null
193+ creationPolicy : WorkspaceCreationPolicy | null ,
194+ destinationFor : ( workspaceId : string ) => string
197195) : Promise < void > {
198196 if ( creationPolicy && ! creationPolicy . canCreate ) {
199197 logger . warn ( 'No workspaces found and workspace creation is blocked' , {
@@ -212,7 +210,7 @@ async function handleNoWorkspaces(
212210 } )
213211 if ( data . workspace ?. id ) {
214212 logger . info ( `Created default workspace: ${ data . workspace . id } ` )
215- router . replace ( `/workspace/ ${ data . workspace . id } /home` )
213+ router . replace ( destinationFor ( data . workspace . id ) )
216214 return
217215 }
218216 logger . error ( 'Failed to create default workspace' )
0 commit comments