import { BookOpen, DownloadCloud, ExternalLink, Globe, Loader2, } from "lucide-react"; import type { ProjectRepoFile } from "@/features/projects/hooks"; import { projectExternalRefUrl } from "@/features/projects/lib/projectExternalUrl"; import type { ProjectRepoUnavailableReason } from "@/features/projects/lib/projectRepoAvailability"; import { Button } from "@/shared/ui/button"; import { BuzzLoadingState } from "@/shared/ui/BuzzLoadingState"; import { Markdown, SyntaxHighlightedCode } from "@/shared/ui/markdown"; import { baseName, formatLastChangedAt, languageForPath, } from "./ProjectRepositoryPanel"; import { type RepoSourceHeaderControls, RepoSourceDropdown, RepoSyncActionButton, RepositoryBranchDropdown, } from "./ProjectRepositorySource"; import { GitHubMark } from "./GitHubMark"; import { ProjectRepositoryUnavailableState } from "./ProjectRepositoryUnavailableState"; export function findReadmeFile(files: ProjectRepoFile[]) { const readmes = files.filter((file) => /^readme(?:\.(?:md|markdown|mdx|txt))?$/i.test(baseName(file.path)), ); return readmes.find((file) => !file.path.includes("/")) ?? readmes[0] ?? null; } function decodeHtmlEntities(value: string) { return value .replace(/&/g, "&") .replace(/</g, "<") .replace(/>/g, ">") .replace(/"/g, '"') .replace(/'/g, "'"); } function htmlInlineToMarkdown(value: string): string { return decodeHtmlEntities(value) .replace(//gi, "\n") .replace(/]*)>/gi, (_match: string, attrs: string) => { const src = attrs.match(/\bsrc=["']([^"']+)["']/i)?.[1]; const alt = attrs.match(/\balt=["']([^"']*)["']/i)?.[1] ?? ""; return src ? `![${alt}](${src})` : ""; }) .replace( /]*\bhref=["']([^"']+)["'][^>]*>([\s\S]*?)<\/a>/gi, (_match: string, href: string, label: string) => `[${htmlInlineToMarkdown(label).trim()}](${href})`, ) .replace(/<(strong|b)\b[^>]*>([\s\S]*?)<\/\1>/gi, "**$2**") .replace(/<(em|i)\b[^>]*>([\s\S]*?)<\/\1>/gi, "*$2*") .replace(/]*>([\s\S]*?)<\/code>/gi, "`$1`") .replace(/]*>([\s\S]*?)<\/sub>/gi, "$1") .replace(/]*>([\s\S]*?)<\/span>/gi, "$1") .replace(/<[^>]+>/g, "") .trim(); } function normalizeReadmeMarkdown(content: string) { return content .replace( /]*>([\s\S]*?)<\/h\1>/gi, (_match, depth: string, value: string) => `${"#".repeat(Number(depth))} ${htmlInlineToMarkdown(value)}\n\n`, ) .replace( /]*>([\s\S]*?)<\/p>/gi, (_match, value: string) => `${htmlInlineToMarkdown(value)}\n\n`, ) .replace( /]*>([\s\S]*?)<\/div>/gi, (_match, value: string) => `${htmlInlineToMarkdown(value)}\n\n`, ) .replace( /]*>([\s\S]*?)<\/center>/gi, (_match, value: string) => `${htmlInlineToMarkdown(value)}\n\n`, ) .replace(/\n{3,}/g, "\n\n") .trim(); } export function ReadmePanel({ accessChannelId, file, gitDataState, externalHost, externalUrl, hideHeader, ownerAvatarUrl, ownerIsAgent, ownerName, sourceControls, unavailableReason, }: { /** `buzz-channel` binding of the repository, for access-restricted copy. */ accessChannelId?: string | null; file: ProjectRepoFile | null; gitDataState: "checking" | "available" | "empty" | "unavailable"; externalHost?: string; externalUrl?: string | null; /** * Skip the header rows entirely — the workspace layout renders the source * controls and last-changed timestamp itself. */ hideHeader?: boolean; ownerAvatarUrl?: string | null; ownerIsAgent?: boolean; ownerName?: string; unavailableReason?: ProjectRepoUnavailableReason; /** Branch picker + remote/local toggle rendered in the panel header. */ sourceControls?: RepoSourceHeaderControls; }) { const externalOpenUrl = projectExternalRefUrl( externalUrl, sourceControls?.selectedTag ?? sourceControls?.branch, ); // Two header rows, mirroring the files panel: controls on top, then the // file identity row. const header = hideHeader ? null : ( <> {sourceControls ? (
) : null}
{file ? baseName(file.path) : "README"} {file ? ( Last changed {formatLastChangedAt(file.lastChangedAt)} ) : null}
); if (gitDataState === "checking") { return (
{header}
); } if (gitDataState === "unavailable") { if (!externalHost) { return (
); } return (
{externalHost === "github.com" ? ( ) : ( )}

Code hosted on {externalHost}

Clone this repository locally to explore its files, commits, and contributors in Buzz.

{externalOpenUrl ? ( {externalOpenUrl} ) : null}
{sourceControls?.onCloneLocal ? ( ) : null} {externalOpenUrl ? ( ) : null}
); } if (!file?.previewContent) { return (
{header}
{gitDataState === "empty" ? "No files have been pushed to this repository yet." : "Add a README to this repository to describe setup, usage, and project context."}
); } const language = languageForPath(file.path); const isMarkdown = /\.(?:md|markdown|mdx)$/i.test(file.path); const readmeContent = isMarkdown ? normalizeReadmeMarkdown(file.previewContent) : file.previewContent; return (
{header}
{isMarkdown ? ( ) : language ? (
            
          
) : (
            
              {file.previewContent}
            
          
)}
); }