| 1 | using AgentForge.Abstractions; |
| 2 | using AgentForge.Contracts; |
| 3 | using AgentForge.Data; |
| 4 | using AgentForge.Plugin.View; |
| 5 | using AgentForge.Plugin.View.Components; |
| 6 | using AgentForge.Plugin.View.Components.Layout; |
| 7 | |
| 8 | namespace AgentForge.Plugin.Org.Web.Components; |
| 9 | |
| 10 | internal sealed class ForgeMainPageCatalogBody(ForgeRepository repository) : IForgeMainPageCatalogBody |
| 11 | { |
| 12 | public string RenderTree( |
| 13 | IReadOnlyList<ForgeOrgCatalogRepoItem> repos, |
| 14 | in ForgeOrgCatalogViewContext groupingContext, |
| 15 | IForgeOrgCatalogGrouping? grouping = null) |
| 16 | { |
| 17 | var responses = ForgeOrgCatalogRepoMapping.ToResponses(repos); |
| 18 | var orgs = repository.ListOrgs() |
| 19 | .OrderBy(org => org.DisplayName, StringComparer.OrdinalIgnoreCase) |
| 20 | .ToList(); |
| 21 | |
| 22 | var nodes = new List<string>(); |
| 23 | foreach (var org in orgs) |
| 24 | { |
| 25 | var orgRepos = responses |
| 26 | .Where(repo => string.Equals(repo.Org, org.Slug, StringComparison.OrdinalIgnoreCase)) |
| 27 | .ToList(); |
| 28 | var groups = repository.ListRepoGroups(org.Id); |
| 29 | if (orgRepos.Count == 0 && groups.Count == 0) |
| 30 | continue; |
| 31 | |
| 32 | var groupPaths = repository.BuildRepoGroupPathLookup(org.Id); |
| 33 | var catalogBasePath = $"/view/orgs/{Uri.EscapeDataString(org.Slug)}"; |
| 34 | var tree = ForgeViewOrgRepoTree.Render( |
| 35 | org.Slug, |
| 36 | catalogBasePath, |
| 37 | groups, |
| 38 | orgRepos, |
| 39 | groupPaths, |
| 40 | groupingContext, |
| 41 | grouping, |
| 42 | groupingContext.ActiveGroupPath); |
| 43 | |
| 44 | var meta = ForgeHtml.Span("meta", ForgeHtml.Text($"{orgRepos.Count} repos")); |
| 45 | var summary = ForgeHtml.Fragment( |
| 46 | ForgeHtml.A(catalogBasePath, org.DisplayName), |
| 47 | ForgeHtml.Text(" "), |
| 48 | meta); |
| 49 | var details = ForgeViewDisclosure.Render( |
| 50 | summary, |
| 51 | tree, |
| 52 | open: orgs.Count == 1, |
| 53 | "org-group-details global-org-section", |
| 54 | "org-group-head global-org-head"); |
| 55 | |
| 56 | nodes.Add(ForgeHtml.Li("global-org-node", details)); |
| 57 | } |
| 58 | |
| 59 | if (nodes.Count == 0) |
| 60 | return ForgeViewEmptyState.Render("No repository groups or repos yet."); |
| 61 | |
| 62 | return ForgeHtml.Ul("plain global-org-catalog", [.. nodes]); |
| 63 | } |
| 64 | } |
| 65 | |