@@ -16,9 +16,7 @@ import com.coder.gateway.sdk.OS
16
16
import com.coder.gateway.sdk.ex.AuthenticationResponseException
17
17
import com.coder.gateway.sdk.getOS
18
18
import com.coder.gateway.sdk.toURL
19
- import com.coder.gateway.sdk.v2.models.ProvisionerJobStatus
20
19
import com.coder.gateway.sdk.v2.models.Workspace
21
- import com.coder.gateway.sdk.v2.models.WorkspaceBuildTransition
22
20
import com.coder.gateway.sdk.withPath
23
21
import com.intellij.ide.BrowserUtil
24
22
import com.intellij.ide.IdeBundle
@@ -51,11 +49,13 @@ import com.intellij.util.ui.ListTableModel
51
49
import com.intellij.util.ui.table.IconTableCellRenderer
52
50
import kotlinx.coroutines.CoroutineScope
53
51
import kotlinx.coroutines.Dispatchers
52
+ import kotlinx.coroutines.Job
54
53
import kotlinx.coroutines.cancel
54
+ import kotlinx.coroutines.delay
55
+ import kotlinx.coroutines.isActive
55
56
import kotlinx.coroutines.launch
56
57
import kotlinx.coroutines.withContext
57
58
import org.zeroturnaround.exec.ProcessExecutor
58
- import java.awt.Color
59
59
import java.awt.Component
60
60
import java.awt.Dimension
61
61
import javax.swing.Icon
@@ -88,6 +88,8 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
88
88
}
89
89
}
90
90
91
+ private var poller: Job ? = null
92
+
91
93
override val component = panel {
92
94
indent {
93
95
row {
@@ -105,10 +107,12 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
105
107
row(CoderGatewayBundle .message(" gateway.connector.view.login.url.label" )) {
106
108
textField().resizableColumn().horizontalAlign(HorizontalAlign .FILL ).gap(RightGap .SMALL ).bindText(wizardModel::coderURL).applyToComponent {
107
109
addActionListener {
110
+ poller?.cancel()
108
111
loginAndLoadWorkspace()
109
112
}
110
113
}
111
114
button(CoderGatewayBundle .message(" gateway.connector.view.coder.workspaces.connect.text" )) {
115
+ poller?.cancel()
112
116
loginAndLoadWorkspace()
113
117
}.applyToComponent {
114
118
background = WelcomeScreenUIManager .getMainAssociatedComponentBackground()
@@ -211,18 +215,30 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
211
215
}
212
216
213
217
private fun loadWorkspaces () {
214
- cs.launch {
215
- val workspaceList = withContext(Dispatchers .IO ) {
216
- try {
217
- return @withContext coderClient.workspaces().collectAgents()
218
- } catch (e: Exception ) {
219
- logger.error(" Could not retrieve workspaces for ${coderClient.me.username} on ${coderClient.coderURL} . Reason: $e " )
220
- emptyList()
218
+ poller = cs.launch {
219
+ while (isActive) {
220
+ val workspaceList = withContext(Dispatchers .IO ) {
221
+ try {
222
+ return @withContext coderClient.workspaces().collectAgents()
223
+ } catch (e: Exception ) {
224
+ logger.error(" Could not retrieve workspaces for ${coderClient.me.username} on ${coderClient.coderURL} . Reason: $e " )
225
+ emptyList()
226
+ }
221
227
}
222
- }
223
228
224
- // if we just run the update on the main dispatcher, the code will block because it cant get some AWT locks
225
- ApplicationManager .getApplication().invokeLater { listTableModelOfWorkspaces.updateItems(workspaceList) }
229
+ val selectedWorkspace = withContext(Dispatchers .Main ) {
230
+ tableOfWorkspaces.selectedObject
231
+ }
232
+
233
+ // if we just run the update on the main dispatcher, the code will block because it cant get some AWT locks
234
+ ApplicationManager .getApplication().invokeLater {
235
+ listTableModelOfWorkspaces.updateItems(workspaceList)
236
+ if (selectedWorkspace != null ) {
237
+ tableOfWorkspaces.selectItem(selectedWorkspace)
238
+ }
239
+ }
240
+ }
241
+ delay(5000 )
226
242
}
227
243
}
228
244
@@ -279,10 +295,16 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
279
295
}
280
296
}
281
297
298
+ override fun onPrevious () {
299
+ super .onPrevious()
300
+ poller?.cancel()
301
+ }
302
+
282
303
override fun onNext (wizardModel : CoderWorkspacesWizardModel ): Boolean {
283
304
val workspace = tableOfWorkspaces.selectedObject
284
305
if (workspace != null ) {
285
306
wizardModel.selectedWorkspace = workspace
307
+ poller?.cancel()
286
308
return true
287
309
}
288
310
return false
@@ -387,6 +409,13 @@ class CoderWorkspacesStepView(val enableNextButtonCallback: (Boolean) -> Unit) :
387
409
this .addRows(workspaces)
388
410
}
389
411
412
+ private fun TableView<WorkspaceAgentModel>.selectItem (workspace : WorkspaceAgentModel ) {
413
+ this .items.forEachIndexed { index, workspaceAgentModel ->
414
+ if (workspaceAgentModel.name == workspace.name)
415
+ this .setRowSelectionInterval(index, index)
416
+ }
417
+ }
418
+
390
419
companion object {
391
420
val logger = Logger .getInstance(CoderWorkspacesStepView ::class .java.simpleName)
392
421
}
0 commit comments