Files
git-clone/client/git-clone.tsx
T

348 lines
12 KiB
TypeScript

import type { PluginScreenProps } from "@getpaseo/plugin/client";
import { usePaseo, useRpc } from "@getpaseo/plugin/client";
import { useMutation, useQuery } from "@tanstack/react-query";
import { useEffect, useMemo, useState } from "react";
import { ActivityIndicator, Pressable, ScrollView, Text, TextInput, View } from "react-native";
import {
destinationPreview,
gitCloneRepoRpc,
gitCloneStatusRpc,
parseGitUrl,
validateBranchName,
validateDirectoryName,
} from "../shared/git-clone";
function errorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}
export function GitCloneScreen({ theme, host, layout, navigation }: PluginScreenProps) {
const paseo = usePaseo();
const getStatus = useRpc(gitCloneStatusRpc);
const cloneRepository = useRpc(gitCloneRepoRpc);
const [url, setUrl] = useState("");
const [targetDirectory, setTargetDirectory] = useState("");
const [targetTouched, setTargetTouched] = useState(false);
const [directoryName, setDirectoryName] = useState("");
const [branch, setBranch] = useState("");
const status = useQuery({
queryKey: ["git-clone", host.id, "status"],
queryFn: () => getStatus({}),
retry: false,
staleTime: 15_000,
});
useEffect(() => {
if (!targetTouched && status.data) {
setTargetDirectory(status.data.suggestedTargetDirectory);
}
}, [status.data, targetTouched]);
const trimmedUrl = url.trim();
const parsedUrl = useMemo(() => (trimmedUrl ? parseGitUrl(trimmedUrl) : null), [trimmedUrl]);
const trimmedDirName = directoryName.trim();
const dirNameValidation = useMemo(
() => (trimmedDirName ? validateDirectoryName(trimmedDirName) : null),
[trimmedDirName],
);
const trimmedBranch = branch.trim();
const branchValidation = useMemo(
() => (trimmedBranch ? validateBranchName(trimmedBranch) : null),
[trimmedBranch],
);
const effectiveFolderName =
dirNameValidation && dirNameValidation.ok
? dirNameValidation.value
: parsedUrl && parsedUrl.ok
? parsedUrl.repoName
: null;
const clone = useMutation({
mutationFn: async () => {
const check = parseGitUrl(url);
if (!check.ok) throw new Error(check.error);
const directory = targetDirectory.trim();
if (!directory) throw new Error("Enter a target directory");
const result = await cloneRepository({
url: check.url,
targetDirectory: directory,
directoryName: trimmedDirName || undefined,
branch: trimmedBranch || undefined,
});
try {
const workspace = await paseo.workspaces.open(result.path);
return { ...result, workspaceId: workspace.id };
} catch (error) {
throw new Error(
`Cloned to ${result.path}, but Paseo could not open it: ${errorMessage(error)}`,
);
}
},
onSuccess: (data) => {
navigation?.openWorkspace({ workspaceId: data.workspaceId, serverId: host.id });
},
});
const styles = useMemo(
() => ({
screen: {
flex: 1,
backgroundColor: theme.colors.surface0,
},
content: {
padding: layout.compact ? 16 : 24,
gap: 16,
},
title: {
color: theme.colors.foreground,
fontSize: layout.compact ? 22 : 28,
fontWeight: "600" as const,
},
detail: { color: theme.colors.foregroundMuted, fontSize: 14, lineHeight: 20 },
label: { color: theme.colors.foreground, fontSize: 14, fontWeight: "600" as const },
input: {
minHeight: 44,
borderWidth: 1,
borderColor: theme.colors.border,
borderRadius: 8,
backgroundColor: theme.colors.surface1,
color: theme.colors.foreground,
paddingHorizontal: 12,
paddingVertical: 10,
fontSize: 15,
},
card: {
borderWidth: 1,
borderColor: theme.colors.border,
borderRadius: 8,
backgroundColor: theme.colors.surface1,
padding: layout.compact ? 14 : 18,
gap: 14,
},
row: {
flexDirection: layout.compact ? ("column" as const) : ("row" as const),
gap: 12,
},
col: {
flex: 1,
gap: 8,
},
button: {
minHeight: 44,
borderRadius: 8,
backgroundColor: theme.colors.accent,
alignItems: "center" as const,
justifyContent: "center" as const,
paddingHorizontal: 16,
paddingVertical: 11,
},
buttonDisabled: { opacity: 0.55 },
buttonText: {
color: theme.colors.accentForeground,
fontSize: 15,
fontWeight: "600" as const,
textAlign: "center" as const,
},
secondaryButton: {
minHeight: 40,
borderRadius: 8,
borderWidth: 1,
borderColor: theme.colors.border,
alignItems: "center" as const,
justifyContent: "center" as const,
paddingHorizontal: 14,
paddingVertical: 9,
},
secondaryButtonText: {
color: theme.colors.foreground,
fontSize: 14,
fontWeight: "600" as const,
},
error: { color: theme.colors.statusDanger, fontSize: 14, lineHeight: 20 },
success: { color: theme.colors.statusSuccess, fontSize: 14, lineHeight: 20 },
state: {
paddingVertical: 24,
alignItems: "center" as const,
gap: 10,
},
}),
[theme, layout.compact],
);
const canClone =
Boolean(status.data?.gitAvailable) &&
Boolean(parsedUrl && parsedUrl.ok) &&
targetDirectory.trim().length > 0 &&
(!dirNameValidation || dirNameValidation.ok) &&
(!branchValidation || branchValidation.ok) &&
!clone.isPending;
return (
<ScrollView style={styles.screen} contentContainerStyle={styles.content}>
<View style={{ gap: 6 }}>
<Text style={styles.title}>Clone from Git URL</Text>
<Text style={styles.detail}>
Clone any Git repository by URL onto {host.label} and open it as a workspace.
</Text>
</View>
{status.isPending ? (
<View style={styles.state}>
<ActivityIndicator color={theme.colors.accent} />
<Text style={styles.detail}>Checking Git availability...</Text>
</View>
) : status.error ? (
<View style={{ gap: 12 }}>
<Text style={styles.error}>{errorMessage(status.error)}</Text>
<Pressable
accessibilityRole="button"
accessibilityLabel="Retry checking Git availability"
style={styles.secondaryButton}
onPress={() => void status.refetch()}
>
<Text style={styles.secondaryButtonText}>Retry</Text>
</Pressable>
</View>
) : !status.data.gitAvailable ? (
<View style={styles.card}>
<Text style={styles.label}>Git is not available on this host</Text>
<Text style={styles.detail}>
Install git on {host.label} and make sure it is on the daemon PATH, then retry.
</Text>
<Pressable
accessibilityRole="button"
accessibilityLabel="Recheck Git availability"
style={styles.secondaryButton}
onPress={() => void status.refetch()}
>
<Text style={styles.secondaryButtonText}>Recheck</Text>
</Pressable>
</View>
) : (
<View style={styles.card}>
<View style={{ gap: 8 }}>
<Text style={styles.label}>Repository URL</Text>
<TextInput
accessibilityLabel="Git repository URL"
value={url}
onChangeText={(value) => {
setUrl(value);
if (clone.error || clone.data) clone.reset();
}}
placeholder="https://github.com/owner/repo.git or git@host:owner/repo.git"
placeholderTextColor={theme.colors.foregroundMuted}
autoCapitalize="none"
autoCorrect={false}
style={styles.input}
/>
{parsedUrl ? (
parsedUrl.ok ? (
<Text style={styles.detail}>
Host: {parsedUrl.host} · Repo: {parsedUrl.repoPath}
</Text>
) : (
<Text style={styles.error}>{parsedUrl.error}</Text>
)
) : (
<Text style={styles.detail}>
Supports https://, http://, ssh://, git://, and user@host:owner/repo.git.
</Text>
)}
</View>
<View style={{ gap: 8 }}>
<Text style={styles.label}>Clone into</Text>
<TextInput
accessibilityLabel="Target parent directory"
value={targetDirectory}
onChangeText={(value) => {
setTargetTouched(true);
setTargetDirectory(value);
if (clone.error || clone.data) clone.reset();
}}
placeholder={status.data.suggestedTargetDirectory}
placeholderTextColor={theme.colors.foregroundMuted}
autoCapitalize="none"
autoCorrect={false}
style={styles.input}
/>
{effectiveFolderName && targetDirectory.trim() ? (
<Text style={styles.detail}>
Destination: {destinationPreview(targetDirectory, effectiveFolderName)}
</Text>
) : null}
</View>
<View style={styles.row}>
<View style={styles.col}>
<Text style={styles.label}>Folder name (optional)</Text>
<TextInput
accessibilityLabel="Folder name override"
value={directoryName}
onChangeText={(value) => {
setDirectoryName(value);
if (clone.error || clone.data) clone.reset();
}}
placeholder={parsedUrl && parsedUrl.ok ? parsedUrl.repoName : "Defaults to repo name"}
placeholderTextColor={theme.colors.foregroundMuted}
autoCapitalize="none"
autoCorrect={false}
style={styles.input}
/>
{dirNameValidation && !dirNameValidation.ok ? (
<Text style={styles.error}>{dirNameValidation.error}</Text>
) : null}
</View>
<View style={styles.col}>
<Text style={styles.label}>Branch or tag (optional)</Text>
<TextInput
accessibilityLabel="Branch or tag name"
value={branch}
onChangeText={(value) => {
setBranch(value);
if (clone.error || clone.data) clone.reset();
}}
placeholder="Default branch"
placeholderTextColor={theme.colors.foregroundMuted}
autoCapitalize="none"
autoCorrect={false}
style={styles.input}
/>
{branchValidation && !branchValidation.ok ? (
<Text style={styles.error}>{branchValidation.error}</Text>
) : null}
</View>
</View>
<Pressable
accessibilityRole="button"
accessibilityLabel="Clone repository and open workspace"
disabled={!canClone}
style={[styles.button, !canClone ? styles.buttonDisabled : null]}
onPress={() => clone.mutate()}
>
<Text style={styles.buttonText}>
{clone.isPending ? "Cloning..." : "Clone and open workspace"}
</Text>
</Pressable>
{clone.error ? <Text style={styles.error}>{errorMessage(clone.error)}</Text> : null}
{clone.data ? (
<Text style={styles.success}>
{clone.data.alreadyExists ? "Opened existing checkout" : "Cloned"}: {clone.data.path}
</Text>
) : null}
{status.data.gitVersion ? (
<Text style={styles.detail}>Using {status.data.gitVersion} on {host.label}.</Text>
) : null}
</View>
)}
</ScrollView>
);
}