| 1 | using AgentForge.Services; |
| 2 | |
| 3 | namespace AgentForge.Plugin.View.Components.Code; |
| 4 | |
| 5 | internal static class ForgeViewFileTree |
| 6 | { |
| 7 | internal static string Render( |
| 8 | string repoName, |
| 9 | string branch, |
| 10 | IReadOnlyList<GitRepoService.GitTreeItem> entries, |
| 11 | string? parentPath = null) |
| 12 | { |
| 13 | var items = new List<string>(); |
| 14 | |
| 15 | if (!string.IsNullOrWhiteSpace(parentPath)) |
| 16 | { |
| 17 | var parent = ForgeGitViewHelpers.ParentPath(parentPath); |
| 18 | var parentHref = BuildTreeHref(repoName, branch, parent); |
| 19 | items.Add(ForgeHtml.Li(null, |
| 20 | ForgeHtml.A(parentHref, ".."), |
| 21 | ForgeHtml.Text(" "), |
| 22 | ForgeHtml.Span("meta", ForgeHtml.Text("parent")))); |
| 23 | } |
| 24 | |
| 25 | if (entries.Count == 0) |
| 26 | items.Add(ForgeHtml.Li("meta", ForgeHtml.Text("empty"))); |
| 27 | else |
| 28 | items.AddRange(RenderEntries(repoName, branch, entries)); |
| 29 | |
| 30 | return ForgeHtml.Ul("plain file-tree", [.. items]); |
| 31 | } |
| 32 | |
| 33 | internal static string RenderEntriesOnly(string repoName, string branch, IReadOnlyList<GitRepoService.GitTreeItem> entries) => |
| 34 | ForgeHtml.Ul("plain file-tree", [.. RenderEntries(repoName, branch, entries)]); |
| 35 | |
| 36 | private static IEnumerable<string> RenderEntries(string repoName, string branch, IReadOnlyList<GitRepoService.GitTreeItem> entries) |
| 37 | { |
| 38 | foreach (var entry in entries) |
| 39 | { |
| 40 | if (entry.IsDirectory) |
| 41 | { |
| 42 | var href = $"/view/repos/{ForgeRepoNames.ToUrlPath(repoName)}/tree?branch={Uri.EscapeDataString(branch)}&path={Uri.EscapeDataString(entry.Path)}"; |
| 43 | yield return ForgeHtml.Li(null, |
| 44 | ForgeHtml.Text("📁 "), |
| 45 | ForgeHtml.ARaw(href, ForgeHtml.Fragment(ForgeHtml.Text(entry.Name), ForgeHtml.Text("/")))); |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | var href = $"/view/repos/{ForgeRepoNames.ToUrlPath(repoName)}/blob?branch={Uri.EscapeDataString(branch)}&path={Uri.EscapeDataString(entry.Path)}"; |
| 50 | yield return ForgeHtml.Li(null, |
| 51 | ForgeHtml.Text("📄 "), |
| 52 | ForgeHtml.A(href, entry.Name)); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | private static string BuildTreeHref(string repoName, string branch, string? path) |
| 58 | { |
| 59 | var href = $"/view/repos/{ForgeRepoNames.ToUrlPath(repoName)}/tree?branch={Uri.EscapeDataString(branch)}"; |
| 60 | if (!string.IsNullOrEmpty(path)) |
| 61 | href += $"&path={Uri.EscapeDataString(path)}"; |
| 62 | return href; |
| 63 | } |
| 64 | } |
| 65 | |