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 ( Clone from Git URL Clone any Git repository by URL onto {host.label} and open it as a workspace. {status.isPending ? ( Checking Git availability... ) : status.error ? ( {errorMessage(status.error)} void status.refetch()} > Retry ) : !status.data.gitAvailable ? ( Git is not available on this host Install git on {host.label} and make sure it is on the daemon PATH, then retry. void status.refetch()} > Recheck ) : ( Repository URL { 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 ? ( Host: {parsedUrl.host} ยท Repo: {parsedUrl.repoPath} ) : ( {parsedUrl.error} ) ) : ( Supports https://, http://, ssh://, git://, and user@host:owner/repo.git. )} Clone into { 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() ? ( Destination: {destinationPreview(targetDirectory, effectiveFolderName)} ) : null} Folder name (optional) { 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 ? ( {dirNameValidation.error} ) : null} Branch or tag (optional) { 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 ? ( {branchValidation.error} ) : null} clone.mutate()} > {clone.isPending ? "Cloning..." : "Clone and open workspace"} {clone.error ? {errorMessage(clone.error)} : null} {clone.data ? ( {clone.data.alreadyExists ? "Opened existing checkout" : "Cloned"}: {clone.data.path} ) : null} {status.data.gitVersion ? ( Using {status.data.gitVersion} on {host.label}. ) : null} )} ); }