migrate current branch to query pattern (#116)

This commit is contained in:
Will Chen
2025-05-08 22:23:00 -07:00
committed by GitHub
parent 7839d6bde9
commit b6eeaab1bb
5 changed files with 65 additions and 77 deletions

View File

@@ -0,0 +1,30 @@
import { IpcClient } from "@/ipc/ipc_client";
import { useQuery } from "@tanstack/react-query";
import type { BranchResult } from "@/ipc/ipc_types";
export function useCurrentBranch(appId: number | null) {
const {
data: branchInfo,
isLoading,
refetch: refetchBranchInfo,
} = useQuery<BranchResult, Error>({
queryKey: ["currentBranch", appId],
queryFn: async (): Promise<BranchResult> => {
if (appId === null) {
// This case should ideally be handled by the `enabled` option
// but as a safeguard, and to ensure queryFn always has a valid appId if called.
throw new Error("appId is null, cannot fetch current branch.");
}
const ipcClient = IpcClient.getInstance();
return ipcClient.getCurrentBranch(appId);
},
enabled: appId !== null,
meta: { showErrorToast: false },
});
return {
branchInfo,
isLoading,
refetchBranchInfo,
};
}