@@ -7,9 +7,8 @@ import com.coder.toolbox.services.CoderSecretsService
77import com.coder.toolbox.services.CoderSettingsService
88import com.coder.toolbox.settings.CoderSettings
99import com.coder.toolbox.settings.Source
10+ import com.coder.toolbox.util.CoderProtocolHandler
1011import com.coder.toolbox.util.DialogUi
11- import com.coder.toolbox.util.LinkHandler
12- import com.coder.toolbox.util.toQueryParameters
1312import com.coder.toolbox.views.Action
1413import com.coder.toolbox.views.CoderSettingsPage
1514import com.coder.toolbox.views.ConnectPage
@@ -53,7 +52,6 @@ class CoderRemoteProvider(
5352 private val secrets: CoderSecretsService = CoderSecretsService (context.secretsStore)
5453 private val settingsPage: CoderSettingsPage = CoderSettingsPage (context, settingsService)
5554 private val dialogUi = DialogUi (context, settings)
56- private val linkHandler = LinkHandler (context, settings, httpClient, dialogUi)
5755
5856 // The REST client, if we are signed in
5957 private var client: CoderRestClient ? = null
@@ -65,7 +63,9 @@ class CoderRemoteProvider(
6563
6664 // On the first load, automatically log in if we can.
6765 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)
6969 override val environments: MutableStateFlow <LoadableState <List <RemoteProviderEnvironment >>> = MutableStateFlow (
7070 LoadableState .Value (emptyList())
7171 )
@@ -122,6 +122,12 @@ class CoderRemoteProvider(
122122 environments.update {
123123 LoadableState .Value (resolvedEnvironments.toList())
124124 }
125+ if (isInitialized.value == false ) {
126+ context.logger.info(" Environments for ${client.url} are now initialized" )
127+ isInitialized.update {
128+ true
129+ }
130+ }
125131
126132 lastEnvironments = resolvedEnvironments
127133 } catch (_: CancellationException ) {
@@ -171,14 +177,14 @@ class CoderRemoteProvider(
171177 /* *
172178 * Cancel polling and clear the client and environments.
173179 *
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.
176181 */
177182 override fun close () {
178183 pollJob?.cancel()
179- client = null
184+ client?.close()
180185 lastEnvironments = null
181186 environments.value = LoadableState .Value (emptyList())
187+ isInitialized.update { false }
182188 }
183189
184190 override val svgIcon: SvgIcon =
@@ -213,8 +219,7 @@ class CoderRemoteProvider(
213219 * Just displays the deployment URL at the moment, but we could use this as
214220 * a form for creating new environments.
215221 */
216- override fun getNewEnvironmentUiPage (): UiPage =
217- NewEnvironmentPage (context, context.i18n.pnotr(getDeploymentURL()?.first ? : " " ))
222+ override fun getNewEnvironmentUiPage (): UiPage = coderHeaderPage
218223
219224 /* *
220225 * We always show a list of environments.
@@ -233,11 +238,13 @@ class CoderRemoteProvider(
233238 * Handle incoming links (like from the dashboard).
234239 */
235240 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)
241248 }
242249 }
243250
@@ -263,7 +270,7 @@ class CoderRemoteProvider(
263270 // Show sign in page if we have not configured the client yet.
264271 if (client == null ) {
265272 // When coming back to the application, authenticate immediately.
266- val autologin = firstRun && secrets.rememberMe == " true "
273+ val autologin = shouldDoAutoLogin()
267274 var autologinEx: Exception ? = null
268275 secrets.lastToken.let { lastToken ->
269276 secrets.lastDeploymentURL.let { lastDeploymentURL ->
@@ -302,6 +309,8 @@ class CoderRemoteProvider(
302309 return null
303310 }
304311
312+ private fun shouldDoAutoLogin (): Boolean = firstRun && secrets.rememberMe == " true"
313+
305314 /* *
306315 * Create a connect page that starts polling and resets the UI on success.
307316 */
0 commit comments