Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

77 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 { useProject } from "./layout";
13import { ForkBar } from "../../components/fork-bar";
14import { forkSourceFor } from "../../lib/forks.server";
15import { repoPages } from "../../lib/repo-live";
16import { useRepoLive } from "../../lib/use-repo-live";
17
18export function meta({ params, ...args }: Route.MetaArgs) {
19 const path = params["*"] ? `${params["*"]} · ` : "";
20 return page(args, { title: `${path}${params.owner}/${params.repo} · g1t` });
21}
22
23export async function loader({ params, context, request }: Route.LoaderArgs) {
24 const path = { namespace: params.owner, name: params.repo };
25 const viewer = getViewer(context);
26 const [tree, branches, environments, fork] = await Promise.all([
27 repos.tree(path, viewer, params.ref, params["*"] ?? ""),
28 // For the branch menu; the page still shows without it.
29 repos.branches(path, viewer).catch(() => null),
30 // For the About sidebar's Deployments panel, shown at the root only.
31 params["*"] ? null : environmentsFor(path, viewer),
32 // A fork's parent, for the bar above its files, at the root only.
33 params["*"] ? null : forkSourceFor(context, params, viewer),
34 ]);
35 // A branch that was renamed: the same folder on its new name.
36 if (!tree.ok && tree.error.code === "not_found") await redirectIfBranchRenamed(request, path, viewer, params.ref);
37 // Each entry's last commit walks history: streamed in after the list.
38 const lastCommits = lastCommitsFor(path, viewer, params.ref, params["*"] ?? "");
39 const found = unwrap(tree);
40 // The latest commit's author, as their account where there is one.
41 const value = { ...found, head: await showOneCommit(found.head) };
42 // The About beside the files, streamed in after them.
43 const about = value.head && !value.path ? aboutFor(path, viewer, value.repo.id) : null;
44 // The latest commit's checks: streamed in beside it.
45 const checks = commitChecksFor(path, viewer, [value.head?.hash]);
46 return { tree: value, branches: branches?.ok ? branches.value : null, lastCommits, about, deployments: environments, checks, fork };
47}
48
49export default function Tree({ loaderData }: Route.ComponentProps) {
50 const project = useProject();
51 // The files and latest commit follow pushes to the branch (none for a
52 // tag or a commit); the latest commit's checks update as they report.
53 const { tree, branches } = loaderData;
54 const names = branches?.map((branch) => branch.name);
55 useRepoLive({ match: repoPages.files({ branch: !names || names.includes(tree.ref) ? tree.ref : null, branches: names, sha: tree.head?.hash }) });
56 return (
57 <TreeView
58 tree={loaderData.tree}
59 branches={loaderData.branches}
60 lastCommits={loaderData.lastCommits}
61 checks={loaderData.checks}
62 about={loaderData.about}
63 canPush={Boolean(project?.access.can.push)}
64 deployments={loaderData.deployments}
65 fork={
66 loaderData.fork && project && branches?.some((branch) => branch.name === tree.ref) ? (
67 <ForkBar
68 base={`/${tree.repo.namespace}/${tree.repo.name}`}
69 branch={tree.ref}
70 parent={loaderData.fork}
71 canPush={Boolean(project.access.can.push)}
72 />
73 ) : null
74 }
75 />
76 );
77}