g1t/apps/web/app/routes/repo/blob.tsx

46 lines1,908 bytesCodeBlame
1import type { Route } from "./+types/blob";
2import { page } from "../../lib/meta";
3import { BlobView } from "../../components/repo-view";
4import { 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, list] = await Promise.all([
19 repos.blob(path, viewer, params.ref, file),
20 wantsBlame ? repos.blame(path, viewer, params.ref, file) : null,
21 // For the branch menu; the page still shows without it.
22 repos.branches(path, viewer).catch(() => null),
23 ]);
24 const branches = list?.ok ? list.value : null;
25 // A branch that was renamed: the same file on its new name.
26 if (!found.ok && found.error.code === "not_found") await redirectIfBranchRenamed(request, path, viewer, params.ref);
27 const blob = unwrap(found);
28 if (blame?.ok && blob.text != null) {
29 return {
30 blob,
31 html: null,
32 blame: { blame: blame.value, lines: await highlightLines(blob.path, blob.text) },
33 branches,
34 };
35 }
36 return {
37 blob,
38 html: blob.text == null ? null : await highlightLines(blob.path, blob.text),
39 blame: null,
40 branches,
41 };
42}
43
44export default function Blob({ loaderData }: Route.ComponentProps) {
45 return <BlobView blob={loaderData.blob} html={loaderData.html} blame={loaderData.blame} branches={loaderData.branches} />;
46}