| 1 | import type { Route } from "./+types/tree"; |
| 2 | import { page } from "../../lib/meta"; |
| 3 | import { TreeView } from "../../components/repo-view"; |
| 4 | import { redirectIfBranchRenamed } from "../../lib/branch-redirect.server"; |
| 5 | import { commitChecksFor } from "../../lib/commit-checks.server"; |
| 6 | import { aboutFor } from "../../lib/about.server"; |
| 7 | import { environmentsFor } from "../../lib/deployments.server"; |
| 8 | import { lastCommitsFor } from "../../lib/last-commits.server"; |
| 9 | import { showOneCommit } from "../../lib/commit-people.server"; |
| 10 | import { repos } from "../../lib/services.server"; |
| 11 | import { getViewer, unwrap } from "../../lib/session.server"; |
| 12 | import { projectHomepage } from "../../lib/about"; |
| 13 | import { useProject } from "./layout"; |
| 14 | |
| 15 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 16 | const path = params["*"] ? `${params["*"]} · ` : ""; |
| 17 | return page(args, { title: `${path}${params.owner}/${params.repo} · g1t` }); |
| 18 | } |
| 19 | |
| 20 | export 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 | |
| 44 | export 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 | } |