@@ -7,9 +7,8 @@ import com.coder.toolbox.services.CoderSecretsService
7
7
import com.coder.toolbox.services.CoderSettingsService
8
8
import com.coder.toolbox.settings.CoderSettings
9
9
import com.coder.toolbox.settings.Source
10
+ import com.coder.toolbox.util.CoderProtocolHandler
10
11
import com.coder.toolbox.util.DialogUi
11
- import com.coder.toolbox.util.LinkHandler
12
- import com.coder.toolbox.util.toQueryParameters
13
12
import com.coder.toolbox.views.Action
14
13
import com.coder.toolbox.views.CoderSettingsPage
15
14
import com.coder.toolbox.views.ConnectPage
@@ -53,7 +52,6 @@ class CoderRemoteProvider(
53
52
private val secrets: CoderSecretsService = CoderSecretsService (context.secretsStore)
54
53
private val settingsPage: CoderSettingsPage = CoderSettingsPage (context, settingsService)
55
54
private val dialogUi = DialogUi (context, settings)
56
- private val linkHandler = LinkHandler (context, settings, httpClient, dialogUi)
57
55
58
56
// The REST client, if we are signed in
59
57
private var client: CoderRestClient ? = null
@@ -65,7 +63,9 @@ class CoderRemoteProvider(
65
63
66
64
// On the first load, automatically log in if we can.
67
65
private var firstRun = true
68
-
66
+ private val isInitialized: MutableStateFlow <Boolean > = MutableStateFlow (false )
67
+ private var coderHeaderPage = NewEnvironmentPage (context, context.i18n.pnotr(getDeploymentURL()?.first ? : " " ))
68
+ private val linkHandler = CoderProtocolHandler (context, settings, httpClient, dialogUi, isInitialized)
69
69
override val environments: MutableStateFlow <LoadableState <List <RemoteProviderEnvironment >>> = MutableStateFlow (
70
70
LoadableState .Value (emptyList())
71
71
)
@@ -122,6 +122,12 @@ class CoderRemoteProvider(
122
122
environments.update {
123
123
LoadableState .Value (resolvedEnvironments.toList())
124
124
}
125
+ if (isInitialized.value == false ) {
126
+ context.logger.info(" Environments for ${client.url} are now initialized" )
127
+ isInitialized.update {
128
+ true
129
+ }
130
+ }
125
131
126
132
lastEnvironments = resolvedEnvironments
127
133
} catch (_: CancellationException ) {
@@ -171,14 +177,14 @@ class CoderRemoteProvider(
171
177
/* *
172
178
* Cancel polling and clear the client and environments.
173
179
*
174
- * Called as part of our own logout but it is unclear where it is called by
175
- * Toolbox. Maybe on uninstall?
180
+ * Also called as part of our own logout.
176
181
*/
177
182
override fun close () {
178
183
pollJob?.cancel()
179
- client = null
184
+ client?.close()
180
185
lastEnvironments = null
181
186
environments.value = LoadableState .Value (emptyList())
187
+ isInitialized.update { false }
182
188
}
183
189
184
190
override val svgIcon: SvgIcon =
@@ -213,8 +219,7 @@ class CoderRemoteProvider(
213
219
* Just displays the deployment URL at the moment, but we could use this as
214
220
* a form for creating new environments.
215
221
*/
216
- override fun getNewEnvironmentUiPage (): UiPage =
217
- NewEnvironmentPage (context, context.i18n.pnotr(getDeploymentURL()?.first ? : " " ))
222
+ override fun getNewEnvironmentUiPage (): UiPage = coderHeaderPage
218
223
219
224
/* *
220
225
* We always show a list of environments.
@@ -233,11 +238,13 @@ class CoderRemoteProvider(
233
238
* Handle incoming links (like from the dashboard).
234
239
*/
235
240
override suspend fun handleUri (uri : URI ) {
236
- val params = uri.toQueryParameters()
237
- context.cs.launch {
238
- val name = linkHandler.handle(params)
239
- // TODO@JB: Now what? How do we actually connect this workspace?
240
- context.logger.debug(" External request for $name : $uri " )
241
+ linkHandler.handle(uri, shouldDoAutoLogin()) { restClient, cli ->
242
+ // stop polling and de-initialize resources
243
+ close()
244
+ // start initialization with the new settings
245
+ this @CoderRemoteProvider.client = restClient
246
+ coderHeaderPage = NewEnvironmentPage (context, context.i18n.pnotr(restClient.url.toString()))
247
+ pollJob = poll(restClient, cli)
241
248
}
242
249
}
243
250
@@ -263,7 +270,7 @@ class CoderRemoteProvider(
263
270
// Show sign in page if we have not configured the client yet.
264
271
if (client == null ) {
265
272
// When coming back to the application, authenticate immediately.
266
- val autologin = firstRun && secrets.rememberMe == " true "
273
+ val autologin = shouldDoAutoLogin()
267
274
var autologinEx: Exception ? = null
268
275
secrets.lastToken.let { lastToken ->
269
276
secrets.lastDeploymentURL.let { lastDeploymentURL ->
@@ -302,6 +309,8 @@ class CoderRemoteProvider(
302
309
return null
303
310
}
304
311
312
+ private fun shouldDoAutoLogin (): Boolean = firstRun && secrets.rememberMe == " true"
313
+
305
314
/* *
306
315
* Create a connect page that starts polling and resets the UI on success.
307
316
*/
0 commit comments