@@ -34,10 +34,10 @@ export default async function TerminalPage({
3434 searchParams,
3535} : TerminalPageProps ) {
3636 const { command = '' , sandboxId, template } = await searchParams
37- const terminalTemplate = normalizeTerminalTemplate ( template )
3837 const terminalSandboxId = normalizeTerminalSandboxId ( sandboxId )
38+ const requestedTemplate = normalizeTerminalTemplate ( template )
3939
40- if ( ! terminalTemplate ) {
40+ if ( ! terminalSandboxId && ! requestedTemplate ) {
4141 return < TerminalUnavailable message = "The terminal template is invalid." />
4242 }
4343
@@ -51,7 +51,7 @@ export default async function TerminalPage({
5151 return (
5252 < TerminalSignIn
5353 sandboxId = { terminalSandboxId }
54- template = { terminalTemplate }
54+ template = { requestedTemplate ?? 'base' }
5555 />
5656 )
5757 }
@@ -69,14 +69,18 @@ export default async function TerminalPage({
6969 authContext . user . id ,
7070 authContext . accessToken
7171 )
72- const team = terminalSandboxId
73- ? await resolveTerminalSandboxTeam ( {
72+ const terminalSandbox = terminalSandboxId
73+ ? await resolveTerminalSandbox ( {
7474 accessToken : authContext . accessToken ,
7575 preferredTeamId : resolvedTeam ?. id ,
7676 sandboxId : terminalSandboxId ,
7777 teams : teamsResult . data ,
7878 } )
79+ : null
80+ const team = terminalSandbox
81+ ? terminalSandbox . team
7982 : teamsResult . data . find ( ( candidate ) => candidate . id === resolvedTeam ?. id )
83+ const terminalTemplate = terminalSandbox ?. template ?? requestedTemplate
8084
8185 if ( ! team ) {
8286 return (
@@ -95,7 +99,7 @@ export default async function TerminalPage({
9599 : await isTerminalTemplateAvailable ( {
96100 accessToken : authContext . accessToken ,
97101 teamId : team . id ,
98- template : terminalTemplate ,
102+ template : terminalTemplate ?? 'base' ,
99103 } )
100104
101105 if ( ! templateAvailable . ok ) {
@@ -119,7 +123,7 @@ export default async function TerminalPage({
119123 launchTarget = { {
120124 command,
121125 sandboxId : terminalSandboxId ,
122- template : terminalTemplate ,
126+ template : terminalTemplate ?? 'base' ,
123127 } }
124128 sandboxManagementAuth = { createSandboxManagementAuth (
125129 authContext ,
@@ -139,7 +143,7 @@ function normalizeTerminalSandboxId(sandboxId?: string) {
139143 return parsedSandboxId . success ? parsedSandboxId . data : null
140144}
141145
142- async function resolveTerminalSandboxTeam ( {
146+ async function resolveTerminalSandbox ( {
143147 accessToken,
144148 preferredTeamId,
145149 sandboxId,
@@ -152,34 +156,54 @@ async function resolveTerminalSandboxTeam({
152156} ) {
153157 if ( preferredTeamId ) {
154158 const preferredTeam = teams . find ( ( team ) => team . id === preferredTeamId )
155- if (
156- preferredTeam &&
157- ( await hasSandboxInTeam ( {
159+ if ( preferredTeam ) {
160+ const preferredSandbox = await getSandboxInTeam ( {
158161 accessToken,
159162 sandboxId,
160163 teamId : preferredTeam . id ,
161- } ) )
162- ) {
163- return preferredTeam
164+ } )
165+
166+ if ( preferredSandbox ) {
167+ return {
168+ team : preferredTeam ,
169+ template : getSandboxTemplate ( preferredSandbox ) ,
170+ }
171+ }
164172 }
165173 }
166174
167175 const candidateTeams = teams . filter ( ( team ) => team . id !== preferredTeamId )
168176 const teamMatches = await Promise . all (
169177 candidateTeams . map ( async ( team ) => ( {
170- team,
171- ownsSandbox : await hasSandboxInTeam ( {
178+ sandbox : await getSandboxInTeam ( {
172179 accessToken,
173180 sandboxId,
174181 teamId : team . id ,
175182 } ) ,
183+ team,
176184 } ) )
177185 )
178186
179- return teamMatches . find ( ( match ) => match . ownsSandbox ) ?. team ?? null
187+ const match = teamMatches . find ( ( { sandbox } ) => sandbox )
188+
189+ return match ?. sandbox
190+ ? {
191+ team : match . team ,
192+ template : getSandboxTemplate ( match . sandbox ) ,
193+ }
194+ : null
195+ }
196+
197+ type TerminalSandbox = {
198+ alias ?: string
199+ templateID : string
200+ }
201+
202+ function getSandboxTemplate ( sandbox : TerminalSandbox ) {
203+ return sandbox . alias ?? sandbox . templateID
180204}
181205
182- async function hasSandboxInTeam ( {
206+ async function getSandboxInTeam ( {
183207 accessToken,
184208 sandboxId,
185209 teamId,
@@ -201,9 +225,9 @@ async function hasSandboxInTeam({
201225 cache : 'no-store' ,
202226 } )
203227
204- return result . response . ok && Boolean ( result . data )
228+ return result . response . ok && result . data ? result . data : null
205229 } catch {
206- return false
230+ return null
207231 }
208232}
209233
0 commit comments