/** Clone Repository › GitHub: the repositories of the account gh is signed in to. */ import { useEffect, useMemo, useState, type KeyboardEvent } from "react"; import type { GhAccountRepo, GhAccountRepos } from "@/bindings"; import { GH_MANUAL_INSTALL_URL, installGhTool } from "@/platform/ghTool"; import { tx } from "@/platform/i18n"; import { call, commands, isCancelled } from "@/platform/ipc"; import { Button, EmptyState, HighlightedText, Icon, List, SearchField, Spinner, T, fuzzyMatch, listNavigate, } from "@/ui"; import type { CloneSourceProps } from "@/workbench/contributions"; import { openInBrowser } from "../data/local"; import styles from "./GitHubCloneSource.module.css"; import { useRowHeight } from "@/ui/density"; type Row = { repo: GhAccountRepo; indices: number[] | null }; /** Repositories matching `query` (fuzzy, on `owner/name`), in the account's order. */ export function matchRepos(repos: readonly GhAccountRepo[], query: string): Row[] { const q = query.trim(); if (!q) return repos.map((repo) => ({ repo, indices: null })); return repos.flatMap((repo) => { const indices = fuzzyMatch(repo.nameWithOwner, q); return indices ? [{ repo, indices }] : []; }); } export function GitHubCloneSource({ url, onUrl, onClone, disabled }: CloneSourceProps) { const rowHeight = useRowHeight(26); const [state, setState] = useState(null); const [query, setQuery] = useState(""); const [attempt, setAttempt] = useState(0); const [installingGh, setInstallingGh] = useState(false); const [installError, setInstallError] = useState(null); useEffect(() => { let alive = true; call(commands.ghAccountRepos()) .then((s) => alive && setState(s)) .catch((e) => alive && setState({ kind: "error", message: e instanceof Error ? e.message : String(e) })); return () => { alive = false; }; }, [attempt]); const rows = useMemo(() => (state?.kind === "ready" ? matchRepos(state.repos, query) : []), [state, query]); const active = rows.findIndex((r) => r.repo.cloneUrl === url); const reload = () => { setInstallError(null); setState(null); setAttempt((n) => n + 1); }; const installGh = () => { if (installingGh) return; setInstallingGh(true); setInstallError(null); void installGhTool() .promise.then((installed) => { if (installed) reload(); }) .catch((e: unknown) => { if (!isCancelled(e)) setInstallError(e instanceof Error ? e.message : String(e)); }) .finally(() => setInstallingGh(false)); }; const retry = ( ); if (!state) return (
Loading repositories…
); if (state.kind === "notInstalled") return ( {retry} } > GitHub repositories are listed with the gh command line tool. Install it, then run{" "} gh auth login.{installError &&
{installError}
}
); if (state.kind === "notLoggedIn") return ( Run gh auth login in a terminal, then retry. ); if (state.kind === "error") return ( {state.message} ); const pick = (i: number) => { const row = rows[i]; if (row) onUrl(row.repo.cloneUrl); }; const onKeyDown = (e: KeyboardEvent) => { if (e.key === "Enter" && rows[active]) { e.preventDefault(); onClone(rows[active].repo.cloneUrl); return; } const next = listNavigate(e.key, { items: rows, active: Math.max(active, 0) }); if (next !== null) { e.preventDefault(); pick(next); } }; return (
{state.login}
aria-label="Repositories" items={rows} getKey={(r) => r.repo.nameWithOwner} activeIndex={active} onActiveChange={pick} focusable={false} rowHeight={rowHeight} empty={ {state.repos.length ? "No matching repositories" : "No repositories"} } renderItem={(r) => ( onClone(r.repo.cloneUrl)} > {r.indices ? : r.repo.nameWithOwner} {r.repo.fork && ( fork )} {r.repo.archived && ( archived )} {r.repo.description && {r.repo.description}} )} />
); }