Skip to content
58 linesCodeBlameRaw
1import type { Route } from "./+types/tree";
2import { page } from "../../lib/meta";
3import { TreeView } from "../../components/repo-view";
4import { redirectIfBranchRenamed } from "../../lib/branch-redirect.server";
5import { commitChecksFor } from "../../lib/commit-checks.server";
6import { aboutFor } from "../../lib/about.server";
7import { environmentsFor } from "../../lib/deployments.server";
8import { lastCommitsFor } from "../../lib/last-commits.server";
9import { showOneCommit } from "../../lib/commit-people.server";
10import { repos } from "../../lib/services.server";
11import { getViewer, unwrap } from "../../lib/session.server";
12import { projectHomepage } from "../../lib/about";
13import { useProject } from "./layout";
14
15export function meta({ params, ...args }: Route.MetaArgs) {
16 const path = params["*"] ? `${params["*"]} · ` : "";
17 return page(args, { title: `${path}${params.owner}/${params.repo} · g1t` });
18}
19
20export async function loader({ params, context, request }: Route.LoaderArgs) {
21 const path = { namespace: params.owner, name: params.repo };
22 const viewer = getViewer(context);
23 const [tree, branches, environments] = await Promise.all([
24 repos.tree(path, viewer, params.ref, params["*"] ?? ""),
25 // For the branch menu; the page still shows without it.
26 repos.branches(path, viewer).catch(() => null),
27 // For the About sidebar's Deployments panel, shown at the root only.
28 params["*"] ? null : environmentsFor(path, viewer),
29 ]);
30 // A branch that was renamed: the same folder on its new name.
31 if (!tree.ok && tree.error.code === "not_found") await redirectIfBranchRenamed(request, path, viewer, params.ref);
32 // Each entry's last commit walks history: streamed in after the list.
33 const lastCommits = lastCommitsFor(path, viewer, params.ref, params["*"] ?? "");
34 const found = unwrap(tree);
35 // The latest commit's author, as their account where there is one.
36 const value = { ...found, head: await showOneCommit(found.head) };
37 // The About beside the files, streamed in after them.
38 const about = value.head && !value.path ? aboutFor(path, viewer, value.repo.id) : null;
39 // The latest commit's checks: streamed in beside it.
40 const checks = commitChecksFor(path, viewer, [value.head?.hash]);
41 return { tree: value, branches: branches?.ok ? branches.value : null, lastCommits, about, deployments: environments, checks };
42}
43
44export default function Tree({ loaderData }: Route.ComponentProps) {
45 const project = useProject();
46 return (
47 <TreeView
48 tree={loaderData.tree}
49 branches={loaderData.branches}
50 lastCommits={loaderData.lastCommits}
51 checks={loaderData.checks}
52 about={loaderData.about}
53 canPush={Boolean(project?.access.can.push)}
54 homepage={projectHomepage(project?.project ?? null)}
55 deployments={loaderData.deployments}
56 />
57 );
58}