consolidate checkout status handling and improve git detection
This commit is contained in:
@@ -12,7 +12,6 @@ import { useRouter } from "expo-router";
|
||||
import { useFocusEffect } from "@react-navigation/native";
|
||||
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||
import { useReanimatedKeyboardAnimation } from "react-native-keyboard-controller";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import ReanimatedAnimated, {
|
||||
useAnimatedStyle,
|
||||
useSharedValue,
|
||||
@@ -72,9 +71,6 @@ import {
|
||||
const DROPDOWN_WIDTH = 220;
|
||||
const EMPTY_STREAM_ITEMS: StreamItem[] = [];
|
||||
|
||||
|
||||
type BranchStatus = "idle" | "loading" | "ready" | "error";
|
||||
|
||||
export function AgentReadyScreen({
|
||||
serverId,
|
||||
agentId,
|
||||
@@ -372,43 +368,6 @@ function AgentScreenContent({
|
||||
const agentModel = extractAgentModel(agent);
|
||||
const modelDisplayValue = agentModel ?? "Unknown";
|
||||
|
||||
const repoInfoQuery = useQuery({
|
||||
queryKey: ["checkoutStatus", serverId, agent?.cwd ?? ""],
|
||||
queryFn: async () => {
|
||||
if (!client) {
|
||||
throw new Error("Daemon client unavailable");
|
||||
}
|
||||
const payload = await client.getCheckoutStatus(agent?.cwd ?? ".");
|
||||
if (payload.error) {
|
||||
throw new Error(payload.error.message);
|
||||
}
|
||||
return {
|
||||
cwd: payload.cwd,
|
||||
currentBranch: payload.currentBranch ?? null,
|
||||
};
|
||||
},
|
||||
enabled: Boolean(client && isConnected && agent?.cwd),
|
||||
retry: false,
|
||||
});
|
||||
const { refetch: refetchRepoInfo } = repoInfoQuery;
|
||||
const branchStatus: BranchStatus = !agent?.cwd
|
||||
? "idle"
|
||||
: repoInfoQuery.isPending || repoInfoQuery.isFetching
|
||||
? "loading"
|
||||
: repoInfoQuery.isError
|
||||
? "error"
|
||||
: repoInfoQuery.isSuccess
|
||||
? "ready"
|
||||
: "idle";
|
||||
const branchLabel = repoInfoQuery.data?.currentBranch ?? null;
|
||||
const branchError = repoInfoQuery.error instanceof Error
|
||||
? repoInfoQuery.error.message
|
||||
: null;
|
||||
const branchDisplayValue =
|
||||
branchStatus === "error"
|
||||
? branchError ?? "Unavailable"
|
||||
: branchLabel ?? "Unknown";
|
||||
|
||||
// Checkout status for header subtitle
|
||||
const checkoutStatusQuery = useCheckoutStatusQuery({
|
||||
serverId,
|
||||
@@ -648,7 +607,7 @@ function AgentScreenContent({
|
||||
<DropdownMenu
|
||||
onOpenChange={(open) => {
|
||||
if (open && agent?.cwd) {
|
||||
refetchRepoInfo().catch(() => {});
|
||||
checkoutStatusQuery.refresh().catch(() => {});
|
||||
}
|
||||
}}
|
||||
>
|
||||
@@ -679,31 +638,30 @@ function AgentScreenContent({
|
||||
</Text>
|
||||
</View>
|
||||
|
||||
<View style={styles.menuMetaRow}>
|
||||
<Text style={styles.menuMetaLabel}>Branch</Text>
|
||||
<View style={styles.menuMetaValueRow}>
|
||||
{branchStatus === "loading" ? (
|
||||
<>
|
||||
<ActivityIndicator
|
||||
size="small"
|
||||
color={theme.colors.foregroundMuted}
|
||||
/>
|
||||
<Text style={styles.menuMetaPendingText}>Fetching…</Text>
|
||||
</>
|
||||
) : (
|
||||
<Text
|
||||
style={[
|
||||
styles.menuMetaValue,
|
||||
branchStatus === "error" ? styles.menuMetaValueError : null,
|
||||
]}
|
||||
numberOfLines={1}
|
||||
ellipsizeMode="middle"
|
||||
>
|
||||
{branchDisplayValue}
|
||||
</Text>
|
||||
)}
|
||||
{checkout?.isGit && checkout.currentBranch && checkout.currentBranch !== "HEAD" ? (
|
||||
<View style={styles.menuMetaRow}>
|
||||
<Text style={styles.menuMetaLabel}>Branch</Text>
|
||||
<View style={styles.menuMetaValueRow}>
|
||||
{checkoutStatusQuery.isFetching ? (
|
||||
<>
|
||||
<ActivityIndicator
|
||||
size="small"
|
||||
color={theme.colors.foregroundMuted}
|
||||
/>
|
||||
<Text style={styles.menuMetaPendingText}>Fetching…</Text>
|
||||
</>
|
||||
) : (
|
||||
<Text
|
||||
style={styles.menuMetaValue}
|
||||
numberOfLines={1}
|
||||
ellipsizeMode="middle"
|
||||
>
|
||||
{checkout.currentBranch}
|
||||
</Text>
|
||||
)}
|
||||
</View>
|
||||
</View>
|
||||
</View>
|
||||
) : null}
|
||||
</View>
|
||||
|
||||
<DropdownMenuSeparator />
|
||||
|
||||
@@ -27,6 +27,10 @@ import {
|
||||
import { FileDropZone } from "@/components/file-drop-zone";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useAgentFormState, type CreateAgentInitialValues } from "@/hooks/use-agent-form-state";
|
||||
import {
|
||||
CHECKOUT_STATUS_STALE_TIME,
|
||||
checkoutStatusQueryKey,
|
||||
} from "@/hooks/use-checkout-status-query";
|
||||
import { useDaemonConnections } from "@/contexts/daemon-connections-context";
|
||||
import { useDaemonRegistry } from "@/contexts/daemon-registry-context";
|
||||
import { formatConnectionStatus } from "@/utils/daemons";
|
||||
@@ -309,82 +313,62 @@ export function DraftAgentScreen({
|
||||
"Repository details will load automatically once the selected host is back online."
|
||||
: null;
|
||||
|
||||
type RepoInfoState = {
|
||||
cwd: string;
|
||||
repoRoot: string;
|
||||
currentBranch: string | null;
|
||||
isDirty: boolean;
|
||||
};
|
||||
const repoInfoQuery = useQuery({
|
||||
queryKey: ["checkoutStatus", selectedServerId, trimmedWorkingDir],
|
||||
const checkoutStatusQuery = useQuery({
|
||||
queryKey: checkoutStatusQueryKey(selectedServerId ?? "", trimmedWorkingDir),
|
||||
queryFn: async () => {
|
||||
const client = sessionClient;
|
||||
if (!client) {
|
||||
throw new Error("Daemon client unavailable");
|
||||
}
|
||||
const payload = await client.getCheckoutStatus(trimmedWorkingDir || ".");
|
||||
if (payload.error) {
|
||||
throw new Error(payload.error.message);
|
||||
}
|
||||
if (!payload.isGit) {
|
||||
throw new Error("Not a git repository");
|
||||
}
|
||||
// After the isGit check, TypeScript knows we have a git repo
|
||||
return {
|
||||
cwd: payload.cwd,
|
||||
repoRoot: payload.repoRoot,
|
||||
currentBranch: payload.currentBranch,
|
||||
isDirty: payload.isDirty,
|
||||
};
|
||||
return await client.getCheckoutStatus(trimmedWorkingDir);
|
||||
},
|
||||
enabled:
|
||||
Boolean(selectedServerId) &&
|
||||
Boolean(trimmedWorkingDir) &&
|
||||
!repoAvailabilityError &&
|
||||
Boolean(sessionClient) &&
|
||||
isConnected,
|
||||
retry: false,
|
||||
staleTime: CHECKOUT_STATUS_STALE_TIME,
|
||||
refetchOnMount: "always",
|
||||
});
|
||||
const repoInfo = repoInfoQuery.data ?? null;
|
||||
const refetchRepoInfo = repoInfoQuery.refetch;
|
||||
const repoRequestError = repoInfoQuery.error as Error | null;
|
||||
const repoRequestStatus: "idle" | "loading" | "success" | "error" =
|
||||
!shouldInspectRepo || repoAvailabilityError
|
||||
? "idle"
|
||||
: repoInfoQuery.isPending || repoInfoQuery.isFetching
|
||||
? "loading"
|
||||
: repoInfoQuery.isError
|
||||
? "error"
|
||||
: repoInfoQuery.isSuccess
|
||||
? "success"
|
||||
: "idle";
|
||||
|
||||
const checkout = checkoutStatusQuery.data ?? null;
|
||||
const refetchCheckoutStatus = checkoutStatusQuery.refetch;
|
||||
const checkoutQueryError =
|
||||
checkoutStatusQuery.error instanceof Error ? checkoutStatusQuery.error.message : null;
|
||||
const checkoutPayloadError = checkout?.error ? checkout.error.message : null;
|
||||
|
||||
const isNonGitDirectory =
|
||||
repoRequestStatus === "error" &&
|
||||
/not in a git repository|not a git repository/i.test(repoRequestError?.message ?? "");
|
||||
Boolean(trimmedWorkingDir) &&
|
||||
checkoutStatusQuery.isSuccess &&
|
||||
checkout?.isGit === false &&
|
||||
checkout?.error == null;
|
||||
|
||||
const isDirectoryNotExists =
|
||||
repoRequestStatus === "error" &&
|
||||
/does not exist|no such file or directory|ENOENT/i.test(repoRequestError?.message ?? "");
|
||||
checkoutStatusQuery.isError &&
|
||||
/does not exist|no such file or directory|ENOENT/i.test(checkoutQueryError ?? "");
|
||||
|
||||
const repoInfoStatus: "idle" | "loading" | "ready" | "error" = !shouldInspectRepo
|
||||
? "idle"
|
||||
: repoAvailabilityError
|
||||
? "error"
|
||||
: repoRequestStatus === "loading"
|
||||
: checkoutStatusQuery.isPending || checkoutStatusQuery.isFetching
|
||||
? "loading"
|
||||
: repoRequestStatus === "error"
|
||||
? isNonGitDirectory
|
||||
? "idle"
|
||||
: "error"
|
||||
: repoRequestStatus === "success"
|
||||
: checkoutStatusQuery.isError || Boolean(checkoutPayloadError)
|
||||
? "error"
|
||||
: checkout?.isGit
|
||||
? "ready"
|
||||
: "idle";
|
||||
|
||||
const repoInfoError =
|
||||
repoAvailabilityError ?? (isNonGitDirectory ? null : repoRequestError?.message ?? null);
|
||||
const gitHelperText = isNonGitDirectory
|
||||
? "No git repository detected. Git options are disabled for this directory."
|
||||
: null;
|
||||
repoAvailabilityError ??
|
||||
(checkoutStatusQuery.isError ? checkoutQueryError : null) ??
|
||||
checkoutPayloadError;
|
||||
const isCreateWorktree = worktreeMode === "create";
|
||||
const isAttachWorktree = worktreeMode === "attach";
|
||||
|
||||
const worktreeListRoot = repoInfo?.repoRoot ?? trimmedWorkingDir;
|
||||
const worktreeListRoot = checkout?.repoRoot ?? trimmedWorkingDir;
|
||||
const worktreeListQuery = useQuery({
|
||||
queryKey: ["paseoWorktreeList", selectedServerId, worktreeListRoot],
|
||||
queryFn: async () => {
|
||||
@@ -446,10 +430,10 @@ export function DraftAgentScreen({
|
||||
setSelectedWorktreePath("");
|
||||
}
|
||||
if (mode !== "none") {
|
||||
refetchRepoInfo();
|
||||
refetchCheckoutStatus();
|
||||
}
|
||||
},
|
||||
[worktreeSlug, refetchRepoInfo]
|
||||
[worktreeSlug, refetchCheckoutStatus]
|
||||
);
|
||||
|
||||
const validateWorktreeName = useCallback(
|
||||
@@ -567,11 +551,14 @@ export function DraftAgentScreen({
|
||||
if (baseBranch) {
|
||||
return;
|
||||
}
|
||||
const current = repoInfo?.currentBranch?.trim();
|
||||
const current = checkout?.isGit ? checkout.currentBranch?.trim() : null;
|
||||
if (!current || current === "HEAD") {
|
||||
return;
|
||||
}
|
||||
if (current) {
|
||||
setBaseBranch(current);
|
||||
}
|
||||
}, [isCreateWorktree, isNonGitDirectory, baseBranch, repoInfo?.currentBranch]);
|
||||
}, [isCreateWorktree, isNonGitDirectory, baseBranch, checkout]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isNonGitDirectory && worktreeMode !== "none") {
|
||||
@@ -787,7 +774,6 @@ export function DraftAgentScreen({
|
||||
baseBranch,
|
||||
worktreeSlug,
|
||||
selectedWorktreePath,
|
||||
repoInfo?.currentBranch,
|
||||
gitBlockingError,
|
||||
baseBranchError,
|
||||
isDirectoryNotExists,
|
||||
@@ -891,7 +877,7 @@ export function DraftAgentScreen({
|
||||
worktreeMode={worktreeMode}
|
||||
onWorktreeModeChange={handleWorktreeModeChange}
|
||||
worktreeSlug={worktreeSlug}
|
||||
currentBranch={repoInfo?.currentBranch ?? null}
|
||||
currentBranch={checkout?.isGit ? checkout.currentBranch ?? null : null}
|
||||
baseBranch={baseBranch}
|
||||
onBaseBranchChange={handleBaseBranchChange}
|
||||
status={repoInfoStatus}
|
||||
|
||||
Reference in New Issue
Block a user