@@ -10,10 +10,17 @@ import com.coder.toolbox.sdk.v2.models.Workspace
10
10
import com.coder.toolbox.sdk.v2.models.WorkspaceAgent
11
11
import com.coder.toolbox.sdk.v2.models.WorkspaceStatus
12
12
import com.coder.toolbox.util.WebUrlValidationResult.Invalid
13
+ import com.coder.toolbox.views.CoderCliSetupWizardPage
14
+ import com.coder.toolbox.views.CoderSettingsPage
15
+ import com.coder.toolbox.views.state.CoderCliSetupContext
16
+ import com.coder.toolbox.views.state.CoderCliSetupWizardState
17
+ import com.coder.toolbox.views.state.WizardStep
18
+ import com.jetbrains.toolbox.api.remoteDev.ProviderVisibilityState
13
19
import com.jetbrains.toolbox.api.remoteDev.connection.RemoteToolsHelper
14
20
import kotlinx.coroutines.Job
15
21
import kotlinx.coroutines.TimeoutCancellationException
16
22
import kotlinx.coroutines.delay
23
+ import kotlinx.coroutines.flow.MutableStateFlow
17
24
import kotlinx.coroutines.flow.StateFlow
18
25
import kotlinx.coroutines.launch
19
26
import kotlinx.coroutines.time.withTimeout
@@ -25,12 +32,13 @@ import kotlin.time.Duration.Companion.seconds
25
32
import kotlin.time.toJavaDuration
26
33
27
34
private const val CAN_T_HANDLE_URI_TITLE = " Can't handle URI"
28
- private val noOpTextProgress: (String ) -> Unit = { _ -> }
29
35
30
36
@Suppress(" UnstableApiUsage" )
31
37
open class CoderProtocolHandler (
32
38
private val context : CoderToolboxContext ,
33
39
private val dialogUi : DialogUi ,
40
+ private val settingsPage : CoderSettingsPage ,
41
+ private val visibilityState : MutableStateFlow <ProviderVisibilityState >,
34
42
private val isInitialized : StateFlow <Boolean >,
35
43
) {
36
44
private val settings = context.settingsStore.readOnly()
@@ -45,8 +53,6 @@ open class CoderProtocolHandler(
45
53
suspend fun handle (
46
54
uri : URI ,
47
55
shouldWaitForAutoLogin : Boolean ,
48
- markAsBusy : () -> Unit ,
49
- unmarkAsBusy : () -> Unit ,
50
56
reInitialize : suspend (CoderRestClient , CoderCLIManager ) -> Unit
51
57
) {
52
58
val params = uri.toQueryParameters()
@@ -58,7 +64,6 @@ open class CoderProtocolHandler(
58
64
// this switches to the main plugin screen, even
59
65
// if last opened provider was not Coder
60
66
context.envPageManager.showPluginEnvironmentsPage()
61
- markAsBusy()
62
67
if (shouldWaitForAutoLogin) {
63
68
isInitialized.waitForTrue()
64
69
}
@@ -67,39 +72,53 @@ open class CoderProtocolHandler(
67
72
val deploymentURL = resolveDeploymentUrl(params) ? : return
68
73
val token = if (! context.settingsStore.requireTokenAuth) null else resolveToken(params) ? : return
69
74
val workspaceName = resolveWorkspaceName(params) ? : return
70
- val restClient = buildRestClient(deploymentURL, token) ? : return
71
- val workspace = restClient.workspaces().matchName(workspaceName, deploymentURL) ? : return
72
75
73
- val cli = configureCli(deploymentURL, restClient)
74
-
75
- var agent: WorkspaceAgent
76
- try {
76
+ suspend fun onConnect (
77
+ restClient : CoderRestClient ,
78
+ cli : CoderCLIManager
79
+ ) {
80
+ val workspace = restClient.workspaces().matchName(workspaceName, deploymentURL)
81
+ if (workspace == null ) {
82
+ context.envPageManager.showPluginEnvironmentsPage()
83
+ return
84
+ }
77
85
reInitialize(restClient, cli)
78
86
context.envPageManager.showPluginEnvironmentsPage()
79
87
if (! prepareWorkspace(workspace, restClient, workspaceName, deploymentURL)) return
80
88
// we resolve the agent after the workspace is started otherwise we can get misleading
81
89
// errors like: no agent available while workspace is starting or stopping
82
90
// we also need to retrieve the workspace again to have the latest resources (ex: agent)
83
91
// attached to the workspace.
84
- agent = resolveAgent(
92
+ val agent: WorkspaceAgent = resolveAgent(
85
93
params,
86
94
restClient.workspace(workspace.id)
87
95
) ? : return
88
96
if (! ensureAgentIsReady(workspace, agent)) return
89
- } finally {
90
- unmarkAsBusy()
91
- }
92
- delay(2 .seconds)
93
- val environmentId = " ${workspace.name} .${agent.name} "
94
- context.showEnvironmentPage(environmentId)
97
+ delay(2 .seconds)
98
+ val environmentId = " ${workspace.name} .${agent.name} "
99
+ context.showEnvironmentPage(environmentId)
95
100
96
- val productCode = params.ideProductCode()
97
- val buildNumber = params.ideBuildNumber()
98
- val projectFolder = params.projectFolder()
101
+ val productCode = params.ideProductCode()
102
+ val buildNumber = params.ideBuildNumber()
103
+ val projectFolder = params.projectFolder()
104
+
105
+ if (! productCode.isNullOrBlank() && ! buildNumber.isNullOrBlank()) {
106
+ launchIde(environmentId, productCode, buildNumber, projectFolder)
107
+ }
108
+ }
99
109
100
- if (! productCode.isNullOrBlank() && ! buildNumber.isNullOrBlank()) {
101
- launchIde(environmentId, productCode, buildNumber, projectFolder)
110
+ CoderCliSetupContext .apply {
111
+ url = deploymentURL.toURL()
112
+ CoderCliSetupContext .token = token
102
113
}
114
+ CoderCliSetupWizardState .goToStep(WizardStep .CONNECT )
115
+ context.ui.showUiPage(
116
+ CoderCliSetupWizardPage (
117
+ context, settingsPage, visibilityState, true ,
118
+ jumpToMainPageOnError = true ,
119
+ onConnect = ::onConnect
120
+ )
121
+ )
103
122
}
104
123
105
124
private suspend fun resolveDeploymentUrl (params : Map <String , String >): String? {
@@ -308,13 +327,14 @@ open class CoderProtocolHandler(
308
327
309
328
private suspend fun configureCli (
310
329
deploymentURL : String ,
311
- restClient : CoderRestClient
330
+ restClient : CoderRestClient ,
331
+ progressReporter : (String ) -> Unit
312
332
): CoderCLIManager {
313
333
val cli = ensureCLI(
314
334
context,
315
335
deploymentURL.toURL(),
316
336
restClient.buildInfo().version,
317
- noOpTextProgress
337
+ progressReporter
318
338
)
319
339
320
340
// We only need to log in if we are using token-based auth.
@@ -455,12 +475,6 @@ open class CoderProtocolHandler(
455
475
}
456
476
}
457
477
458
-
459
- private fun CoderToolboxContext.popupPluginMainPage () {
460
- this .ui.showWindow()
461
- this .envPageManager.showPluginEnvironmentsPage(true )
462
- }
463
-
464
478
private suspend fun CoderToolboxContext.showEnvironmentPage (envId : String ) {
465
479
this .ui.showWindow()
466
480
this .envPageManager.showEnvironmentPage(envId, false )
0 commit comments