Skip to content

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

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