Refactor useVersions to @tanstack/react-query (#114)

working
This commit is contained in:
Will Chen
2025-05-08 22:04:20 -07:00
committed by GitHub
parent cb9ffcc550
commit 34ac9df743
5 changed files with 105 additions and 68 deletions

27
package-lock.json generated
View File

@@ -32,6 +32,7 @@
"@rollup/plugin-commonjs": "^28.0.3", "@rollup/plugin-commonjs": "^28.0.3",
"@tailwindcss/typography": "^0.5.16", "@tailwindcss/typography": "^0.5.16",
"@tailwindcss/vite": "^4.1.3", "@tailwindcss/vite": "^4.1.3",
"@tanstack/react-query": "^5.75.5",
"@tanstack/react-router": "^1.114.34", "@tanstack/react-router": "^1.114.34",
"@types/uuid": "^10.0.0", "@types/uuid": "^10.0.0",
"@vitejs/plugin-react": "^4.3.4", "@vitejs/plugin-react": "^4.3.4",
@@ -5361,6 +5362,32 @@
"url": "https://github.com/sponsors/tannerlinsley" "url": "https://github.com/sponsors/tannerlinsley"
} }
}, },
"node_modules/@tanstack/query-core": {
"version": "5.75.5",
"resolved": "https://registry.npmjs.org/@tanstack/query-core/-/query-core-5.75.5.tgz",
"integrity": "sha512-kPDOxtoMn2Ycycb76Givx2fi+2pzo98F9ifHL/NFiahEDpDwSVW6o12PRuQ0lQnBOunhRG5etatAhQij91M3MQ==",
"license": "MIT",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
}
},
"node_modules/@tanstack/react-query": {
"version": "5.75.5",
"resolved": "https://registry.npmjs.org/@tanstack/react-query/-/react-query-5.75.5.tgz",
"integrity": "sha512-QrLCJe40BgBVlWdAdf2ZEVJ0cISOuEy/HKupId1aTKU6gPJZVhSvZpH+Si7csRflCJphzlQ77Yx6gUxGW9o0XQ==",
"license": "MIT",
"dependencies": {
"@tanstack/query-core": "5.75.5"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/tannerlinsley"
},
"peerDependencies": {
"react": "^18 || ^19"
}
},
"node_modules/@tanstack/react-router": { "node_modules/@tanstack/react-router": {
"version": "1.119.0", "version": "1.119.0",
"resolved": "https://registry.npmjs.org/@tanstack/react-router/-/react-router-1.119.0.tgz", "resolved": "https://registry.npmjs.org/@tanstack/react-router/-/react-router-1.119.0.tgz",

View File

@@ -95,6 +95,7 @@
"@rollup/plugin-commonjs": "^28.0.3", "@rollup/plugin-commonjs": "^28.0.3",
"@tailwindcss/typography": "^0.5.16", "@tailwindcss/typography": "^0.5.16",
"@tailwindcss/vite": "^4.1.3", "@tailwindcss/vite": "^4.1.3",
"@tanstack/react-query": "^5.75.5",
"@tanstack/react-router": "^1.114.34", "@tanstack/react-router": "^1.114.34",
"@types/uuid": "^10.0.0", "@types/uuid": "^10.0.0",
"@vitejs/plugin-react": "^4.3.4", "@vitejs/plugin-react": "^4.3.4",

View File

@@ -21,17 +21,20 @@ export function VersionPane({ isVisible, onClose }: VersionPaneProps) {
selectedVersionIdAtom, selectedVersionIdAtom,
); );
useEffect(() => { useEffect(() => {
// Refresh versions in case the user updated versions outside of the app async function updateVersions() {
// (e.g. manually using git). // Refresh versions in case the user updated versions outside of the app
// Avoid loading state which causes brief flash of loading state. // (e.g. manually using git).
refreshVersions(); // Avoid loading state which causes brief flash of loading state.
if (!isVisible && selectedVersionId) { if (!isVisible && selectedVersionId) {
setSelectedVersionId(null); setSelectedVersionId(null);
IpcClient.getInstance().checkoutVersion({ await IpcClient.getInstance().checkoutVersion({
appId: appId!, appId: appId!,
versionId: "main", versionId: "main",
}); });
}
refreshVersions();
} }
updateVersions();
}, [isVisible, refreshVersions]); }, [isVisible, refreshVersions]);
if (!isVisible) { if (!isVisible) {
return null; return null;

View File

@@ -1,78 +1,79 @@
import { useState, useEffect, useCallback } from "react"; import { useCallback, useEffect } from "react";
import { useAtom, useAtomValue } from "jotai"; import { useAtom, useAtomValue } from "jotai";
import { versionsListAtom } from "@/atoms/appAtoms"; import { versionsListAtom } from "@/atoms/appAtoms";
import { IpcClient } from "@/ipc/ipc_client"; import { IpcClient } from "@/ipc/ipc_client";
import { showError } from "@/lib/toast"; import { showError } from "@/lib/toast";
import { chatMessagesAtom, selectedChatIdAtom } from "@/atoms/chatAtoms"; import { chatMessagesAtom, selectedChatIdAtom } from "@/atoms/chatAtoms";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import type { Version } from "@/ipc/ipc_types";
export function useVersions(appId: number | null) { export function useVersions(appId: number | null) {
const [versions, setVersions] = useAtom(versionsListAtom); const [, setVersionsAtom] = useAtom(versionsListAtom);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<Error | null>(null);
const selectedChatId = useAtomValue(selectedChatIdAtom); const selectedChatId = useAtomValue(selectedChatIdAtom);
const [, setMessages] = useAtom(chatMessagesAtom); const [, setMessages] = useAtom(chatMessagesAtom);
useEffect(() => { const queryClient = useQueryClient();
const loadVersions = async () => {
// If no app is selected, clear versions and return const {
data: versions,
isLoading: loading,
error,
refetch: refreshVersions,
} = useQuery<Version[], Error>({
queryKey: ["versions", appId],
queryFn: async (): Promise<Version[]> => {
if (appId === null) { if (appId === null) {
setVersions([]); return [];
setLoading(false);
return;
} }
try {
const ipcClient = IpcClient.getInstance();
const versionsList = await ipcClient.listVersions({ appId });
setVersions(versionsList);
setError(null);
} catch (error) {
console.error("Error loading versions:", error);
setError(error instanceof Error ? error : new Error(String(error)));
} finally {
setLoading(false);
}
};
loadVersions();
}, [appId, setVersions]);
const refreshVersions = useCallback(async () => {
if (appId === null) {
return;
}
try {
const ipcClient = IpcClient.getInstance(); const ipcClient = IpcClient.getInstance();
const versionsList = await ipcClient.listVersions({ appId }); return ipcClient.listVersions({ appId });
setVersions(versionsList); },
setError(null); enabled: appId !== null,
} catch (error) { initialData: [],
console.error("Error refreshing versions:", error); });
setError(error instanceof Error ? error : new Error(String(error)));
useEffect(() => {
if (versions) {
setVersionsAtom(versions);
} }
}, [appId, setVersions, setError]); }, [versions, setVersionsAtom]);
const revertVersionMutation = useMutation<void, Error, { versionId: string }>(
{
mutationFn: async ({ versionId }: { versionId: string }) => {
if (appId === null) {
throw new Error("App ID is null");
}
const ipcClient = IpcClient.getInstance();
await ipcClient.revertVersion({ appId, previousVersionId: versionId });
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["versions", appId] });
if (selectedChatId) {
const chat = await IpcClient.getInstance().getChat(selectedChatId);
setMessages(chat.messages);
}
},
onError: (e: Error) => {
showError(e);
},
},
);
const revertVersion = useCallback( const revertVersion = useCallback(
async ({ versionId }: { versionId: string }) => { async ({ versionId }: { versionId: string }) => {
if (appId === null) { if (appId === null) {
return; return;
} }
await revertVersionMutation.mutateAsync({ versionId });
try {
const ipcClient = IpcClient.getInstance();
await ipcClient.revertVersion({ appId, previousVersionId: versionId });
await refreshVersions();
if (selectedChatId) {
const chat = await IpcClient.getInstance().getChat(selectedChatId);
setMessages(chat.messages);
}
} catch (error) {
showError(error);
}
}, },
[appId, setVersions, setError, selectedChatId, setMessages], [appId, revertVersionMutation],
); );
return { versions, loading, error, refreshVersions, revertVersion }; return {
versions: versions || [],
loading,
error,
refreshVersions,
revertVersion,
};
} }

