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