Skip to content

Commit 4572088

Browse files
committed
chore: separate autologin concerns
ConnectStep should not care if it was it is the first screen in the login process (auto login) or not. Instead, the wizard page should orchestrate the behavior of the back button depending on whether it is auto login or not
1 parent caf80d3 commit 4572088

3 files changed

Lines changed: 24 additions & 49 deletions

File tree

src/main/kotlin/com/coder/toolbox/CoderRemoteProvider.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ class CoderRemoteProvider(
379379
context, settingsPage, visibilityState,
380380
url = newUrl,
381381
credentials = credentials,
382-
jumpToMainPageOnError = true,
383382
onConnect = onConnect.andThen(deferredLinkHandler(params, newUrl))
384383
.andThen { _, _ ->
385384
coderHeaderPage.isBusy.update { false }
@@ -642,4 +641,4 @@ class CoderRemoteProvider(
642641
LoadableState.Loading
643642
}
644643
}
645-
}
644+
}

src/main/kotlin/com/coder/toolbox/views/CoderSetupWizardPage.kt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,11 @@ class CoderSetupWizardPage private constructor(
2121
private val context: CoderToolboxContext,
2222
private val settingsPage: CoderSettingsPage,
2323
visibilityState: StateFlow<ProviderVisibilityState>,
24-
initialAutoSetup: Boolean = false,
25-
jumpToMainPageOnError: Boolean = false,
24+
private var autoLogin: Boolean = false,
2625
onConnect: SuspendBiConsumer<CoderRestClient, CoderCLIManager>,
2726
onTokenRefreshed: (suspend (url: URL, oauthSessionCtx: CoderOAuthSessionContext) -> Unit)? = null
2827
) : CoderPage(MutableStateFlow(context.i18n.ptrl("Setting up Coder")), false) {
2928
val model: WizardModel = WizardModel()
30-
31-
private val shouldAutoSetup = MutableStateFlow(initialAutoSetup)
3229
private val settingsAction = Action(context, "Settings") {
3330
context.ui.showUiPage(settingsPage)
3431
}
@@ -38,10 +35,8 @@ class CoderSetupWizardPage private constructor(
3835
private val connectStep = ConnectStep(
3936
context,
4037
model,
41-
shouldAutoLogin = shouldAutoSetup,
42-
jumpToMainPageOnError = jumpToMainPageOnError,
4338
visibilityState,
44-
refreshWizard = this::displaySteps,
39+
navigateBack = this::navigateBackFromConnect,
4540
onConnect = onConnect,
4641
onTokenRefreshed = onTokenRefreshed
4742
)
@@ -114,9 +109,6 @@ class CoderSetupWizardPage private constructor(
114109
settingsAction,
115110
Action(context, "Back", closesPage = false, actionBlock = {
116111
connectStep.onBack()
117-
shouldAutoSetup.update {
118-
false
119-
}
120112
displaySteps()
121113
})
122114
)
@@ -131,6 +123,20 @@ class CoderSetupWizardPage private constructor(
131123
model.goTo(WizardStep.CONNECT)
132124
}
133125

126+
private fun navigateBackFromConnect() {
127+
if (autoLogin) {
128+
autoLogin = false
129+
model.clearFormData()
130+
model.goToFirst()
131+
return
132+
}
133+
if (context.settingsStore.requiresTokenAuth) {
134+
model.goToPrevious()
135+
} else {
136+
model.goToFirst()
137+
}
138+
}
139+
134140
/**
135141
* Cancels any in-flight work owned by this wizard. Called by the page router
136142
* when the wizard is being replaced (e.g. by a deep link to a different
@@ -170,14 +176,11 @@ class CoderSetupWizardPage private constructor(
170176
visibilityState: StateFlow<ProviderVisibilityState>,
171177
url: URL,
172178
credentials: Credentials,
173-
initialAutoSetup: Boolean = true,
174-
jumpToMainPageOnError: Boolean = false,
175179
onConnect: SuspendBiConsumer<CoderRestClient, CoderCLIManager>,
176180
onTokenRefreshed: (suspend (url: URL, oauthSessionCtx: CoderOAuthSessionContext) -> Unit)? = null,
177181
): CoderSetupWizardPage = CoderSetupWizardPage(
178182
context, settingsPage, visibilityState,
179-
initialAutoSetup = initialAutoSetup,
180-
jumpToMainPageOnError = jumpToMainPageOnError,
183+
autoLogin = true,
181184
onConnect = onConnect,
182185
onTokenRefreshed = onTokenRefreshed,
183186
).apply {

src/main/kotlin/com/coder/toolbox/views/ConnectStep.kt

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ private const val USER_HIT_THE_BACK_BUTTON = "User hit the back button"
3030
class ConnectStep(
3131
private val context: CoderToolboxContext,
3232
private val model: WizardModel,
33-
private val shouldAutoLogin: StateFlow<Boolean>,
34-
private val jumpToMainPageOnError: Boolean,
3533
visibilityState: StateFlow<ProviderVisibilityState>,
36-
private val refreshWizard: () -> Unit,
34+
private val navigateBack: () -> Unit,
3735
private val onConnect: SuspendBiConsumer<CoderRestClient, CoderCLIManager>,
3836
private val onTokenRefreshed: (suspend (url: URL, oauthSessionCtx: CoderOAuthSessionContext) -> Unit)? = null
3937
) : WizardStep {
@@ -63,7 +61,7 @@ class ConnectStep(
6361

6462
// Don't launch another connection attempt if one is already in progress.
6563
if (signInJob?.isActive == true) {
66-
context.logger.info(">> ConnectStep: connection already in progress, skipping duplicate")
64+
context.logger.info("Connection already in progress, skipping duplicate")
6765
return
6866
}
6967

@@ -145,16 +143,11 @@ class ConnectStep(
145143
// The provider's onConnect ran close() which clears the router; combined
146144
// with client now being non-null this drops the wizard from getOverrideUiPage.
147145
context.envPageManager.showPluginEnvironmentsPage()
148-
} catch (ex: CancellationException) {
146+
} catch (ex: Exception) {
149147
if (ex.message != USER_HIT_THE_BACK_BUTTON) {
150-
errorReporter.report("Connection to $hostName was configured", ex)
151-
handleNavigation()
152-
refreshWizard()
148+
errorReporter.report("Failed to configure $hostName", ex)
153149
}
154-
} catch (ex: Exception) {
155-
errorReporter.report("Failed to configure $hostName", ex)
156-
handleNavigation()
157-
refreshWizard()
150+
navigateBack()
158151
}
159152
}
160153

@@ -176,26 +169,6 @@ class ConnectStep(
176169
statusField.textState.update { context.i18n.pnotr(msg) }
177170
}
178171

179-
/**
180-
* Handle navigation logic for both errors and back button
181-
*/
182-
private fun handleNavigation() {
183-
if (shouldAutoLogin.value) {
184-
model.clearFormData()
185-
if (jumpToMainPageOnError) {
186-
context.popupPluginMainPage()
187-
} else {
188-
model.goToFirst()
189-
}
190-
} else {
191-
if (context.settingsStore.requiresTokenAuth) {
192-
model.goToPrevious()
193-
} else {
194-
model.goToFirst()
195-
}
196-
}
197-
}
198-
199172
override suspend fun onNext(): Boolean {
200173
return false
201174
}
@@ -205,7 +178,7 @@ class ConnectStep(
205178
context.logger.info("Back button was pressed, cancelling in-progress connection setup...")
206179
signInJob?.cancel(CancellationException(USER_HIT_THE_BACK_BUTTON))
207180
} finally {
208-
handleNavigation()
181+
navigateBack()
209182
}
210183
}
211184

0 commit comments

Comments
 (0)