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