Keep git review state fresh after reconnect (#1491)
This commit is contained in:
@@ -1,14 +1,8 @@
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useEffect } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useHostRuntimeClient, useHostRuntimeIsConnected } from "@/runtime/host-runtime";
|
||||
import { checkoutStatusQueryKey } from "@/git/query-keys";
|
||||
import {
|
||||
applyCheckoutStatusUpdate,
|
||||
type CheckoutStatusClient,
|
||||
type CheckoutStatusPayload,
|
||||
peekOrFetchCheckoutStatus,
|
||||
} from "./checkout-status-cache";
|
||||
import { fetchCheckoutStatus } from "./checkout-status-cache";
|
||||
|
||||
export type { CheckoutStatusPayload } from "./checkout-status-cache";
|
||||
|
||||
@@ -19,40 +13,25 @@ interface UseCheckoutStatusQueryOptions {
|
||||
cwd: string;
|
||||
}
|
||||
|
||||
function fetchCheckoutStatus(
|
||||
client: CheckoutStatusClient,
|
||||
cwd: string,
|
||||
): Promise<CheckoutStatusPayload> {
|
||||
return client.getCheckoutStatus(cwd);
|
||||
}
|
||||
|
||||
export function useCheckoutStatusQuery({ serverId, cwd }: UseCheckoutStatusQueryOptions) {
|
||||
const { t } = useTranslation();
|
||||
const queryClient = useQueryClient();
|
||||
const client = useHostRuntimeClient(serverId);
|
||||
const isConnected = useHostRuntimeIsConnected(serverId);
|
||||
|
||||
useEffect(() => {
|
||||
if (!client || !isConnected || !cwd) {
|
||||
return;
|
||||
}
|
||||
|
||||
return client.on("checkout_status_update", (message) => {
|
||||
applyCheckoutStatusUpdate({ queryClient, serverId, cwd, message });
|
||||
});
|
||||
}, [client, isConnected, cwd, queryClient, serverId]);
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: checkoutStatusQueryKey(serverId, cwd),
|
||||
queryFn: async () => {
|
||||
if (!client) {
|
||||
throw new Error(t("common.errors.daemonClientUnavailable"));
|
||||
}
|
||||
return await peekOrFetchCheckoutStatus({ queryClient, client, serverId, cwd });
|
||||
return await fetchCheckoutStatus({ client, serverId, cwd });
|
||||
},
|
||||
enabled: !!client && isConnected && !!cwd,
|
||||
staleTime: Infinity,
|
||||
refetchOnMount: false,
|
||||
// Freshness is push-driven (checkout_status_update applied globally); with
|
||||
// staleTime: Infinity, refetchOnMount only fires after an explicit invalidation
|
||||
// (e.g. reconnect), which is exactly when the push stream may have been missed.
|
||||
refetchOnMount: true,
|
||||
refetchOnReconnect: false,
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
@@ -81,7 +60,7 @@ export function useCheckoutStatusCacheOnly({ serverId, cwd }: UseCheckoutStatusQ
|
||||
if (!client) {
|
||||
throw new Error(t("common.errors.daemonClientUnavailable"));
|
||||
}
|
||||
return await fetchCheckoutStatus(client, cwd);
|
||||
return await fetchCheckoutStatus({ client, serverId, cwd });
|
||||
},
|
||||
enabled: false,
|
||||
staleTime: CHECKOUT_STATUS_STALE_TIME,
|
||||
|
||||
Reference in New Issue
Block a user