Skip to content

Commit b04eab5

Browse files
Followup Tasks for new Create Workspace page (#16591)
* adding mutation for create workspace * swapping to use mutation and a few cleanup tasks * add cancel button to existing workspace modal
1 parent d48ea07 commit b04eab5

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { GitpodServer } from "@gitpod/gitpod-protocol";
8+
import { useMutation } from "@tanstack/react-query";
9+
import { getGitpodService } from "../../service/service";
10+
11+
export const useCreateWorkspaceMutation = () => {
12+
return useMutation({
13+
mutationFn: async (options: GitpodServer.CreateWorkspaceOptions) => {
14+
return await getGitpodService().server.createWorkspace(options);
15+
},
16+
});
17+
};

components/dashboard/src/workspaces/CreateWorkspacePage.tsx

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
1010
import { Deferred } from "@gitpod/gitpod-protocol/lib/util/deferred";
1111
import { FunctionComponent, useCallback, useState } from "react";
1212
import { useHistory, useLocation } from "react-router";
13-
import Modal from "../components/Modal";
13+
import Modal, { ModalBody, ModalFooter, ModalHeader } from "../components/Modal";
1414
import RepositoryFinder from "../components/RepositoryFinder";
1515
import SelectIDEComponent from "../components/SelectIDEComponent";
1616
import SelectWorkspaceClassComponent from "../components/SelectWorkspaceClassComponent";
1717
import { UsageLimitReachedModal } from "../components/UsageLimitReachedModal";
1818
import { openAuthorizeWindow } from "../provider-utils";
19-
import { getGitpodService, gitpodHostUrl } from "../service/service";
19+
import { gitpodHostUrl } from "../service/service";
2020
import { LimitReachedOutOfHours, LimitReachedParallelWorkspacesModal } from "../start/CreateWorkspace";
2121
import { StartWorkspaceOptions } from "../start/start-workspace-options";
2222
import { StartWorkspaceError } from "../start/StartPage";
@@ -25,6 +25,7 @@ import { SelectAccountModal } from "../user-settings/SelectAccountModal";
2525
import Spinner from "../icons/Spinner.svg";
2626
import { useFeatureFlags } from "../contexts/FeatureFlagContext";
2727
import { useCurrentTeam } from "../teams/teams-context";
28+
import { useCreateWorkspaceMutation } from "../data/workspaces/create-workspace-mutation";
2829

2930
export const useNewCreateWorkspacePage = () => {
3031
const { startWithOptions } = useFeatureFlags();
@@ -38,6 +39,7 @@ export function CreateWorkspacePage() {
3839
const location = useLocation();
3940
const history = useHistory();
4041
const props = StartWorkspaceOptions.parseSearchParams(location.search);
42+
const createWorkspaceMutation = useCreateWorkspaceMutation();
4143

4244
const [useLatestIde, setUseLatestIde] = useState(
4345
props.ideSettings?.useLatestVersion !== undefined
@@ -60,10 +62,8 @@ export function CreateWorkspacePage() {
6062
[setSelectedIde, setUseLatestIde],
6163
);
6264
const [errorIde, setErrorIde] = useState<string | undefined>(undefined);
63-
const [creating, setCreating] = useState(false);
6465

6566
const [existingWorkspaces, setExistingWorkspaces] = useState<WorkspaceInfo[]>([]);
66-
const [createError, setCreateError] = useState<StartWorkspaceError | undefined>(undefined);
6767
const [selectAccountError, setSelectAccountError] = useState<SelectAccountPayload | undefined>(undefined);
6868

6969
const createWorkspace = useCallback(
@@ -85,8 +85,7 @@ export function CreateWorkspacePage() {
8585
}
8686

8787
try {
88-
setCreating(true);
89-
const result = await getGitpodService().server.createWorkspace({
88+
const result = await createWorkspaceMutation.mutateAsync({
9089
contextUrl: repo,
9190
organizationId: team?.id,
9291
...opts,
@@ -99,12 +98,10 @@ export function CreateWorkspacePage() {
9998
setExistingWorkspaces(result.existingWorkspaces);
10099
}
101100
} catch (error) {
102-
setCreateError(error);
103-
} finally {
104-
setCreating(false);
101+
console.log(error);
105102
}
106103
},
107-
[history, repo, selectedIde, selectedWsClass, team?.id, useLatestIde],
104+
[createWorkspaceMutation, history, repo, selectedIde, selectedWsClass, team?.id, useLatestIde],
108105
);
109106

110107
const onClickCreate = useCallback(() => createWorkspace(), [createWorkspace]);
@@ -119,15 +116,6 @@ export function CreateWorkspacePage() {
119116
/>
120117
);
121118
}
122-
if (existingWorkspaces.length > 0) {
123-
return (
124-
<ExistingWorkspaceModal
125-
existingWorkspaces={existingWorkspaces}
126-
createWorkspace={createWorkspace}
127-
onClose={() => {}}
128-
/>
129-
);
130-
}
131119

132120
return (
133121
<div className="flex flex-col mt-32 mx-auto ">
@@ -161,9 +149,15 @@ export function CreateWorkspacePage() {
161149
<div className="w-full flex justify-end mt-6 space-x-2 px-6">
162150
<button
163151
onClick={onClickCreate}
164-
disabled={creating || !repo || repo.length === 0 || !!errorIde || !!errorWsClass}
152+
disabled={
153+
createWorkspaceMutation.isLoading ||
154+
!repo ||
155+
repo.length === 0 ||
156+
!!errorIde ||
157+
!!errorWsClass
158+
}
165159
>
166-
{creating ? (
160+
{createWorkspaceMutation.isLoading ? (
167161
<div className="flex">
168162
<img className="h-4 w-4 animate-spin" src={Spinner} alt="loading spinner" />
169163
<span className="pl-2">Creating Workspace ...</span>
@@ -175,12 +169,19 @@ export function CreateWorkspacePage() {
175169
</div>
176170
<div>
177171
<StatusMessage
178-
error={createError}
172+
error={createWorkspaceMutation.error as StartWorkspaceError}
179173
setSelectAccountError={setSelectAccountError}
180174
createWorkspace={createWorkspace}
181175
/>
182176
</div>
183177
</div>
178+
{existingWorkspaces.length > 0 && (
179+
<ExistingWorkspaceModal
180+
existingWorkspaces={existingWorkspaces}
181+
createWorkspace={createWorkspace}
182+
onClose={() => setExistingWorkspaces([])}
183+
/>
184+
)}
184185
</div>
185186
);
186187
}
@@ -307,8 +308,8 @@ const ExistingWorkspaceModal: FunctionComponent<ExistingWorkspaceModalProps> = (
307308
}) => {
308309
return (
309310
<Modal visible={true} closeable={true} onClose={onClose}>
310-
<h3>Running Workspaces</h3>
311-
<div className="border-t border-b border-gray-200 dark:border-gray-800 mt-4 -mx-6 px-6 py-2">
311+
<ModalHeader>Running Workspaces</ModalHeader>
312+
<ModalBody>
312313
<p className="mt-1 mb-2 text-base">
313314
You already have running workspaces with the same context. You can open an existing one or open a
314315
new workspace.
@@ -335,12 +336,15 @@ const ExistingWorkspaceModal: FunctionComponent<ExistingWorkspaceModalProps> = (
335336
);
336337
})}
337338
</>
338-
</div>
339-
<div className="flex justify-end mt-6">
339+
</ModalBody>
340+
<ModalFooter>
341+
<button className="secondary" onClick={onClose}>
342+
Cancel
343+
</button>
340344
<button onClick={() => createWorkspace({ ignoreRunningWorkspaceOnSameCommit: true })}>
341345
New Workspace
342346
</button>
343-
</div>
347+
</ModalFooter>
344348
</Modal>
345349
);
346350
};

0 commit comments

Comments
 (0)