Rename branch (#199)

This is more important now that import app is available and not every
git repo will be initialized with `main` as the default branch. This
handles the other common case which is the `master` branch.
This commit is contained in:
Will Chen
2025-05-19 14:41:18 -07:00
committed by GitHub
parent bc8166c274
commit cbfe26bac1
6 changed files with 154 additions and 10 deletions

View File

@@ -0,0 +1,58 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { IpcClient } from "@/ipc/ipc_client";
import { showError } from "@/lib/toast";
import { selectedAppIdAtom } from "@/atoms/appAtoms";
import { useAtomValue } from "jotai";
interface RenameBranchParams {
appId: number;
oldBranchName: string;
newBranchName: string;
}
export function useRenameBranch() {
const queryClient = useQueryClient();
const currentAppId = useAtomValue(selectedAppIdAtom);
const mutation = useMutation<void, Error, RenameBranchParams>({
mutationFn: async (params: RenameBranchParams) => {
if (params.appId === null || params.appId === undefined) {
throw new Error("App ID is required to rename a branch.");
}
if (!params.oldBranchName) {
throw new Error("Old branch name is required.");
}
if (!params.newBranchName) {
throw new Error("New branch name is required.");
}
await IpcClient.getInstance().renameBranch(params);
},
onSuccess: (_, variables) => {
// Invalidate queries that depend on branch information
queryClient.invalidateQueries({
queryKey: ["currentBranch", variables.appId],
});
queryClient.invalidateQueries({
queryKey: ["versions", variables.appId],
});
// Potentially show a success message or trigger other actions
},
meta: {
showErrorToast: true,
},
});
const renameBranch = async (params: Omit<RenameBranchParams, "appId">) => {
if (!currentAppId) {
showError("No application selected.");
return;
}
return mutation.mutateAsync({ ...params, appId: currentAppId });
};
return {
renameBranch,
isRenamingBranch: mutation.isPending,
renameBranchError: mutation.error,
};
}