Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/workspaces/create-workspace-mutation.ts
2501 views
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 { useMutation } from "@tanstack/react-query";
8
import { useState } from "react";
9
import { workspaceClient } from "../../service/public-api";
10
import {
11
CreateAndStartWorkspaceRequest,
12
CreateAndStartWorkspaceResponse,
13
} from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";
14
import { PartialMessage } from "@bufbuild/protobuf";
15
import { ConnectError } from "@connectrpc/connect";
16
17
export const useCreateWorkspaceMutation = () => {
18
const [isStarting, setIsStarting] = useState(false);
19
const mutation = useMutation<
20
CreateAndStartWorkspaceResponse,
21
ConnectError,
22
PartialMessage<CreateAndStartWorkspaceRequest>
23
>({
24
mutationFn: async (options) => {
25
return await workspaceClient.createAndStartWorkspace(options);
26
},
27
onMutate: async (options: PartialMessage<CreateAndStartWorkspaceRequest>) => {
28
setIsStarting(true);
29
},
30
onError: (error) => {
31
setIsStarting(false);
32
},
33
onSuccess: (result) => {
34
if (result.workspace?.id) {
35
// successfully started a workspace, wait a bit before we allow to start another one
36
setTimeout(() => {
37
setIsStarting(false);
38
}, 4000);
39
} else {
40
setIsStarting(false);
41
}
42
},
43
});
44
return {
45
createWorkspace: (options: PartialMessage<CreateAndStartWorkspaceRequest>) => {
46
return mutation.mutateAsync(options);
47
},
48
// Can we use mutation.isLoading here instead?
49
isStarting,
50
error: mutation.error,
51
reset: mutation.reset,
52
};
53
};
54
55