Skip to content

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

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