import { listBranches, listTree, readBlob } from "@/lib/git"; import { cloneUrls, type Repo } from "@/lib/repo"; import { encodePath } from "@/lib/format"; import { BranchSelect } from "./branch-select"; import { Breadcrumbs } from "./breadcrumbs"; import { CloneButton } from "./clone-button"; import { DocTabs } from "./doc-tabs"; import { FileTable } from "./file-table"; import { BranchIcon } from "./icons"; import { Markdown } from "./markdown"; import { AddFileMenu } from "./repo-add-file-menu"; import { ButtonLink } from "./ui"; const DOCS = [ { label: "README", pattern: /^readme(\.(md|markdown|txt))?$/i }, { label: "Code of conduct", pattern: /^code_of_conduct(\.(md|markdown|txt))?$/i }, { label: "Contributing", pattern: /^contributing(\.(md|markdown|txt))?$/i }, { label: "License", pattern: /^(licen[cs]e|copying)(\.(md|markdown|txt))?$/i }, ]; /** The directory's README, plus (at the root) the other community files, as tabs. */ async function Docs({ repo, gitRef, path }: { repo: Repo; gitRef: string; path: string }) { const entries = await listTree(repo.dir, gitRef, path); const docs = (path ? DOCS.slice(0, 1) : DOCS).flatMap((doc) => { const entry = entries.find((e) => e.type === "blob" && doc.pattern.test(e.name)); return entry ? [{ ...doc, entry }] : []; }); const tabs = ( await Promise.all( docs.map(async ({ label, entry }) => { const blob = await readBlob(repo.dir, gitRef, entry.path); if (blob.binary || blob.tooLarge) return null; const text = blob.content.toString("utf8"); return { label, href: `${repo.base}/blob/${encodePath(gitRef)}/${encodePath(entry.path)}`, content: /\.(md|markdown)$/i.test(entry.name) ? ( {text} ) : (
{text}
), }; }), ) ).filter((t) => t !== null); return tabs.length ? : null; } export async function TreeView({ repo, gitRef, path }: { repo: Repo; gitRef: string; path: string }) { const [branches, urls] = await Promise.all([listBranches(repo.dir), cloneUrls(repo)]); const onBranch = branches.some((b) => b.name === gitRef); return (
b.name)} path={path} /> {path && }
{!path && ( }> Branches {branches.length} )} {repo.canWrite && onBranch && } {!path && }
); }