Path: blob/main/components/dashboard/src/data/workspaces/delete-inactive-workspaces-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, useQueryClient } from "@tanstack/react-query";7import { workspaceClient } from "../../service/public-api";8import { getListWorkspacesQueryKey, ListWorkspacesQueryResult } from "./list-workspaces-query";9import { useCurrentOrg } from "../organizations/orgs-query";1011type DeleteInactiveWorkspacesArgs = {12workspaceIds: string[];13};14export const useDeleteInactiveWorkspacesMutation = () => {15const queryClient = useQueryClient();16const org = useCurrentOrg();1718return useMutation({19mutationFn: async ({ workspaceIds }: DeleteInactiveWorkspacesArgs) => {20const deletedWorkspaceIds = [];2122for (const workspaceId of workspaceIds) {23try {24await workspaceClient.deleteWorkspace({ workspaceId });2526deletedWorkspaceIds.push(workspaceId);27} catch (e) {28// TODO good candidate for a toast?29console.error("Error deleting inactive workspace");30}31}3233return deletedWorkspaceIds;34},35onSuccess: (deletedWorkspaceIds) => {36const queryKey = getListWorkspacesQueryKey(org.data?.id);3738// Remove deleted workspaces from cache so it's reflected right away39// Using the result of the mutationFn so we only remove workspaces that were delete40queryClient.setQueryData<ListWorkspacesQueryResult>(queryKey, (oldWorkspacesData) => {41return oldWorkspacesData?.filter((info) => {42return !deletedWorkspaceIds.includes(info.id);43});44});4546queryClient.invalidateQueries({ queryKey });47},48});49};505152