feat(workspaces): implement hook for creating workspaces with success/error notifications

This commit is contained in:
ngc2207 2025-02-03 12:12:53 +08:00
parent e96f111a39
commit 0084b56df6

View File

@ -0,0 +1,35 @@
import { toast } from "sonner";
import { client } from "@/lib/rpc";
import { InferRequestType, InferResponseType } from "hono";
import { useMutation, useQueryClient } from "@tanstack/react-query";
type ResponseType = InferResponseType<(typeof client.api.workspaces)["$post"]>;
type RequestType = InferRequestType<(typeof client.api.workspaces)["$post"]>;
export const useCreateWorkspace = () => {
const queryClient = useQueryClient();
const mutation = useMutation<ResponseType, Error, RequestType>({
mutationFn: async ({ json }) => {
const response = await client.api.workspaces["$post"]({
json,
});
if (!response.ok) {
throw new Error("Failed to create workspace");
}
return await response.json();
},
onSuccess: () => {
toast.success("Workspace created successfully");
queryClient.invalidateQueries({ queryKey: ["workspaces"] });
},
onError: () => {
toast.error("Failed to create workspace");
},
});
return mutation;
};