Path: blob/main/components/dashboard/src/data/workspaces/create-workspace-mutation.ts
2501 views
/**1* Copyright (c) 2023 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { useMutation } from "@tanstack/react-query";7import { useState } from "react";8import { workspaceClient } from "../../service/public-api";9import {10CreateAndStartWorkspaceRequest,11CreateAndStartWorkspaceResponse,12} from "@gitpod/public-api/lib/gitpod/v1/workspace_pb";13import { PartialMessage } from "@bufbuild/protobuf";14import { ConnectError } from "@connectrpc/connect";1516export const useCreateWorkspaceMutation = () => {17const [isStarting, setIsStarting] = useState(false);18const mutation = useMutation<19CreateAndStartWorkspaceResponse,20ConnectError,21PartialMessage<CreateAndStartWorkspaceRequest>22>({23mutationFn: async (options) => {24return await workspaceClient.createAndStartWorkspace(options);25},26onMutate: async (options: PartialMessage<CreateAndStartWorkspaceRequest>) => {27setIsStarting(true);28},29onError: (error) => {30setIsStarting(false);31},32onSuccess: (result) => {33if (result.workspace?.id) {34// successfully started a workspace, wait a bit before we allow to start another one35setTimeout(() => {36setIsStarting(false);37}, 4000);38} else {39setIsStarting(false);40}41},42});43return {44createWorkspace: (options: PartialMessage<CreateAndStartWorkspaceRequest>) => {45return mutation.mutateAsync(options);46},47// Can we use mutation.isLoading here instead?48isStarting,49error: mutation.error,50reset: mutation.reset,51};52};535455