| e4a233c | | | 1 | import type { Metadata } from "next"; |
| 3e3af55 | | | 2 | import Link from "next/link"; |
| bf5fc33 | | | 3 | import { FileIcon } from "@/app/components/file-icon"; |
| bf5fc33 | | | 4 | import { Markdown } from "@/app/components/markdown"; |
| 59a80f9 | | | 5 | import { GitImportForm } from "@/app/components/git-import-form"; |
| 2dc32fd | | | 6 | import { groveApiUrl, encodePath, timeAgo } from "@/lib/utils"; |
| 3e3af55 | | | 7 | |
| 3e3af55 | | | 8 | interface Props { |
| 3e3af55 | | | 9 | params: Promise<{ owner: string; repo: string }>; |
| 3e3af55 | | | 10 | } |
| 3e3af55 | | | 11 | |
| e4a233c | | | 12 | export async function generateMetadata({ params }: Props): Promise<Metadata> { |
| 86450dc | | | 13 | const { repo } = await params; |
| 86450dc | | | 14 | return { title: `Grove · ${repo}` }; |
| e4a233c | | | 15 | } |
| e4a233c | | | 16 | |
| 80fafdf | | | 17 | async function getTree(owner: string, repo: string, ref: string) { |
| f0bb192 | | | 18 | const res = await fetch(`${groveApiUrl}/api/repos/${owner}/${repo}/tree/${ref}`, { |
| 3e3af55 | | | 19 | cache: "no-store", |
| 3e3af55 | | | 20 | }); |
| 3e3af55 | | | 21 | if (!res.ok) return null; |
| 3e3af55 | | | 22 | return res.json(); |
| 3e3af55 | | | 23 | } |
| 3e3af55 | | | 24 | |
| 80fafdf | | | 25 | async function getBlob(owner: string, repo: string, ref: string, path: string) { |
| f0bb192 | | | 26 | const res = await fetch(`${groveApiUrl}/api/repos/${owner}/${repo}/blob/${ref}/${path}`, { |
| 3e3af55 | | | 27 | cache: "no-store", |
| 3e3af55 | | | 28 | }); |
| 3e3af55 | | | 29 | if (!res.ok) return null; |
| 3e3af55 | | | 30 | return res.json(); |
| 3e3af55 | | | 31 | } |
| 3e3af55 | | | 32 | |
| 80fafdf | | | 33 | async function getBookmarks(owner: string, repo: string) { |
| f0bb192 | | | 34 | const res = await fetch(`${groveApiUrl}/api/repos/${owner}/${repo}/branches`, { |
| bf5fc33 | | | 35 | cache: "no-store", |
| bf5fc33 | | | 36 | }); |
| bf5fc33 | | | 37 | if (!res.ok) return null; |
| bf5fc33 | | | 38 | return res.json(); |
| bf5fc33 | | | 39 | } |
| bf5fc33 | | | 40 | |
| 2dc32fd | | | 41 | async function getLatestCommit(owner: string, repo: string, ref: string) { |
| 2dc32fd | | | 42 | const res = await fetch( |
| 2dc32fd | | | 43 | `${groveApiUrl}/api/repos/${owner}/${repo}/commits/${ref}?limit=1`, |
| 2dc32fd | | | 44 | { cache: "no-store" } |
| 2dc32fd | | | 45 | ); |
| 2dc32fd | | | 46 | if (!res.ok) return null; |
| 2dc32fd | | | 47 | return res.json(); |
| 2dc32fd | | | 48 | } |
| 2dc32fd | | | 49 | |
| 80fafdf | | | 50 | async function findReadme(owner: string, repo: string, ref: string, entries: any[]) { |
| bf5fc33 | | | 51 | const readmeNames = ["README.md", "README", "readme.md", "README.txt"]; |
| bf5fc33 | | | 52 | const readmeEntry = entries.find( |
| f0bb192 | | | 53 | (e: any) => e.type !== "tree" && readmeNames.includes(e.name) |
| bf5fc33 | | | 54 | ); |
| bf5fc33 | | | 55 | if (!readmeEntry) return null; |
| 80fafdf | | | 56 | const blob = await getBlob(owner, repo, ref, readmeEntry.name); |
| bf5fc33 | | | 57 | return blob?.content ?? null; |
| bf5fc33 | | | 58 | } |
| bf5fc33 | | | 59 | |
| bf5fc33 | | | 60 | function sortEntries(entries: any[]) { |
| bf5fc33 | | | 61 | return [...entries].sort((a, b) => { |
| bf5fc33 | | | 62 | if (a.type === b.type) return a.name.localeCompare(b.name); |
| bf5fc33 | | | 63 | return a.type === "tree" ? -1 : 1; |
| bf5fc33 | | | 64 | }); |
| bf5fc33 | | | 65 | } |
| bf5fc33 | | | 66 | |
| 3e3af55 | | | 67 | export default async function RepoPage({ params }: Props) { |
| 3e3af55 | | | 68 | const { owner, repo } = await params; |
| 3e3af55 | | | 69 | |
| 80fafdf | | | 70 | const bookmarks = await getBookmarks(owner, repo); |
| bf5fc33 | | | 71 | if (!bookmarks) { |
| 3e3af55 | | | 72 | return ( |
| 530592e | | | 73 | <div className="py-10"> |
| cf89d3c | | | 74 | <h1 className="text-lg" style={{ color: "var(--text-secondary)" }}> |
| 135dfe5 | | | 75 | Repository not found |
| 135dfe5 | | | 76 | </h1> |
| 135dfe5 | | | 77 | <p className="text-sm mt-1" style={{ color: "var(--text-muted)" }}> |
| 3e3af55 | | | 78 | {owner}/{repo} does not exist or you don't have access. |
| 3e3af55 | | | 79 | </p> |
| 3e3af55 | | | 80 | </div> |
| 3e3af55 | | | 81 | ); |
| 3e3af55 | | | 82 | } |
| 3e3af55 | | | 83 | |
| f0bb192 | | | 84 | const branches = bookmarks.branches ?? bookmarks.bookmarks ?? []; |
| f0bb192 | | | 85 | const mainBookmark = branches.find((b: any) => b.name === "main"); |
| f0bb192 | | | 86 | const ref = mainBookmark?.name ?? branches[0]?.name ?? "main"; |
| 2dc32fd | | | 87 | const [tree, latestCommitData] = await Promise.all([ |
| 2dc32fd | | | 88 | getTree(owner, repo, ref), |
| 2dc32fd | | | 89 | getLatestCommit(owner, repo, ref), |
| 2dc32fd | | | 90 | ]); |
| 80fafdf | | | 91 | const readme = tree ? await findReadme(owner, repo, ref, tree.entries) : null; |
| bf5fc33 | | | 92 | const isMarkdown = readme !== null; |
| 2dc32fd | | | 93 | const latestCommit = latestCommitData?.commits?.[0] ?? null; |
| bc2f205 | | | 94 | const sortedEntries = tree?.entries ? sortEntries(tree.entries) : []; |
| bc2f205 | | | 95 | const isEmptyRepo = !latestCommit && sortedEntries.length === 0; |
| 2dc32fd | | | 96 | const latestCommitAuthor = |
| 2dc32fd | | | 97 | latestCommit?.author?.split("<")[0]?.trim() ?? latestCommit?.author ?? ""; |
| 2dc32fd | | | 98 | const latestCommitInitial = latestCommitAuthor?.[0]?.toUpperCase() ?? "?"; |
| 2dc32fd | | | 99 | const latestCommitSubject = |
| 2dc32fd | | | 100 | latestCommit?.subject ?? latestCommit?.message?.split("\n")[0] ?? "Commit"; |
| bc2f205 | | | 101 | const emptyRepoOptions = [ |
| bc2f205 | | | 102 | { |
| bc2f205 | | | 103 | title: "Clone empty and start", |
| bc2f205 | | | 104 | description: "Create the first commit in this repository.", |
| bc2f205 | | | 105 | commands: [ |
| 8d8e815 | | | 106 | `grove clone ${owner}/${repo}`, |
| bc2f205 | | | 107 | `cd ${repo}`, |
| bc2f205 | | | 108 | `echo "# ${repo}" > README.md`, |
| bc2f205 | | | 109 | "sl add README.md", |
| bc2f205 | | | 110 | 'sl commit -m "Initial commit"', |
| bc2f205 | | | 111 | `sl push --to ${ref}`, |
| bc2f205 | | | 112 | ], |
| bc2f205 | | | 113 | }, |
| bc2f205 | | | 114 | { |
| bc2f205 | | | 115 | title: "Push existing Sapling repo", |
| bc2f205 | | | 116 | description: "Point your current Sapling repo at this remote and push.", |
| bc2f205 | | | 117 | commands: [ |
| bc2f205 | | | 118 | "cd ~/src/my-existing-repo", |
| 4ab1317 | | | 119 | `sl path default "slapi:${repo}"`, |
| bc2f205 | | | 120 | `sl push --to ${ref}`, |
| bc2f205 | | | 121 | ], |
| bc2f205 | | | 122 | }, |
| bc2f205 | | | 123 | ]; |
| 3e3af55 | | | 124 | |
| 3e3af55 | | | 125 | return ( |
| 530592e | | | 126 | <> |
| 2dc32fd | | | 127 | {latestCommit && ( |
| 2dc32fd | | | 128 | <div className="mb-3 text-sm" style={{ border: "1px solid var(--border-subtle)" }}> |
| 2dc32fd | | | 129 | <div className="flex items-center gap-3 py-2 pl-3 pr-3"> |
| 2dc32fd | | | 130 | <span |
| 2dc32fd | | | 131 | title={latestCommit.author} |
| 2dc32fd | | | 132 | style={{ |
| 2dc32fd | | | 133 | display: "inline-flex", |
| 2dc32fd | | | 134 | alignItems: "center", |
| 2dc32fd | | | 135 | justifyContent: "center", |
| 2dc32fd | | | 136 | width: 22, |
| 2dc32fd | | | 137 | height: 22, |
| 2dc32fd | | | 138 | borderRadius: "50%", |
| 2dc32fd | | | 139 | backgroundColor: "var(--bg-inset)", |
| 2dc32fd | | | 140 | border: "1px solid var(--border-subtle)", |
| 2dc32fd | | | 141 | color: "var(--text-muted)", |
| 2dc32fd | | | 142 | fontSize: "0.65rem", |
| 2dc32fd | | | 143 | cursor: "default", |
| 2dc32fd | | | 144 | }} |
| 2dc32fd | | | 145 | > |
| 2dc32fd | | | 146 | {latestCommitInitial} |
| 2dc32fd | | | 147 | </span> |
| 2dc32fd | | | 148 | <div className="min-w-0 flex-1"> |
| 2dc32fd | | | 149 | <div className="truncate"> |
| 2dc32fd | | | 150 | <Link |
| 2dc32fd | | | 151 | href={`/${owner}/${repo}/commit/${latestCommit.hash}`} |
| 2dc32fd | | | 152 | className="hover:underline" |
| 2dc32fd | | | 153 | style={{ color: "var(--text-primary)" }} |
| 2dc32fd | | | 154 | > |
| 2dc32fd | | | 155 | {latestCommitSubject} |
| 2dc32fd | | | 156 | </Link> |
| 2dc32fd | | | 157 | </div> |
| 2dc32fd | | | 158 | <div className="text-xs" style={{ color: "var(--text-faint)" }}> |
| 2dc32fd | | | 159 | Most recent commit {latestCommitAuthor ? `by ${latestCommitAuthor}` : ""} |
| 2dc32fd | | | 160 | </div> |
| 2dc32fd | | | 161 | </div> |
| 2dc32fd | | | 162 | <Link |
| 2dc32fd | | | 163 | href={`/${owner}/${repo}/commit/${latestCommit.hash}`} |
| 2dc32fd | | | 164 | className="font-mono text-xs hover:underline" |
| 2dc32fd | | | 165 | style={{ color: "var(--accent)" }} |
| 2dc32fd | | | 166 | > |
| 2dc32fd | | | 167 | {latestCommit.hash.slice(0, 7)} |
| 2dc32fd | | | 168 | </Link> |
| 2dc32fd | | | 169 | <span className="text-xs shrink-0" style={{ color: "var(--text-faint)" }}> |
| 2dc32fd | | | 170 | {timeAgo(latestCommit.timestamp)} |
| 2dc32fd | | | 171 | </span> |
| 2dc32fd | | | 172 | </div> |
| 2dc32fd | | | 173 | </div> |
| 2dc32fd | | | 174 | )} |
| 2dc32fd | | | 175 | |
| bc2f205 | | | 176 | {isEmptyRepo ? ( |
| bc2f205 | | | 177 | <div |
| bc2f205 | | | 178 | className="px-4 py-6" |
| bc2f205 | | | 179 | style={{ |
| bc2f205 | | | 180 | backgroundColor: "var(--bg-card)", |
| bc2f205 | | | 181 | border: "1px solid var(--border-subtle)", |
| bc2f205 | | | 182 | }} |
| bc2f205 | | | 183 | > |
| bc2f205 | | | 184 | <p className="text-sm" style={{ color: "var(--text-secondary)" }}> |
| bc2f205 | | | 185 | This repository is empty |
| bc2f205 | | | 186 | </p> |
| bc2f205 | | | 187 | <p className="text-xs mt-1 mb-4" style={{ color: "var(--text-faint)" }}> |
| bc2f205 | | | 188 | Push your first commit to <span className="font-mono">{ref}</span> to get started. |
| bc2f205 | | | 189 | </p> |
| bc2f205 | | | 190 | <div className="grid gap-3"> |
| 59a80f9 | | | 191 | <GitImportForm owner={owner} repo={repo} /> |
| bc2f205 | | | 192 | {emptyRepoOptions.map((option) => ( |
| bc2f205 | | | 193 | <div |
| bc2f205 | | | 194 | key={option.title} |
| bdf6540 | | | 195 | className="text-left p-3 min-w-0 overflow-hidden" |
| bc2f205 | | | 196 | style={{ |
| bc2f205 | | | 197 | backgroundColor: "var(--bg-inset)", |
| bc2f205 | | | 198 | border: "1px solid var(--border-subtle)", |
| bc2f205 | | | 199 | }} |
| bc2f205 | | | 200 | > |
| bc2f205 | | | 201 | <p className="text-xs mb-1" style={{ color: "var(--text-secondary)" }}> |
| bc2f205 | | | 202 | {option.title} |
| bc2f205 | | | 203 | </p> |
| bc2f205 | | | 204 | <p className="text-xs mb-2" style={{ color: "var(--text-faint)" }}> |
| bc2f205 | | | 205 | {option.description} |
| bc2f205 | | | 206 | </p> |
| bc2f205 | | | 207 | <pre |
| bc2f205 | | | 208 | className="text-xs whitespace-pre overflow-x-auto" |
| bc2f205 | | | 209 | style={{ color: "var(--text-muted)" }} |
| bc2f205 | | | 210 | > |
| bc2f205 | | | 211 | {option.commands.join("\n")} |
| bc2f205 | | | 212 | </pre> |
| bc2f205 | | | 213 | </div> |
| bc2f205 | | | 214 | ))} |
| bc2f205 | | | 215 | </div> |
| bc2f205 | | | 216 | </div> |
| bc2f205 | | | 217 | ) : tree && sortedEntries.length > 0 ? ( |
| 135dfe5 | | | 218 | <div> |
| bf5fc33 | | | 219 | <div |
| bf5fc33 | | | 220 | className="text-sm" |
| bf5fc33 | | | 221 | style={{ |
| bf5fc33 | | | 222 | border: "1px solid var(--border-subtle)", |
| bf5fc33 | | | 223 | }} |
| bf5fc33 | | | 224 | > |
| bc2f205 | | | 225 | {sortedEntries.map((entry: any, i: number) => ( |
| bf5fc33 | | | 226 | <Link |
| bf5fc33 | | | 227 | key={entry.name} |
| bf5fc33 | | | 228 | href={ |
| bf5fc33 | | | 229 | entry.type === "tree" |
| d744b82 | | | 230 | ? `/${owner}/${repo}/tree/${ref}/${encodePath(entry.name)}` |
| d744b82 | | | 231 | : `/${owner}/${repo}/blob/${ref}/${encodePath(entry.name)}` |
| bf5fc33 | | | 232 | } |
| bf5fc33 | | | 233 | className="flex items-center gap-2 py-1.5 pl-3 pr-3 hover-row" |
| bf5fc33 | | | 234 | style={{ |
| bf5fc33 | | | 235 | borderTop: |
| bf5fc33 | | | 236 | i > 0 ? "1px solid var(--divide)" : undefined, |
| bf5fc33 | | | 237 | color: "var(--accent)", |
| bf5fc33 | | | 238 | }} |
| bf5fc33 | | | 239 | > |
| bf5fc33 | | | 240 | <FileIcon type={entry.type} name={entry.name} /> |
| bf5fc33 | | | 241 | <span className="hover:underline">{entry.name}</span> |
| bf5fc33 | | | 242 | </Link> |
| bf5fc33 | | | 243 | ))} |
| bf5fc33 | | | 244 | </div> |
| 3e3af55 | | | 245 | </div> |
| bc2f205 | | | 246 | ) : null} |
| 3e3af55 | | | 247 | |
| bf5fc33 | | | 248 | {readme && ( |
| 135dfe5 | | | 249 | <div className="mt-8"> |
| bf5fc33 | | | 250 | {isMarkdown ? ( |
| bf5fc33 | | | 251 | <Markdown content={readme} /> |
| bf5fc33 | | | 252 | ) : ( |
| bf5fc33 | | | 253 | <pre |
| bf5fc33 | | | 254 | className="whitespace-pre-wrap text-sm p-4" |
| bf5fc33 | | | 255 | style={{ |
| bf5fc33 | | | 256 | color: "var(--text-secondary)", |
| bf5fc33 | | | 257 | backgroundColor: "var(--bg-card)", |
| bf5fc33 | | | 258 | border: "1px solid var(--border-subtle)", |
| bf5fc33 | | | 259 | }} |
| bf5fc33 | | | 260 | > |
| bf5fc33 | | | 261 | {readme} |
| bf5fc33 | | | 262 | </pre> |
| bf5fc33 | | | 263 | )} |
| 3e3af55 | | | 264 | </div> |
| 3e3af55 | | | 265 | )} |
| 530592e | | | 266 | </> |
| 3e3af55 | | | 267 | ); |
| 3e3af55 | | | 268 | } |