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-inactive-workspaces-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 DeleteInactiveWorkspacesArgs = {
13
workspaceIds: string[];
14
};
15
export const useDeleteInactiveWorkspacesMutation = () => {
16
const queryClient = useQueryClient();
17
const org = useCurrentOrg();
18
19
return useMutation({
20
mutationFn: async ({ workspaceIds }: DeleteInactiveWorkspacesArgs) => {
21
const deletedWorkspaceIds = [];
22
23
for (const workspaceId of workspaceIds) {
24
try {
25
await workspaceClient.deleteWorkspace({ workspaceId });
26
27
deletedWorkspaceIds.push(workspaceId);
28
} catch (e) {
29
// TODO good candidate for a toast?
30
console.error("Error deleting inactive workspace");
31
}
32
}
33
34
return deletedWorkspaceIds;
35
},
36
onSuccess: (deletedWorkspaceIds) => {
37
const queryKey = getListWorkspacesQueryKey(org.data?.id);
38
39
// Remove deleted workspaces from cache so it's reflected right away
40
// Using the result of the mutationFn so we only remove workspaces that were delete
41
queryClient.setQueryData<ListWorkspacesQueryResult>(queryKey, (oldWorkspacesData) => {
42
return oldWorkspacesData?.filter((info) => {
43
return !deletedWorkspaceIds.includes(info.id);
44
});
45
});
46
47
queryClient.invalidateQueries({ queryKey });
48
},
49
});
50
};
51
52