41 lines1,685 bytesCodeBlame
1import type { Route } from "./+types/blob";
2import { page } from "../../lib/meta";
3import { BlobView } from "../../components/repo-view";
4import { highlight, highlightLines } from "../../lib/highlight.server";
5import { repos } from "../../lib/services.server";
6import { redirectIfBranchRenamed } from "../../lib/branch-redirect.server";
7import { getViewer, unwrap } from "../../lib/session.server";
8
9export function meta({ params, ...args }: Route.MetaArgs) {
10 return page(args, { title: `${params["*"]} · ${params.owner}/${params.repo} · g1t` });
11}
12
13export async function loader({ params, context, request }: Route.LoaderArgs) {
14 const viewer = getViewer(context);
15 const path = { namespace: params.owner, name: params.repo };
16 const file = params["*"] ?? "";
17 const wantsBlame = new URL(request.url).searchParams.has("blame");
18 const [found, blame] = await Promise.all([
19 repos.blob(path, viewer, params.ref, file),
20 wantsBlame ? repos.blame(path, viewer, params.ref, file) : null,
21 ]);
22 // A branch that was renamed: the same file on its new name.
23 if (!found.ok && found.error.code === "not_found") await redirectIfBranchRenamed(request, path, viewer, params.ref);
24 const blob = unwrap(found);
25 if (blame?.ok && blob.text != null) {
26 return {
27 blob,
28 html: null,
29 blame: { blame: blame.value, lines: await highlightLines(blob.path, blob.text) },
30 };
31 }
32 return {
33 blob,
34 html: blob.text == null ? null : await highlight(blob.path, blob.text),
35 blame: null,
36 };
37}
38
39export default function Blob({ loaderData }: Route.ComponentProps) {
40 return <BlobView blob={loaderData.blob} html={loaderData.html} blame={loaderData.blame} />;
41}