Skip to content

Commit c785282

Browse files
committed
Display error when auto creation fails
1 parent fefcf2f commit c785282

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

site/src/pages/CreateWorkspacePage/CreateWorkspacePage.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ import {
1919
import { CreateWorkspacePageView } from "./CreateWorkspacePageView"
2020
import { Loader } from "components/Loader/Loader"
2121
import { ErrorAlert } from "components/Alert/ErrorAlert"
22+
import {
23+
uniqueNamesGenerator,
24+
animals,
25+
colors,
26+
NumberDictionary,
27+
} from "unique-names-generator"
2228

2329
const CreateWorkspacePage: FC = () => {
2430
const organizationId = useOrganizationId()
@@ -27,21 +33,23 @@ const CreateWorkspacePage: FC = () => {
2733
const navigate = useNavigate()
2834
const [searchParams] = useSearchParams()
2935
const defaultBuildParameters = getDefaultBuildParameters(searchParams)
30-
const defaultName = searchParams.get("name") ?? ""
36+
const mode = (searchParams.get("mode") ?? "form") as CreateWorkspaceMode
3137
const [createWorkspaceState, send] = useMachine(createWorkspaceMachine, {
3238
context: {
3339
organizationId,
3440
templateName,
35-
mode: (searchParams.get("mode") ?? "form") as CreateWorkspaceMode,
41+
mode,
3642
defaultBuildParameters,
43+
defaultName:
44+
mode === "auto" ? generateUniqueName() : searchParams.get("name") ?? "",
3745
},
3846
actions: {
3947
onCreateWorkspace: (_, event) => {
4048
navigate(`/@${event.data.owner_name}/${event.data.name}`)
4149
},
4250
},
4351
})
44-
const { template, error, parameters, permissions, gitAuth } =
52+
const { template, error, parameters, permissions, gitAuth, defaultName } =
4553
createWorkspaceState.context
4654
const title = createWorkspaceState.matches("autoCreating")
4755
? "Creating workspace..."
@@ -86,6 +94,8 @@ const CreateWorkspacePage: FC = () => {
8694
)
8795
}
8896

97+
export default CreateWorkspacePage
98+
8999
const getDefaultBuildParameters = (
90100
urlSearchParams: URLSearchParams,
91101
): WorkspaceBuildParameter[] => {
@@ -114,4 +124,12 @@ export const orderedTemplateParameters = (
114124
return [...immutables, ...mutables]
115125
}
116126

117-
export default CreateWorkspacePage
127+
const generateUniqueName = () => {
128+
const numberDictionary = NumberDictionary.generate({ min: 0, max: 99 })
129+
return uniqueNamesGenerator({
130+
dictionaries: [animals, colors, numberDictionary],
131+
separator: "-",
132+
length: 3,
133+
style: "lowerCase",
134+
})
135+
}

site/src/pages/CreateWorkspacePage/CreateWorkspacePageView.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from "components/TemplateParameters/TemplateParameters"
2727
import { CreateWSPermissions } from "xServices/createWorkspace/createWorkspaceXService"
2828
import { GitAuth } from "components/GitAuth/GitAuth"
29+
import { ErrorAlert } from "components/Alert/ErrorAlert"
2930

3031
export interface CreateWorkspacePageViewProps {
3132
error: unknown
@@ -101,6 +102,7 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
101102
return (
102103
<FullPageHorizontalForm title="New workspace" onCancel={onCancel}>
103104
<HorizontalForm onSubmit={form.handleSubmit}>
105+
{Boolean(error) && <ErrorAlert error={error} />}
104106
{/* General info */}
105107
<FormSection
106108
title="General"

site/src/xServices/createWorkspace/createWorkspaceXService.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ import {
1515
WorkspaceBuildParameter,
1616
} from "api/typesGenerated"
1717
import { assign, createMachine } from "xstate"
18-
import {
19-
uniqueNamesGenerator,
20-
animals,
21-
colors,
22-
NumberDictionary,
23-
} from "unique-names-generator"
2418
import { paramsUsedToCreateWorkspace } from "utils/workspace"
2519
import { REFRESH_GITAUTH_BROADCAST_CHANNEL } from "utils/gitAuth"
2620

@@ -30,6 +24,7 @@ type CreateWorkspaceContext = {
3024
organizationId: string
3125
templateName: string
3226
mode: CreateWorkspaceMode
27+
defaultName: string
3328
error?: Error | unknown
3429
// Form
3530
template?: Template
@@ -96,7 +91,7 @@ export const createWorkspaceMachine =
9691
},
9792
onError: {
9893
actions: ["assignError"],
99-
target: "idle",
94+
target: "loadingFormData",
10095
},
10196
},
10297
},
@@ -161,11 +156,12 @@ export const createWorkspaceMachine =
161156
templateName,
162157
organizationId,
163158
defaultBuildParameters,
159+
defaultName,
164160
}) => {
165161
const template = await getTemplateByName(organizationId, templateName)
166162
return createWorkspace(organizationId, "me", {
167163
template_id: template.id,
168-
name: generateUniqueName(),
164+
name: defaultName,
169165
rich_parameter_values: defaultBuildParameters,
170166
})
171167
},
@@ -206,16 +202,6 @@ export const createWorkspaceMachine =
206202
},
207203
)
208204

209-
const generateUniqueName = () => {
210-
const numberDictionary = NumberDictionary.generate({ min: 0, max: 99 })
211-
return uniqueNamesGenerator({
212-
dictionaries: [animals, colors, numberDictionary],
213-
separator: "-",
214-
length: 3,
215-
style: "lowerCase",
216-
})
217-
}
218-
219205
const checkCreateWSPermissions = async (organizationId: string) => {
220206
// HACK: below, we pass in * for the owner_id, which is a hacky way of checking if the
221207
// current user can create a workspace on behalf of anyone within the org (only org owners should be able to do this).

0 commit comments

Comments
 (0)