Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3ae664

Browse files
authored
fix: add typegen for templateVersionEditorXService (#6069)
This was borked before, and actually broken!
1 parent dd9e1f3 commit e3ae664

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

site/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
"@types/react-helmet": "6.1.5",
105105
"@types/react-syntax-highlighter": "15.5.5",
106106
"@types/semver": "7.3.12",
107+
"@types/tar-js": "^0.3.2",
107108
"@types/ua-parser-js": "0.7.36",
108109
"@types/uuid": "8.3.4",
109110
"@typescript-eslint/eslint-plugin": "5.50.0",

site/src/pages/TemplateVersionPage/TemplateVersionEditorPage/TemplateVersionEditorPage.tsx

-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ export const TemplateVersionEditorPage: FC = () => {
4242
if (!versionState.context.template) {
4343
throw new Error("no template")
4444
}
45-
// Send a cancel just in case a version is already being created!
46-
sendEvent({
47-
type: "CANCEL_VERSION",
48-
})
4945
sendEvent({
5046
type: "CREATE_VERSION",
5147
files: files,

site/src/xServices/templateVersionEditor/templateVersionEditorXService.ts

+41-15
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import {
22
ProvisionerJobLog,
3+
ProvisionerJobStatus,
34
TemplateVersion,
45
UploadResponse,
56
WorkspaceResource,
67
} from "api/typesGenerated"
78
import { assign, createMachine } from "xstate"
89
import * as API from "api/api"
910
import { TemplateVersionFiles } from "util/templateVersion"
10-
import * as Tar from "tar-js"
11+
import Tar from "tar-js"
1112

1213
export interface CreateVersionData {
1314
file: File
@@ -40,21 +41,34 @@ export const templateVersionEditorMachine = createMachine(
4041
| { type: "ADD_BUILD_LOG"; log: ProvisionerJobLog }
4142
| { type: "UPDATE_ACTIVE_VERSION" },
4243
services: {} as {
44+
uploadTar: {
45+
data: UploadResponse
46+
}
4347
createBuild: {
4448
data: TemplateVersion
4549
}
4650
cancelBuild: {
51+
data: void
52+
}
53+
fetchVersion: {
4754
data: TemplateVersion
4855
}
56+
getResources: {
57+
data: WorkspaceResource[]
58+
}
59+
updateActiveVersion: {
60+
data: void
61+
}
4962
},
5063
},
64+
tsTypes: {} as import("./templateVersionEditorXService.typegen").Typegen0,
5165
initial: "idle",
5266
states: {
5367
idle: {
5468
on: {
5569
CREATE_VERSION: {
5670
actions: ["assignCreateBuild"],
57-
target: "uploadTar",
71+
target: "cancelingBuild",
5872
},
5973
UPDATE_ACTIVE_VERSION: {
6074
target: "updatingActiveVersion",
@@ -71,14 +85,24 @@ export const templateVersionEditorMachine = createMachine(
7185
},
7286
},
7387
},
88+
cancelingBuild: {
89+
tags: "loading",
90+
invoke: {
91+
id: "cancelBuild",
92+
src: "cancelBuild",
93+
onDone: {
94+
target: "uploadTar",
95+
},
96+
},
97+
},
7498
uploadTar: {
7599
tags: "loading",
76100
invoke: {
77101
id: "uploadTar",
78102
src: "uploadTar",
79103
onDone: {
80104
target: "creatingBuild",
81-
actions: ["assignUploadResponse"],
105+
actions: "assignUploadResponse",
82106
},
83107
},
84108
},
@@ -88,7 +112,7 @@ export const templateVersionEditorMachine = createMachine(
88112
id: "createBuild",
89113
src: "createBuild",
90114
onDone: {
91-
actions: ["assignBuild"],
115+
actions: "assignBuild",
92116
target: "watchingBuildLogs",
93117
},
94118
},
@@ -104,14 +128,14 @@ export const templateVersionEditorMachine = createMachine(
104128
},
105129
on: {
106130
ADD_BUILD_LOG: {
107-
actions: ["addBuildLog"],
131+
actions: "addBuildLog",
108132
},
109133
CANCEL_VERSION: {
110-
actions: ["cancelBuild"],
134+
actions: "cancelBuild",
111135
target: "idle",
112136
},
113137
CREATE_VERSION: {
114-
actions: ["cancelBuild", "assignCreateBuild"],
138+
actions: ["assignCreateBuild"],
115139
target: "uploadTar",
116140
},
117141
},
@@ -145,8 +169,8 @@ export const templateVersionEditorMachine = createMachine(
145169
assignCreateBuild: assign({
146170
files: (_, event) => event.files,
147171
templateId: (_, event) => event.templateId,
148-
buildLogs: [],
149-
resources: [],
172+
buildLogs: (_, _1) => [],
173+
resources: (_, _1) => [],
150174
}),
151175
assignResources: assign({
152176
resources: (_, event) => event.data,
@@ -174,7 +198,7 @@ export const templateVersionEditorMachine = createMachine(
174198
...context.version,
175199
job: {
176200
...context.version.job,
177-
status: "running",
201+
status: "running" as ProvisionerJobStatus,
178202
},
179203
}
180204
},
@@ -238,20 +262,22 @@ export const templateVersionEditorMachine = createMachine(
238262
}
239263
return API.getTemplateVersionResources(ctx.version.id)
240264
},
241-
cancelBuild: (ctx) => {
265+
cancelBuild: async (ctx) => {
242266
if (!ctx.version) {
243-
throw new Error("template version must be set")
267+
return
268+
}
269+
if (ctx.version.job.status === "running") {
270+
await API.cancelTemplateVersionBuild(ctx.version.id)
244271
}
245-
return API.cancelTemplateVersionBuild(ctx.version.id)
246272
},
247-
updateActiveVersion: (ctx) => {
273+
updateActiveVersion: async (ctx) => {
248274
if (!ctx.templateId) {
249275
throw new Error("template must be set")
250276
}
251277
if (!ctx.version) {
252278
throw new Error("template version must be set")
253279
}
254-
return API.updateActiveTemplateVersion(ctx.templateId, {
280+
await API.updateActiveTemplateVersion(ctx.templateId, {
255281
id: ctx.version.id,
256282
})
257283
},

site/yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -3455,6 +3455,11 @@
34553455
resolved "https://registry.yarnpkg.com/@types/tapable/-/tapable-1.0.8.tgz#b94a4391c85666c7b73299fd3ad79d4faa435310"
34563456
integrity sha512-ipixuVrh2OdNmauvtT51o3d8z12p6LtFW9in7U79der/kwejjdNchQC5UMn5u/KxNoM7VHHOs/l8KS8uHxhODQ==
34573457

3458+
"@types/tar-js@^0.3.2":
3459+
version "0.3.2"
3460+
resolved "https://registry.yarnpkg.com/@types/tar-js/-/tar-js-0.3.2.tgz#23d3c7c8ce22bec6ed8355c2169dd3fd6ebe2583"
3461+
integrity sha512-0ySOBNP+/Ey67EZ0QaCOgkt6zSAeayTwsoln02ztlyB5lF4NQD0sl5C7E5eKS+QUb7xgTEPdPo9LUjYOHUJVqQ==
3462+
34583463
"@types/testing-library__jest-dom@^5.9.1":
34593464
version "5.14.5"
34603465
resolved "https://registry.yarnpkg.com/@types/testing-library__jest-dom/-/testing-library__jest-dom-5.14.5.tgz#d113709c90b3c75fdb127ec338dad7d5f86c974f"

0 commit comments

Comments
 (0)