make checkout version and revert version fit pattern (#118)

This commit is contained in:
Will Chen
2025-05-08 23:23:24 -07:00
committed by GitHub
parent 8d61659c60
commit c203b1d009
9 changed files with 161 additions and 92 deletions

View File

@@ -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,
};
}