Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/data/workspaces/delete-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, useQueryClient } from "@tanstack/react-query";
8
import { workspaceClient } from "../../service/public-api";
9
import { getListWorkspacesQueryKey, ListWorkspacesQueryResult } from "./list-workspaces-query";
10
import { useCurrentOrg } from "../organizations/orgs-query";
11
12
type DeleteWorkspaceArgs = {
13
workspaceId: string;
14
};
15
16
export const useDeleteWorkspaceMutation = () => {
17
const queryClient = useQueryClient();
18
const org = useCurrentOrg();
19
20
return useMutation({
21
mutationFn: async ({ workspaceId }: DeleteWorkspaceArgs) => {
22
return await workspaceClient.deleteWorkspace({ workspaceId });
23
},
24
onSuccess: (_, { workspaceId }) => {
25
const queryKey = getListWorkspacesQueryKey(org.data?.id);
26
27
// Remove workspace from cache so it's reflected right away
28
queryClient.setQueryData<ListWorkspacesQueryResult>(queryKey, (oldWorkspacesData) => {
29
return oldWorkspacesData?.filter((info) => {
30
return info.id !== workspaceId;
31
});
32
});
33
34
queryClient.invalidateQueries({ queryKey });
35
},
36
});
37
};
38
39