✨ feat: update [...path].astro page
swadhinbiswas committed 3 months ago
5f99cf3
1 parent c3d05c4
commit 5f99cf3
1 file changed
+51 -16
Showing 1 changed file+51-16
▼src/pages/[owner]/[repo]/tree/[branch]/[...path].astro
+51-16
| @@ -72,7 +72,25 @@ try { | ||
72 | 72 | } |
73 | 73 | |
74 | 74 | // 2. Git Operations |
75 | -const currentBranch = branch || repoData.defaultBranch; | |
75 | +// Fetch all branches | |
76 | +let branches: string[] = [repoData.defaultBranch]; | |
77 | +try { | |
78 | + const branchInfos = await getBranches(repoPath); | |
79 | + branches = branchInfos.map((b) => b.name); | |
80 | + // Ensure default branch is first | |
81 | + branches = [ | |
82 | + repoData.defaultBranch, | |
83 | + ...branches.filter((b) => b !== repoData.defaultBranch), | |
84 | + ]; | |
85 | +} catch (e) { | |
86 | + console.error("Error fetching branches:", e); | |
87 | +} | |
88 | + | |
89 | +// Use actual git branch if default branch doesn't exist in git | |
90 | +let currentBranch = branch || repoData.defaultBranch; | |
91 | +if (!branch && branches.length > 0 && !branches.includes(currentBranch)) { | |
92 | + currentBranch = branches[0]; // Use first actual branch from git | |
93 | +} | |
76 | 94 | const currentPath = path || ""; |
77 | 95 | let files: any[] = []; |
78 | 96 | let latestCommit: any = null; |
| @@ -163,19 +181,7 @@ const repo = { | ||
163 | 181 | openPrCount: repoData.openPrCount, |
164 | 182 | }; |
165 | 183 | |
166 | -// Fetch all branches | |
167 | -let branches: string[] = [repoData.defaultBranch]; | |
168 | -try { | |
169 | - const branchInfos = await getBranches(repoPath); | |
170 | - branches = branchInfos.map((b) => b.name); | |
171 | - // Ensure default branch is first | |
172 | - branches = [ | |
173 | - repoData.defaultBranch, | |
174 | - ...branches.filter((b) => b !== repoData.defaultBranch), | |
175 | - ]; | |
176 | -} catch (e) { | |
177 | - console.error("Error fetching branches:", e); | |
178 | -} | |
184 | +// Branches already fetched above | |
179 | 185 | --- |
180 | 186 | |
181 | 187 | <BaseLayout title={`${repo.owner}/${repo.name} - OpenCodeHub`}> |
| @@ -205,11 +211,13 @@ try { | ||
205 | 211 | class="hidden absolute left-0 top-full mt-1 w-64 rounded-md border bg-popover p-2 shadow-lg z-10" |
206 | 212 | > |
207 | 213 | <input |
214 | + id="branch-search" | |
208 | 215 | type="search" |
209 | 216 | placeholder="Find a branch..." |
210 | 217 | class="w-full mb-2 h-8 rounded-md border bg-background px-3 text-sm" |
218 | + onclick="event.stopPropagation()" | |
211 | 219 | /> |
212 | - <div class="max-h-64 overflow-y-auto"> | |
220 | + <div id="branch-list" class="max-h-64 overflow-y-auto"> | |
213 | 221 | <div |
214 | 222 | class="text-xs font-semibold text-muted-foreground px-2 py-1" |
215 | 223 | > |
| @@ -218,8 +226,9 @@ try { | ||
218 | 226 | { |
219 | 227 | branches.map((b) => ( |
220 | 228 | <a |
229 | + data-branch={b} | |
221 | 230 | href={`/${repo.owner}/${repo.name}/tree/${b}`} |
222 | - class={`flex items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover:bg-accent ${b === currentBranch ? "bg-accent" : ""}`} | |
231 | + class={`branch-item flex items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover:bg-accent ${b === currentBranch ? "bg-accent" : ""}`} | |
223 | 232 | > |
224 | 233 | {b === currentBranch && ( |
225 | 234 | <span class="text-primary">✓</span> |
| @@ -228,6 +237,15 @@ try { | ||
228 | 237 | </a> |
229 | 238 | )) |
230 | 239 | } |
240 | + <div class="border-t border-border mt-2 pt-2"> | |
241 | + <a | |
242 | + href={`/${repo.owner}/${repo.name}/compare?base=${repoData.defaultBranch}&head=${currentBranch}`} | |
243 | + class="flex items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover:bg-accent text-primary" | |
244 | + > | |
245 | + <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M5 12h14"/><path d="M12 5v14"/></svg> | |
246 | + Create new branch | |
247 | + </a> | |
248 | + </div> | |
231 | 249 | </div> |
232 | 250 | </div> |
233 | 251 | </div> |
| @@ -402,11 +420,28 @@ try { | ||
402 | 420 | // Branch dropdown |
403 | 421 | const branchBtn = document.getElementById("branch-btn"); |
404 | 422 | const branchDropdown = document.getElementById("branch-dropdown"); |
423 | + const branchSearch = document.getElementById("branch-search") as HTMLInputElement; | |
405 | 424 | |
406 | 425 | branchBtn?.addEventListener("click", (e) => { |
407 | 426 | e.stopPropagation(); |
408 | 427 | branchDropdown?.classList.toggle("hidden"); |
409 | 428 | document.getElementById("clone-dropdown")?.classList.add("hidden"); |
429 | + if (!branchDropdown?.classList.contains("hidden")) { | |
430 | + branchSearch?.focus(); | |
431 | + } | |
432 | + }); | |
433 | + | |
434 | + // Branch search/filter | |
435 | + branchSearch?.addEventListener("input", (e) => { | |
436 | + const query = (e.target as HTMLInputElement).value.toLowerCase(); | |
437 | + document.querySelectorAll(".branch-item").forEach((el) => { | |
438 | + const branch = el.getAttribute("data-branch") || ""; | |
439 | + (el as HTMLElement).style.display = branch.toLowerCase().includes(query) ? "" : "none"; | |
440 | + }); | |
441 | + }); | |
442 | + | |
443 | + branchSearch?.addEventListener("click", (e) => { | |
444 | + e.stopPropagation(); | |
410 | 445 | }); |
411 | 446 | |
412 | 447 | // Clone dropdown |
413 | 448 | |