@@ -4,7 +4,6 @@ import com.coder.toolbox.browser.browse
44import com.coder.toolbox.cli.CoderCLIManager
55import com.coder.toolbox.feed.IdeFeedManager
66import com.coder.toolbox.oauth.OAuth2Client
7- import com.coder.toolbox.oauth.OAuthTokenResponse
87import com.coder.toolbox.plugin.PluginManager
98import com.coder.toolbox.sdk.CoderRestClient
109import com.coder.toolbox.sdk.ex.APIResponseException
@@ -22,14 +21,15 @@ import com.coder.toolbox.util.url
2221import com.coder.toolbox.util.validateStrictWebUrl
2322import com.coder.toolbox.util.withPath
2423import com.coder.toolbox.views.Action
25- import com.coder.toolbox.views.CoderCliSetupWizardPage
2624import com.coder.toolbox.views.CoderDelimiter
2725import com.coder.toolbox.views.CoderSettingsPage
26+ import com.coder.toolbox.views.CoderSetupWizardPage
2827import com.coder.toolbox.views.NewEnvironmentPage
2928import com.coder.toolbox.views.SuspendBiConsumer
3029import com.coder.toolbox.views.state.CoderOAuthSessionContext
30+ import com.coder.toolbox.views.state.Credentials
3131import com.coder.toolbox.views.state.PageRouter
32- import com.coder.toolbox.views.state.WizardStep
32+ import com.coder.toolbox.views.state.toSessionContext
3333import com.jetbrains.toolbox.api.core.ui.icons.SvgIcon
3434import com.jetbrains.toolbox.api.core.ui.icons.SvgIcon.IconType
3535import com.jetbrains.toolbox.api.core.util.LoadableState
@@ -374,20 +374,18 @@ class CoderRemoteProvider(
374374 // Different URL - we need a new connection. Tear down any
375375 // in-flight wizard, install a fresh one on the router, and let
376376 // showPluginEnvironmentsPage() pull it through getOverrideUiPage.
377- router.activeWizard?.dispose()
378- val wizard = CoderCliSetupWizardPage (
377+ val credentials = newToken?. let { Credentials . Token (it) } ? : Credentials . MTls
378+ val wizard = CoderSetupWizardPage .connectStep (
379379 context, settingsPage, visibilityState,
380- initialAutoSetup = true ,
380+ url = newUrl,
381+ credentials = credentials,
381382 jumpToMainPageOnError = true ,
382383 onConnect = onConnect.andThen(deferredLinkHandler(params, newUrl))
383384 .andThen { _, _ ->
384385 coderHeaderPage.isBusy.update { false }
385386 },
386- onTokenRefreshed = ::onTokenRefreshed
387+ onTokenRefreshed = ::onTokenRefreshed,
387388 )
388- wizard.model.url = newUrl
389- wizard.model.token = newToken
390- wizard.model.goTo(WizardStep .CONNECT )
391389 router.replaceWith(wizard)
392390 context.envPageManager.showPluginEnvironmentsPage()
393391 }
@@ -454,14 +452,13 @@ class CoderRemoteProvider(
454452 private suspend fun exchangeOAuthCodeForToken (
455453 code : String ,
456454 oauthSessionContext : CoderOAuthSessionContext ,
457- wizard : CoderCliSetupWizardPage ,
455+ wizard : CoderSetupWizardPage ,
458456 ) {
459457 try {
460458 context.logger.info(" Handling OAuth callback..." )
461459
462460 val tokenResponse = OAuth2Client (context).exchangeCode(oauthSessionContext, code)
463- wizard.model.oauthSession = oauthSessionContext.copy(tokenResponse = tokenResponse)
464- wizard.model.goTo(WizardStep .CONNECT )
461+ wizard.advanceToConnectWithOAuth(oauthSessionContext.copy(tokenResponse = tokenResponse))
465462 } catch (e: Exception ) {
466463 context.logAndShowError(" OAuth Error" , " Exception during token exchange: ${e.message} " , e)
467464 }
@@ -529,47 +526,31 @@ class CoderRemoteProvider(
529526 */
530527 override fun getOverrideUiPage (): UiPage ? {
531528 if (client != null ) return null
532- return router.requireWizard { buildSetupWizard() }
529+ return router.getOrCreate { buildSetupWizard() }
533530 }
534531
535532 /* *
536533 * Build the wizard for the current state. Called once per provider lifetime
537534 * (until [close] clears the router); subsequent visibility cycles reuse the
538535 * same instance, preserving any in-flight connect job.
539536 */
540- private fun buildSetupWizard (): CoderCliSetupWizardPage {
537+ private fun buildSetupWizard (): CoderSetupWizardPage {
541538 // When coming back to the application, initializeSession immediately.
542539 if (shouldDoAutoSetup()) {
543540 try {
544- val storedOAuthSession = context.secrets.oauthSessionFor(context.deploymentUrl.toString())
545- val wizard = CoderCliSetupWizardPage (
541+ val url = context.deploymentUrl
542+ val credentials = context.secrets.oauthSessionFor(url.toString())?.let {
543+ Credentials .OAuth (it.toSessionContext())
544+ } ? : context.secrets.apiTokenFor(url)?.let {
545+ Credentials .Token (it)
546+ } ? : Credentials .MTls
547+ return CoderSetupWizardPage .connectStep(
546548 context, settingsPage, visibilityState,
547- initialAutoSetup = true ,
548- jumpToMainPageOnError = false ,
549+ url = url ,
550+ credentials = credentials ,
549551 onConnect = onConnect,
550- onTokenRefreshed = ::onTokenRefreshed
552+ onTokenRefreshed = ::onTokenRefreshed,
551553 )
552- wizard.model.url = context.deploymentUrl
553- wizard.model.token = context.secrets.apiTokenFor(context.deploymentUrl)
554- if (storedOAuthSession != null ) {
555- wizard.model.oauthSession = CoderOAuthSessionContext (
556- clientId = storedOAuthSession.clientId,
557- clientSecret = storedOAuthSession.clientSecret,
558- tokenCodeVerifier = " " ,
559- state = " " ,
560- tokenEndpoint = storedOAuthSession.tokenEndpoint,
561- tokenAuthMethod = storedOAuthSession.tokenAuthMethod,
562- tokenResponse = OAuthTokenResponse (
563- accessToken = " " ,
564- tokenType = " " ,
565- expiresIn = null ,
566- refreshToken = storedOAuthSession.refreshToken,
567- scope = null
568- )
569- )
570- }
571- wizard.model.goTo(WizardStep .CONNECT )
572- return wizard
573554 } catch (ex: Exception ) {
574555 errorBuffer.add(ex)
575556 } finally {
@@ -578,14 +559,11 @@ class CoderRemoteProvider(
578559 }
579560
580561 // Login flow.
581- val setupWizardPage = CoderCliSetupWizardPage (
582- context,
583- settingsPage,
584- visibilityState,
562+ val setupWizardPage = CoderSetupWizardPage .deploymentUrlStep(
563+ context, settingsPage, visibilityState,
585564 onConnect = onConnect,
586- onTokenRefreshed = ::onTokenRefreshed
565+ onTokenRefreshed = ::onTokenRefreshed,
587566 )
588- setupWizardPage.model.goToFirst()
589567 // We might have navigated here due to a polling error.
590568 errorBuffer.forEach {
591569 setupWizardPage.notify(" Error encountered" , it)
0 commit comments