Skip to content

Commit a254da8

Browse files
committed
impl: initial stubs for removing a workspace
- biggest issue is that popup dialogs are only visible in the main page so user can only confirm from that place. It can press the delete button even from the workspace details page, but it will have to navigate back to the main page to confirm whether he wants to proceed with the delete action. - the actual delete is not yet implemented
1 parent f2dc464 commit a254da8

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/main/kotlin/com/coder/toolbox/CoderRemoteEnvironment.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class CoderRemoteEnvironment(
7171
},
7272
)
7373
actionsList.add(
74-
Action("Stop", enabled = { status.ready() || status.pending() }) {
74+
Action("Stop", enabled = { status.canStop() }) {
7575
val build = client.stopWorkspace(workspace)
7676
workspace = workspace.copy(latestBuild = build)
7777
update(workspace, agent)
@@ -128,7 +128,32 @@ class CoderRemoteEnvironment(
128128
}
129129

130130
override fun onDelete() {
131-
throw NotImplementedError()
131+
cs.launch {
132+
// TODO info and cancel pop-ups only appear on the main page where all environments are listed.
133+
// However, #showSnackbar works on other pages. Until JetBrains fixes this issue we are going to use the snackbar
134+
val shouldDelete = if (status.canStop()) {
135+
ui.showOkCancelPopup(
136+
"Delete running workspace?",
137+
"Workspace will be closed and all the information in this workspace will be lost, including all files, unsaved changes and historical.",
138+
"Delete",
139+
"Cancel"
140+
)
141+
} else {
142+
ui.showOkCancelPopup(
143+
"Delete workspace?",
144+
"All the information in this workspace will be lost, including all files, unsaved changes and historical.",
145+
"Delete",
146+
"Cancel"
147+
)
148+
}
149+
if (shouldDelete) {
150+
if (status.canStop()) {
151+
client.stopWorkspace(workspace)
152+
}
153+
154+
client.removeWorkspace(workspace)
155+
}
156+
}
132157
}
133158

134159
/**

src/main/kotlin/com/coder/toolbox/models/WorkspaceAndAgentStatus.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ enum class WorkspaceAndAgentStatus(val label: String, val description: String) {
107107
fun canStart(): Boolean = listOf(STOPPED, FAILED, CANCELED)
108108
.contains(this)
109109

110+
/**
111+
* Return true if the workspace can be stopped.
112+
*/
113+
fun canStop(): Boolean = ready() || pending()
114+
110115
// We want to check that the workspace is `running`, the agent is
111116
// `connected`, and the agent lifecycle state is `ready` to ensure the best
112117
// possible scenario for attempting a connection.

src/main/kotlin/com/coder/toolbox/sdk/CoderRestClient.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import retrofit2.converter.moshi.MoshiConverterFactory
3030
import java.net.HttpURLConnection
3131
import java.net.ProxySelector
3232
import java.net.URL
33-
import java.util.*
33+
import java.util.UUID
3434
import javax.net.ssl.X509TrustManager
3535

3636
/**
@@ -229,7 +229,6 @@ open class CoderRestClient(
229229
}
230230

231231
/**
232-
* @throws [APIResponseException].
233232
*/
234233
fun stopWorkspace(workspace: Workspace): WorkspaceBuild {
235234
val buildRequest = CreateWorkspaceBuildRequest(null, WorkspaceTransition.STOP)
@@ -240,6 +239,13 @@ open class CoderRestClient(
240239
return buildResponse.body()!!
241240
}
242241

242+
/**
243+
* @throws [APIResponseException].
244+
*/
245+
fun removeWorkspace(workspace: Workspace) {
246+
// TODO - implement this
247+
}
248+
243249
/**
244250
* Start the workspace with the latest template version. Best practice is
245251
* to STOP a workspace before doing an update if it is started.

0 commit comments

Comments
 (0)