| 1 | using System.Text; |
| 2 | using AgentForge.Abstractions; |
| 3 | using AgentForge.Data; |
| 4 | using AgentForge.Data.Entities; |
| 5 | using AgentForge.Services; |
| 6 | |
| 7 | namespace AgentForge.Plugin.View; |
| 8 | |
| 9 | public static class ForgeViewPresenter |
| 10 | { |
| 11 | internal static ForgeViewPageContext RepoContext( |
| 12 | ForgeRepoEntity repo, |
| 13 | string activeTab, |
| 14 | ForgeRepository repository, |
| 15 | HttpContext? http, |
| 16 | string? searchQuery = null) |
| 17 | { |
| 18 | var auth = http?.RequestServices.GetService<ForgeAuthService>(); |
| 19 | var repoNames = repository.ListRepos() |
| 20 | .Select(r => r.Name) |
| 21 | .OrderBy(n => n, StringComparer.OrdinalIgnoreCase) |
| 22 | .ToList(); |
| 23 | var catalog = http?.RequestServices.GetService<IForgeViewContributorCatalog>(); |
| 24 | var contributors = catalog?.Contributors ?? []; |
| 25 | return new( |
| 26 | repo.Name, |
| 27 | repoNames, |
| 28 | activeTab, |
| 29 | repository.ListIssues(repo.Id).Count, |
| 30 | repository.ListMergeRequests(repo.Id).Count, |
| 31 | ForgeViewViewer.Resolve(http, auth), |
| 32 | searchQuery, |
| 33 | contributors); |
| 34 | } |
| 35 | |
| 36 | public static IResult RepoPage( |
| 37 | string title, |
| 38 | ForgeRepoEntity repo, |
| 39 | string activeTab, |
| 40 | ForgeRepository repository, |
| 41 | string bodyHtml, |
| 42 | HttpContext http, |
| 43 | string? searchQuery = null) => |
| 44 | Html(ForgeViewLayout.Page(title, bodyHtml, RepoContext(repo, activeTab, repository, http, searchQuery))); |
| 45 | |
| 46 | internal static IResult Html(string html) => |
| 47 | Results.Content(html, "text/html", Encoding.UTF8); |
| 48 | |
| 49 | public static IResult Page(string title, string bodyHtml, HttpContext http) |
| 50 | { |
| 51 | var auth = http.RequestServices.GetService<ForgeAuthService>(); |
| 52 | var ctx = new ForgeViewPageContext("", [], Viewer: ForgeViewViewer.Resolve(http, auth)); |
| 53 | return Html(ForgeViewLayout.Page(title, bodyHtml, ctx)); |
| 54 | } |
| 55 | |
| 56 | public static ForgeViewViewerContext ResolveViewer(HttpContext http) |
| 57 | { |
| 58 | var auth = http.RequestServices.GetService<ForgeAuthService>(); |
| 59 | return ForgeViewViewer.Resolve(http, auth); |
| 60 | } |
| 61 | |
| 62 | public static string JsonEncode(string value) => |
| 63 | System.Text.Json.JsonSerializer.Serialize(value); |
| 64 | } |
| 65 | |