Skip to content

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

70 lines2,798 bytesCodeBlame
1import { isCodeownersPath } from "@g1t/contracts";
2
3import type { Route } from "./+types/blob";
4import { page } from "../../lib/meta";
5import { CodeownersFileErrors } from "../../components/codeowners";
6import { BlobView } from "../../components/repo-view";
7import { highlightLines } from "../../lib/highlight.server";
8import { repos, work } from "../../lib/services.server";
9import { redirectIfBranchRenamed } from "../../lib/branch-redirect.server";
10import { getViewer, unwrap } from "../../lib/session.server";
11
12export function meta({ params, ...args }: Route.MetaArgs) {
13 return page(args, { title: `${params["*"]} · ${params.owner}/${params.repo} · g1t` });
14}
15
16export async function loader({ params, context, request }: Route.LoaderArgs) {
17 const viewer = getViewer(context);
18 const path = { namespace: params.owner, name: params.repo };
19 const file = params["*"] ?? "";
20 const wantsBlame = new URL(request.url).searchParams.has("blame");
21 const [found, blame, list, checked] = await Promise.all([
22 repos.blob(path, viewer, params.ref, file),
23 wantsBlame ? repos.blame(path, viewer, params.ref, file) : null,
24 // For the branch menu; the page still shows without it.
25 repos.branches(path, viewer).catch(() => null),
26 // A CODEOWNERS file is checked as it is shown; the file shows without it.
27 isCodeownersPath(file) ? work.codeownersErrors(path, viewer, params.ref).catch(() => null) : null,
28 ]);
29 const codeowners = checked?.ok ? checked.value : null;
30 const branches = list?.ok ? list.value : null;
31 // A branch that was renamed: the same file on its new name.
32 if (!found.ok && found.error.code === "not_found") await redirectIfBranchRenamed(request, path, viewer, params.ref);
33 const blob = unwrap(found);
34 if (blame?.ok && blob.text != null) {
35 return {
36 blob,
37 html: null,
38 blame: { blame: blame.value, lines: await highlightLines(blob.path, blob.text) },
39 branches,
40 codeowners,
41 };
42 }
43 return {
44 blob,
45 html: blob.text == null ? null : await highlightLines(blob.path, blob.text),
46 blame: null,
47 branches,
48 codeowners,
49 };
50}
51
52export default function Blob({ loaderData, params }: Route.ComponentProps) {
53 const { codeowners, blob } = loaderData;
54 // Its errors' lines are marked, when they are this file's.
55 const own = codeowners != null && codeowners.path === blob.path;
56 return (
57 <BlobView
58 blob={blob}
59 html={loaderData.html}
60 blame={loaderData.blame}
61 branches={loaderData.branches}
62 notice={
63 codeowners && (
64 <CodeownersFileErrors report={codeowners} path={blob.path} base={`/${params.owner}/${params.repo}`} />
65 )
66 }
67 marked={own ? codeowners.errors.map((error) => error.line).filter((line) => line > 0) : undefined}
68 />
69 );
70}