make checkout version and revert version fit pattern (#118)
This commit is contained in:
38
src/hooks/useCheckoutVersion.ts
Normal file
38
src/hooks/useCheckoutVersion.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { IpcClient } from "@/ipc/ipc_client";
|
||||
|
||||
interface CheckoutVersionVariables {
|
||||
appId: number;
|
||||
versionId: string;
|
||||
}
|
||||
|
||||
export function useCheckoutVersion() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const { isPending: isCheckingOutVersion, mutateAsync: checkoutVersion } =
|
||||
useMutation<void, Error, CheckoutVersionVariables>({
|
||||
mutationFn: async ({ appId, versionId }) => {
|
||||
if (appId === null) {
|
||||
// Should be caught by UI logic before calling, but as a safeguard.
|
||||
throw new Error("App ID is null, cannot checkout version.");
|
||||
}
|
||||
const ipcClient = IpcClient.getInstance();
|
||||
await ipcClient.checkoutVersion({ appId, versionId });
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
// Invalidate queries that depend on the current version/branch
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["currentBranch", variables.appId],
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["versions", variables.appId],
|
||||
});
|
||||
},
|
||||
meta: { showErrorToast: true },
|
||||
});
|
||||
|
||||
return {
|
||||
checkoutVersion,
|
||||
isCheckingOutVersion,
|
||||
};
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
import { useCallback, useEffect } from "react";
|
||||
import { useEffect } from "react";
|
||||
import { useAtom, useAtomValue } from "jotai";
|
||||
import { versionsListAtom } from "@/atoms/appAtoms";
|
||||
import { IpcClient } from "@/ipc/ipc_client";
|
||||
import { showError } from "@/lib/toast";
|
||||
|
||||
import { chatMessagesAtom, selectedChatIdAtom } from "@/atoms/chatAtoms";
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import type { Version } from "@/ipc/ipc_types";
|
||||
@@ -41,40 +41,35 @@ export function useVersions(appId: number | null) {
|
||||
const revertVersionMutation = useMutation<void, Error, { versionId: string }>(
|
||||
{
|
||||
mutationFn: async ({ versionId }: { versionId: string }) => {
|
||||
if (appId === null) {
|
||||
const currentAppId = appId;
|
||||
if (currentAppId === null) {
|
||||
throw new Error("App ID is null");
|
||||
}
|
||||
const ipcClient = IpcClient.getInstance();
|
||||
await ipcClient.revertVersion({ appId, previousVersionId: versionId });
|
||||
await ipcClient.revertVersion({
|
||||
appId: currentAppId,
|
||||
previousVersionId: versionId,
|
||||
});
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ["versions", appId] });
|
||||
await queryClient.invalidateQueries({
|
||||
queryKey: ["currentBranch", appId],
|
||||
});
|
||||
if (selectedChatId) {
|
||||
const chat = await IpcClient.getInstance().getChat(selectedChatId);
|
||||
setMessages(chat.messages);
|
||||
}
|
||||
},
|
||||
onError: (e: Error) => {
|
||||
showError(e);
|
||||
},
|
||||
meta: { showErrorToast: true },
|
||||
},
|
||||
);
|
||||
|
||||
const revertVersion = useCallback(
|
||||
async ({ versionId }: { versionId: string }) => {
|
||||
if (appId === null) {
|
||||
return;
|
||||
}
|
||||
await revertVersionMutation.mutateAsync({ versionId });
|
||||
},
|
||||
[appId, revertVersionMutation],
|
||||
);
|
||||
|
||||
return {
|
||||
versions: versions || [],
|
||||
loading,
|
||||
error,
|
||||
refreshVersions,
|
||||
revertVersion,
|
||||
revertVersion: revertVersionMutation.mutateAsync,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user