import { Empty, LinkButton } from "@refs/design-system"; import { LayerCard } from "@refs/design-system"; import { ClockCounterClockwiseIcon, FileDashedIcon } from "@refs/design-system/icons"; import { useEffect, useState } from "react"; import { data } from "react-router"; import type { Route } from "./+types/repo-blob"; import { repoLink, splatSegments, type File } from "@refs/backend/contract"; import { BranchPicker, PathBreadcrumbs } from "@refs/design-system"; import type { CodeToken } from "../components/highlight"; import { found, refs, fullName } from "../lib/session"; export const meta: Route.MetaFunction = ({ params }) => [{ title: `${params["*"]} · ${fullName(params)} · refs` }]; export async function loader({ params, context }: Route.LoaderArgs) { const segments = splatSegments(params["*"]); if (!segments) throw data(null, { status: 404 }); const repo = await found(refs(context).repo(fullName(params))); const [file, refList] = await found(Promise.all([repo.file(segments), repo.refs()])); return { repo: repo.summary.name, refs: refList, file }; } function formatSize(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / 1024 / 1024).toFixed(1)} MB`; } /** The highlighter is large and works in the browser only, so the server build leaves it out. */ const loadHighlighter = import.meta.env.SSR ? null : () => import("../components/highlight"); /** * The colours of a file's lines, or null while they load and when the file stays plain. The server sends plain * text, so the code is readable at once; the colours arrive after and change no layout. */ function useHighlight(name: string, lines: readonly string[] | null): CodeToken[][] | null { const [tokens, setTokens] = useState(null); useEffect(() => { setTokens(null); if (!lines || !loadHighlighter) return; let current = true; loadHighlighter() .then((module) => module.highlightLines(name, lines)) .then((result) => current && setTokens(result)) .catch((error: unknown) => console.error("highlighting failed", error)); return () => { current = false; }; }, [name, lines]); return tokens; } function CodeLine({ text, tokens }: { text: string; tokens: readonly CodeToken[] | undefined }) { if (!tokens) return text; return tokens.map((token, index) => token.light || token.dark ? ( {token.text} ) : ( token.text ), ); } function FileBodyView({ file }: { file: File }) { const tokens = useHighlight(file.name, file.body.kind === "text" ? file.body.lines : null); const body = file.body; if (body.kind === "image") { return (
{file.name}
); } if (body.kind === "markdown") return
; if (body.kind === "text") { return (
{body.lines.map((line, i) => ( ))}
{i + 1}
); } return (
} title={body.kind === "binary" ? "Binary file not shown" : "File too large to display"} contents={ View raw } />
); } export default function RepoBlob({ loaderData }: Route.ComponentProps) { const { repo, refs: refList, file } = loaderData; const { ref, path } = file.location; const rawUrl = repoLink(repo, "raw", ref, path); const lines = file.body.kind === "text" ? `${file.body.lines.length} lines · ` : ""; return (
}> History
{lines} {formatSize(file.size)} Raw
); }