| 1da9874 | | | 1 | import type { Metadata } from "next"; |
| 3e3af55 | | | 2 | import Link from "next/link"; |
| bf5fc33 | | | 3 | import { FileIcon } from "@/app/components/file-icon"; |
| bc1b2ba | | | 4 | import { encodePath } from "@/lib/utils"; |
| bc1b2ba | | | 5 | import { getRepoTree } from "@/lib/grove-api"; |
| 3e3af55 | | | 6 | |
| 3e3af55 | | | 7 | interface Props { |
| 3e3af55 | | | 8 | params: Promise<{ owner: string; repo: string; path: string[] }>; |
| 3e3af55 | | | 9 | } |
| 3e3af55 | | | 10 | |
| 1da9874 | | | 11 | export async function generateMetadata({ params }: Props): Promise<Metadata> { |
| 86450dc | | | 12 | const { repo, path: pathParts } = await params; |
| 1da9874 | | | 13 | const path = pathParts.slice(1).join("/"); |
| 86450dc | | | 14 | return { title: `${path || "/"} · ${repo}` }; |
| 1da9874 | | | 15 | } |
| 1da9874 | | | 16 | |
| 3e3af55 | | | 17 | |
| bf5fc33 | | | 18 | function sortEntries(entries: any[]) { |
| bf5fc33 | | | 19 | return [...entries].sort((a, b) => { |
| bf5fc33 | | | 20 | if (a.type === b.type) return a.name.localeCompare(b.name); |
| bf5fc33 | | | 21 | return a.type === "tree" ? -1 : 1; |
| bf5fc33 | | | 22 | }); |
| bf5fc33 | | | 23 | } |
| bf5fc33 | | | 24 | |
| 12ffdd4 | | | 25 | export default async function TreePage({ params }: Props) { |
| 3e3af55 | | | 26 | const { owner, repo, path: pathParts } = await params; |
| 12ffdd4 | | | 27 | const ref = pathParts[0] ?? "main"; |
| 12ffdd4 | | | 28 | const path = pathParts.slice(1).join("/"); |
| 3e3af55 | | | 29 | |
| bc1b2ba | | | 30 | const tree = await getRepoTree(owner, repo, ref, path); |
| 3e3af55 | | | 31 | |
| 3e3af55 | | | 32 | if (!tree) { |
| 3e3af55 | | | 33 | return ( |
| 135dfe5 | | | 34 | <div className="max-w-3xl mx-auto px-4 py-16"> |
| cf89d3c | | | 35 | <h1 className="text-lg" style={{ color: "var(--text-secondary)" }}> |
| 135dfe5 | | | 36 | Path not found |
| 135dfe5 | | | 37 | </h1> |
| 3e3af55 | | | 38 | </div> |
| 3e3af55 | | | 39 | ); |
| 3e3af55 | | | 40 | } |
| 3e3af55 | | | 41 | |
| 3e3af55 | | | 42 | const parts = path.split("/"); |
| 3e3af55 | | | 43 | |
| 3e3af55 | | | 44 | return ( |
| 4dfd09b | | | 45 | <div className="px-4 py-6"> |
| bf5fc33 | | | 46 | <div className="text-sm" style={{ border: "1px solid var(--border-subtle)" }}> |
| bf5fc33 | | | 47 | <Link |
| bf5fc33 | | | 48 | href={ |
| bf5fc33 | | | 49 | parts.length > 1 |
| d744b82 | | | 50 | ? `/${owner}/${repo}/tree/${ref}/${encodePath(parts.slice(0, -1).join("/"))}` |
| bf5fc33 | | | 51 | : `/${owner}/${repo}` |
| bf5fc33 | | | 52 | } |
| bf5fc33 | | | 53 | className="flex items-center gap-2 py-1.5 pl-3 pr-3 hover-row" |
| bf5fc33 | | | 54 | style={{ |
| bf5fc33 | | | 55 | borderBottom: "1px solid var(--divide)", |
| bf5fc33 | | | 56 | color: "var(--text-muted)", |
| bf5fc33 | | | 57 | }} |
| bf5fc33 | | | 58 | > |
| bf5fc33 | | | 59 | <FileIcon type="tree" /> |
| bf5fc33 | | | 60 | <span className="hover:underline">..</span> |
| bf5fc33 | | | 61 | </Link> |
| bf5fc33 | | | 62 | {sortEntries(tree.entries).map((entry: any) => ( |
| bf5fc33 | | | 63 | <Link |
| bf5fc33 | | | 64 | key={entry.name} |
| bf5fc33 | | | 65 | href={ |
| bf5fc33 | | | 66 | entry.type === "tree" |
| d744b82 | | | 67 | ? `/${owner}/${repo}/tree/${ref}/${encodePath(path)}/${encodePath(entry.name)}` |
| d744b82 | | | 68 | : `/${owner}/${repo}/blob/${ref}/${encodePath(path)}/${encodePath(entry.name)}` |
| bf5fc33 | | | 69 | } |
| bf5fc33 | | | 70 | className="flex items-center gap-2 py-1.5 pl-3 pr-3 hover-row" |
| bf5fc33 | | | 71 | style={{ |
| bf5fc33 | | | 72 | borderTop: "1px solid var(--divide)", |
| bf5fc33 | | | 73 | color: "var(--accent)", |
| bf5fc33 | | | 74 | }} |
| bf5fc33 | | | 75 | > |
| bf5fc33 | | | 76 | <FileIcon type={entry.type} name={entry.name} /> |
| bf5fc33 | | | 77 | <span className="hover:underline">{entry.name}</span> |
| bf5fc33 | | | 78 | </Link> |
| bf5fc33 | | | 79 | ))} |
| bf5fc33 | | | 80 | </div> |
| 3e3af55 | | | 81 | </div> |
| 3e3af55 | | | 82 | ); |
| 3e3af55 | | | 83 | } |