@@ -5,7 +5,6 @@ import com.coder.toolbox.cli.CoderCLIManager
5
5
import com.coder.toolbox.cli.ensureCLI
6
6
import com.coder.toolbox.plugin.PluginManager
7
7
import com.coder.toolbox.sdk.CoderRestClient
8
- import com.coder.toolbox.util.humanizeConnectionError
9
8
import com.coder.toolbox.util.toURL
10
9
import com.coder.toolbox.views.state.AuthWizardState
11
10
import com.jetbrains.toolbox.api.localization.LocalizableString
@@ -15,6 +14,10 @@ import com.jetbrains.toolbox.api.ui.components.ValidationErrorField
15
14
import kotlinx.coroutines.Job
16
15
import kotlinx.coroutines.flow.update
17
16
import kotlinx.coroutines.launch
17
+ import kotlinx.coroutines.yield
18
+ import java.util.concurrent.CancellationException
19
+
20
+ private const val USER_HIT_THE_BACK_BUTTON = " User hit the back button"
18
21
19
22
/* *
20
23
* A page that connects a REST client and cli to Coder.
@@ -27,12 +30,9 @@ class ConnectStep(
27
30
cli: CoderCLIManager ,
28
31
) -> Unit ,
29
32
) : WizardStep {
30
- private val settings = context.settingsStore.readOnly()
31
33
private var signInJob: Job ? = null
32
34
33
35
private val statusField = LabelField (context.i18n.pnotr(" " ))
34
-
35
- // override val description: LocalizableString = context.i18n.pnotr("Please wait while we configure Toolbox for ${url.host}.")
36
36
private val errorField = ValidationErrorField (context.i18n.pnotr(" " ))
37
37
38
38
override val panel: RowGroup = RowGroup (
@@ -75,21 +75,29 @@ class ConnectStep(
75
75
proxyValues = null ,
76
76
PluginManager .pluginInfo.version,
77
77
)
78
+ // allows interleaving with the back/cancel action
79
+ yield ()
78
80
client.authenticate()
79
- updateStatus (context.i18n.ptrl(" Checking Coder binary..." ), error = null )
81
+ statusField.textState.update { (context.i18n.ptrl(" Checking Coder binary..." )) }
80
82
val cli = ensureCLI(context, client.url, client.buildVersion)
81
83
// We only need to log in if we are using token-based auth.
82
84
if (client.token != null ) {
83
- updateStatus(context.i18n.ptrl(" Configuring CLI..." ), error = null )
85
+ statusField.textState.update { (context.i18n.ptrl(" Configuring CLI..." )) }
86
+ // allows interleaving with the back/cancel action
87
+ yield ()
84
88
cli.login(client.token)
85
89
}
90
+ // allows interleaving with the back/cancel action
91
+ yield ()
86
92
onConnect(client, cli)
87
93
AuthWizardState .resetSteps()
88
-
94
+ } catch (ex: CancellationException ) {
95
+ if (ex.message == USER_HIT_THE_BACK_BUTTON ) {
96
+ return @launch
97
+ }
98
+ notify(" Connection to ${url.host} was configured" , ex)
89
99
} catch (ex: Exception ) {
90
- val msg = humanizeConnectionError(url, settings.requireTokenAuth, ex)
91
100
notify(" Failed to configure ${url.host} " , ex)
92
- updateStatus(context.i18n.pnotr(" Failed to configure ${url.host} " ), msg)
93
101
}
94
102
}
95
103
}
@@ -99,24 +107,10 @@ class ConnectStep(
99
107
}
100
108
101
109
override fun onBack () {
102
- AuthWizardState .goToPreviousStep()
103
- }
104
-
105
- /* *
106
- * Update the status and error fields then refresh.
107
- */
108
- private fun updateStatus (newStatus : LocalizableString , error : String? ) {
109
- statusField.textState.update { newStatus }
110
- if (! error.isNullOrBlank()) {
111
- errorField.textState.update { context.i18n.pnotr(error) }
110
+ try {
111
+ signInJob?.cancel(CancellationException (USER_HIT_THE_BACK_BUTTON ))
112
+ } finally {
113
+ AuthWizardState .goToPreviousStep()
112
114
}
113
115
}
114
- //
115
- // /**
116
- // * Try connecting again after an error.
117
- // */
118
- // private fun retry() {
119
- // updateStatus(context.i18n.pnotr("Connecting to ${url.host}..."), null)
120
- // connect()
121
- // }
122
116
}
0 commit comments