Centralizes refresh orchestration in WorkspaceGitService and GitHubService: server coalesces, deduplicates, and throttles git/GitHub reads; clients peek snapshots (cold reads fall through to a one-shot fetch) and never drive refreshes. Introduces a two-TTL model (consumer vs internal), force-emit bypass so fingerprint-matched refreshes still propagate, notifyGitMutation wiring at ~13 action handlers, adaptive GitHub polling based on check state, and checkout_status_update / prStatus push-to-cache wire messages. Fixes the stale diff-stat bug and the inverted base-branch diff direction, and flips next-action affordances promptly after commit / PR merge.
158 lines
4.4 KiB
TypeScript
158 lines
4.4 KiB
TypeScript
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import { useEffect } from "react";
|
|
import { useHostRuntimeClient, useHostRuntimeIsConnected } from "@/runtime/host-runtime";
|
|
import type { CheckoutPrStatusResponse } from "@server/shared/messages";
|
|
|
|
export function checkoutPrStatusQueryKey(serverId: string, cwd: string) {
|
|
return ["checkoutPrStatus", serverId, cwd] as const;
|
|
}
|
|
|
|
interface UseCheckoutPrStatusQueryOptions {
|
|
serverId: string;
|
|
cwd: string;
|
|
enabled?: boolean;
|
|
}
|
|
|
|
export type CheckoutPrStatusPayload = CheckoutPrStatusResponse["payload"];
|
|
|
|
export interface PrHint {
|
|
url: string;
|
|
number: number;
|
|
state: "open" | "merged" | "closed";
|
|
checks?: Array<{ name: string; status: string; url: string | null }>;
|
|
checksStatus?: "none" | "pending" | "success" | "failure";
|
|
reviewDecision?: "approved" | "changes_requested" | "pending" | null;
|
|
}
|
|
|
|
function parsePullRequestNumber(url: string): number | null {
|
|
try {
|
|
const pathname = new URL(url).pathname;
|
|
const match = pathname.match(/\/pull\/(\d+)(?:\/|$)/);
|
|
if (!match) {
|
|
return null;
|
|
}
|
|
|
|
const number = Number.parseInt(match[1], 10);
|
|
return Number.isFinite(number) ? number : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function selectWorkspacePrHint(payload: CheckoutPrStatusPayload): PrHint | null {
|
|
const status = payload.status;
|
|
if (!status?.url) {
|
|
return null;
|
|
}
|
|
|
|
const number = parsePullRequestNumber(status.url);
|
|
if (number === null) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
url: status.url,
|
|
number,
|
|
state:
|
|
status.isMerged || status.state === "merged"
|
|
? "merged"
|
|
: status.state === "open"
|
|
? "open"
|
|
: "closed",
|
|
checks: status.checks,
|
|
checksStatus: status.checksStatus as PrHint["checksStatus"],
|
|
reviewDecision: status.reviewDecision as PrHint["reviewDecision"],
|
|
};
|
|
}
|
|
|
|
export function useCheckoutPrStatusQuery({
|
|
serverId,
|
|
cwd,
|
|
enabled = true,
|
|
}: UseCheckoutPrStatusQueryOptions) {
|
|
const queryClient = useQueryClient();
|
|
const client = useHostRuntimeClient(serverId);
|
|
const isConnected = useHostRuntimeIsConnected(serverId);
|
|
|
|
useEffect(() => {
|
|
if (!client || !isConnected || !cwd) {
|
|
return;
|
|
}
|
|
|
|
return client.on("checkout_status_update", (message) => {
|
|
const prStatus = message.payload.prStatus;
|
|
if (!prStatus || prStatus.cwd !== cwd) {
|
|
return;
|
|
}
|
|
queryClient.setQueryData(checkoutPrStatusQueryKey(serverId, cwd), prStatus);
|
|
});
|
|
}, [client, isConnected, cwd, queryClient, serverId]);
|
|
|
|
const query = useQuery({
|
|
queryKey: checkoutPrStatusQueryKey(serverId, cwd),
|
|
queryFn: async () => {
|
|
if (!client) {
|
|
throw new Error("Daemon client not available");
|
|
}
|
|
return await client.checkoutPrStatus(cwd);
|
|
},
|
|
enabled: !!client && isConnected && !!cwd && enabled,
|
|
staleTime: Infinity,
|
|
refetchOnMount: false,
|
|
refetchOnReconnect: false,
|
|
refetchOnWindowFocus: false,
|
|
});
|
|
|
|
return {
|
|
status: query.data?.status ?? null,
|
|
githubFeaturesEnabled: query.data?.githubFeaturesEnabled ?? true,
|
|
payloadError: query.data?.error ?? null,
|
|
isLoading: query.isLoading,
|
|
isFetching: query.isFetching,
|
|
isError: query.isError,
|
|
error: query.error,
|
|
};
|
|
}
|
|
|
|
export function useWorkspacePrHint({
|
|
serverId,
|
|
cwd,
|
|
enabled = true,
|
|
}: UseCheckoutPrStatusQueryOptions): PrHint | null {
|
|
const queryClient = useQueryClient();
|
|
const client = useHostRuntimeClient(serverId);
|
|
const isConnected = useHostRuntimeIsConnected(serverId);
|
|
|
|
useEffect(() => {
|
|
if (!client || !isConnected || !cwd) {
|
|
return;
|
|
}
|
|
|
|
return client.on("checkout_status_update", (message) => {
|
|
const prStatus = message.payload.prStatus;
|
|
if (!prStatus || prStatus.cwd !== cwd) {
|
|
return;
|
|
}
|
|
queryClient.setQueryData(checkoutPrStatusQueryKey(serverId, cwd), prStatus);
|
|
});
|
|
}, [client, isConnected, cwd, queryClient, serverId]);
|
|
|
|
const query = useQuery<CheckoutPrStatusPayload, Error, PrHint | null>({
|
|
queryKey: checkoutPrStatusQueryKey(serverId, cwd),
|
|
queryFn: async () => {
|
|
if (!client) {
|
|
throw new Error("Daemon client not available");
|
|
}
|
|
return await client.checkoutPrStatus(cwd);
|
|
},
|
|
enabled: !!client && isConnected && !!cwd && enabled,
|
|
staleTime: Infinity,
|
|
refetchOnMount: false,
|
|
refetchOnReconnect: false,
|
|
refetchOnWindowFocus: false,
|
|
select: selectWorkspacePrHint,
|
|
});
|
|
|
|
return query.data ?? null;
|
|
}
|