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