| 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 { lastCommitsFor } from "../../lib/last-commits.server"; |
| 6 | import { repos } from "../../lib/services.server"; |
| 7 | import { getViewer, unwrap } from "../../lib/session.server"; |
| 8 | |
| 9 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 10 | const path = params["*"] ? `${params["*"]} · ` : ""; |
| 11 | return page(args, { title: `${path}${params.owner}/${params.repo} · g1t` }); |
| 12 | } |
| 13 | |
| 14 | export async function loader({ params, context, request }: Route.LoaderArgs) { |
| 15 | const path = { namespace: params.owner, name: params.repo }; |
| 16 | const viewer = getViewer(context); |
| 17 | const [tree, branches] = await Promise.all([ |
| 18 | repos.tree(path, viewer, params.ref, params["*"] ?? ""), |
| 19 | // For the branch menu; the page still shows without it. |
| 20 | repos.branches(path, viewer).catch(() => null), |
| 21 | ]); |
| 22 | // A branch that was renamed: the same folder on its new name. |
| 23 | if (!tree.ok && tree.error.code === "not_found") await redirectIfBranchRenamed(request, path, viewer, params.ref); |
| 24 | // Each entry's last commit walks history: streamed in after the list. |
| 25 | const lastCommits = lastCommitsFor(path, viewer, params.ref, params["*"] ?? ""); |
| 26 | return { tree: unwrap(tree), branches: branches?.ok ? branches.value : null, lastCommits }; |
| 27 | } |
| 28 | |
| 29 | export default function Tree({ loaderData }: Route.ComponentProps) { |
| 30 | return <TreeView tree={loaderData.tree} branches={loaderData.branches} lastCommits={loaderData.lastCommits} />; |
| 31 | } |