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