View File

@@ -5,10 +5,13 @@ import { RouterProvider } from "@tanstack/react-router";
import { PostHogProvider } from "posthog-js/react"; import { PostHogProvider } from "posthog-js/react";
import posthog from "posthog-js"; import posthog from "posthog-js";
import { getTelemetryUserId, isTelemetryOptedIn } from "./hooks/useSettings"; import { getTelemetryUserId, isTelemetryOptedIn } from "./hooks/useSettings";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
// @ts-ignore // @ts-ignore
console.log("Running in mode:", import.meta.env.MODE); console.log("Running in mode:", import.meta.env.MODE);
const queryClient = new QueryClient();
const posthogClient = posthog.init( const posthogClient = posthog.init(
"phc_5Vxx0XT8Ug3eWROhP6mm4D6D2DgIIKT232q4AKxC2ab", "phc_5Vxx0XT8Ug3eWROhP6mm4D6D2DgIIKT232q4AKxC2ab",
{ {
@@ -71,8 +74,10 @@ function App() {
createRoot(document.getElementById("root")!).render( createRoot(document.getElementById("root")!).render(
<StrictMode> <StrictMode>
<PostHogProvider client={posthogClient}> <QueryClientProvider client={queryClient}>
<App /> <PostHogProvider client={posthogClient}>
</PostHogProvider> <App />
</PostHogProvider>
</QueryClientProvider>
</StrictMode>, </StrictMode>,
); );