| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Chronicis.Client.Models; |
| | | 4 | | using Chronicis.Client.Services; |
| | | 5 | | using Chronicis.Shared.DTOs; |
| | | 6 | | using Microsoft.AspNetCore.Components; |
| | | 7 | | |
| | | 8 | | namespace Chronicis.Client.Components.Admin; |
| | | 9 | | |
| | | 10 | | public partial class RenderDefinitionGenerator : ComponentBase |
| | | 11 | | { |
| | 0 | 12 | | [Inject] private IExternalLinkApiService ExternalLinkApi { get; set; } = default!; |
| | 0 | 13 | | [Inject] private IRenderDefinitionService RenderDefService { get; set; } = default!; |
| | 0 | 14 | | [Inject] private ILogger<RenderDefinitionGenerator> Logger { get; set; } = default!; |
| | | 15 | | |
| | 0 | 16 | | private string _selectedSource = "ros"; |
| | 0 | 17 | | private string _recordId = ""; |
| | | 18 | | private bool _isLoading; |
| | | 19 | | private string? _loadError; |
| | | 20 | | private string? _existingDefStatus; |
| | | 21 | | |
| | | 22 | | private ExternalLinkContentDto? _sampleContent; |
| | 0 | 23 | | private string _definitionJson = ""; |
| | | 24 | | private string? _jsonError; |
| | | 25 | | private bool _definitionDirty; |
| | | 26 | | private RenderDefinition? _activeDefinition; |
| | | 27 | | |
| | | 28 | | // Autocomplete support |
| | 0 | 29 | | private static readonly JsonSerializerOptions WriteOptions = new() |
| | 0 | 30 | | { |
| | 0 | 31 | | WriteIndented = true, |
| | 0 | 32 | | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull |
| | 0 | 33 | | }; |
| | | 34 | | |
| | | 35 | | private string SuggestedFilePath |
| | | 36 | | { |
| | | 37 | | get |
| | | 38 | | { |
| | 0 | 39 | | if (string.IsNullOrEmpty(_recordId)) |
| | 0 | 40 | | return ""; |
| | 0 | 41 | | var parts = _recordId.Split('/'); |
| | 0 | 42 | | var category = parts.Length > 0 ? parts[0] : "unknown"; |
| | 0 | 43 | | return $"wwwroot/render-definitions/{_selectedSource}/{category}.json"; |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | private async Task<IEnumerable<string>> SearchRecords(string query, CancellationToken ct) |
| | | 48 | | { |
| | 0 | 49 | | if (string.IsNullOrWhiteSpace(query) || query.Length < 2) |
| | 0 | 50 | | return Enumerable.Empty<string>(); |
| | | 51 | | |
| | | 52 | | try |
| | | 53 | | { |
| | 0 | 54 | | var results = await ExternalLinkApi.GetSuggestionsAsync( |
| | 0 | 55 | | null, _selectedSource, query, ct); |
| | 0 | 56 | | return results.Select(r => r.Id); |
| | | 57 | | } |
| | 0 | 58 | | catch (OperationCanceledException) |
| | | 59 | | { |
| | 0 | 60 | | return Enumerable.Empty<string>(); |
| | | 61 | | } |
| | 0 | 62 | | catch (Exception ex) |
| | | 63 | | { |
| | 0 | 64 | | Logger.LogWarning(ex, "Autocomplete search failed for {Query}", query); |
| | 0 | 65 | | return Enumerable.Empty<string>(); |
| | | 66 | | } |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | private async Task OnRecordSelected(string? value) |
| | | 70 | | { |
| | 0 | 71 | | _recordId = value ?? ""; |
| | 0 | 72 | | if (!string.IsNullOrWhiteSpace(_recordId)) |
| | 0 | 73 | | await LoadSampleRecord(); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | private async Task LoadSampleRecord() |
| | | 77 | | { |
| | 0 | 78 | | if (string.IsNullOrWhiteSpace(_recordId)) |
| | | 79 | | { |
| | 0 | 80 | | _loadError = "Please enter a record ID."; |
| | 0 | 81 | | return; |
| | | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | _isLoading = true; |
| | 0 | 85 | | _loadError = null; |
| | 0 | 86 | | _existingDefStatus = null; |
| | 0 | 87 | | _sampleContent = null; |
| | 0 | 88 | | _activeDefinition = null; |
| | 0 | 89 | | _definitionJson = ""; |
| | 0 | 90 | | StateHasChanged(); |
| | | 91 | | |
| | | 92 | | try |
| | | 93 | | { |
| | 0 | 94 | | var content = await ExternalLinkApi.GetContentAsync( |
| | 0 | 95 | | _selectedSource, _recordId.Trim(), CancellationToken.None); |
| | | 96 | | |
| | 0 | 97 | | if (content == null) |
| | | 98 | | { |
| | 0 | 99 | | _loadError = $"Record not found: {_selectedSource}/{_recordId}"; |
| | | 100 | | } |
| | 0 | 101 | | else if (string.IsNullOrWhiteSpace(content.JsonData)) |
| | | 102 | | { |
| | 0 | 103 | | _loadError = "Record loaded but has no JsonData — structured rendering unavailable."; |
| | 0 | 104 | | _sampleContent = content; |
| | | 105 | | } |
| | | 106 | | else |
| | | 107 | | { |
| | 0 | 108 | | _sampleContent = content; |
| | 0 | 109 | | Logger.LogInformation("Loaded sample record: {Id} with {Len} bytes of JSON", |
| | 0 | 110 | | content.Id, content.JsonData.Length); |
| | | 111 | | |
| | | 112 | | // Try to load an existing render definition for this record's category |
| | 0 | 113 | | await TryLoadExistingDefinition(); |
| | | 114 | | } |
| | 0 | 115 | | } |
| | 0 | 116 | | catch (Exception ex) |
| | | 117 | | { |
| | 0 | 118 | | _loadError = $"Failed to load record: {ex.Message}"; |
| | 0 | 119 | | Logger.LogError(ex, "Error loading sample record {Source}/{Id}", _selectedSource, _recordId); |
| | 0 | 120 | | } |
| | | 121 | | finally |
| | | 122 | | { |
| | 0 | 123 | | _isLoading = false; |
| | | 124 | | } |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | private async Task TryLoadExistingDefinition() |
| | | 128 | | { |
| | 0 | 129 | | if (_sampleContent == null) |
| | 0 | 130 | | return; |
| | | 131 | | |
| | | 132 | | try |
| | | 133 | | { |
| | 0 | 134 | | var lastSlash = _sampleContent.Id.LastIndexOf('/'); |
| | 0 | 135 | | var categoryPath = lastSlash > 0 ? _sampleContent.Id[..lastSlash] : null; |
| | | 136 | | |
| | 0 | 137 | | var existing = await RenderDefService.ResolveAsync(_sampleContent.Source, categoryPath); |
| | | 138 | | |
| | | 139 | | // Check if it's the built-in default or an actual file definition |
| | | 140 | | // The built-in default has no DisplayName and empty Sections |
| | 0 | 141 | | var isDefault = existing.Sections.Count == 0 && string.IsNullOrEmpty(existing.DisplayName); |
| | | 142 | | |
| | 0 | 143 | | if (!isDefault) |
| | | 144 | | { |
| | 0 | 145 | | _definitionJson = JsonSerializer.Serialize(existing, WriteOptions); |
| | 0 | 146 | | _activeDefinition = existing; |
| | 0 | 147 | | _definitionDirty = false; |
| | 0 | 148 | | _jsonError = null; |
| | 0 | 149 | | _existingDefStatus = $"Loaded existing definition ({existing.Sections.Count} sections)"; |
| | 0 | 150 | | Logger.LogInformation("Loaded existing render definition for {Source}/{Category}", |
| | 0 | 151 | | _sampleContent.Source, categoryPath); |
| | | 152 | | } |
| | | 153 | | else |
| | | 154 | | { |
| | 0 | 155 | | _existingDefStatus = "No custom definition found — use Auto-Generate to create one."; |
| | | 156 | | } |
| | 0 | 157 | | } |
| | 0 | 158 | | catch (Exception ex) |
| | | 159 | | { |
| | 0 | 160 | | Logger.LogWarning(ex, "Failed to load existing render definition"); |
| | 0 | 161 | | _existingDefStatus = "Could not check for existing definition."; |
| | 0 | 162 | | } |
| | 0 | 163 | | } |
| | | 164 | | |
| | | 165 | | private void AutoGenerate() |
| | | 166 | | { |
| | 0 | 167 | | if (_sampleContent?.JsonData == null) |
| | 0 | 168 | | return; |
| | | 169 | | |
| | | 170 | | try |
| | | 171 | | { |
| | 0 | 172 | | using var doc = JsonDocument.Parse(_sampleContent.JsonData); |
| | 0 | 173 | | var definition = RenderDefinitionGeneratorService.Generate(doc.RootElement); |
| | 0 | 174 | | _definitionJson = JsonSerializer.Serialize(definition, WriteOptions); |
| | 0 | 175 | | _jsonError = null; |
| | 0 | 176 | | _definitionDirty = false; |
| | | 177 | | |
| | 0 | 178 | | _activeDefinition = definition; |
| | 0 | 179 | | Logger.LogInformation("Auto-generated definition with {Sections} sections, {Hidden} hidden fields", |
| | 0 | 180 | | definition.Sections.Count, definition.Hidden.Count); |
| | 0 | 181 | | } |
| | 0 | 182 | | catch (Exception ex) |
| | | 183 | | { |
| | 0 | 184 | | _jsonError = $"Generation failed: {ex.Message}"; |
| | 0 | 185 | | Logger.LogError(ex, "Auto-generation failed"); |
| | 0 | 186 | | } |
| | 0 | 187 | | } |
| | | 188 | | |
| | | 189 | | private void OnDefinitionJsonChanged(string newJson) |
| | | 190 | | { |
| | 0 | 191 | | _definitionJson = newJson; |
| | 0 | 192 | | _definitionDirty = true; |
| | 0 | 193 | | _jsonError = null; |
| | | 194 | | |
| | | 195 | | try |
| | | 196 | | { |
| | 0 | 197 | | JsonSerializer.Deserialize<RenderDefinition>(newJson, WriteOptions); |
| | 0 | 198 | | } |
| | 0 | 199 | | catch (JsonException ex) |
| | | 200 | | { |
| | 0 | 201 | | _jsonError = $"Invalid JSON: {ex.Message}"; |
| | 0 | 202 | | } |
| | 0 | 203 | | } |
| | | 204 | | |
| | | 205 | | private void ApplyDefinition() |
| | | 206 | | { |
| | 0 | 207 | | if (string.IsNullOrWhiteSpace(_definitionJson)) |
| | 0 | 208 | | return; |
| | | 209 | | |
| | | 210 | | try |
| | | 211 | | { |
| | 0 | 212 | | var definition = JsonSerializer.Deserialize<RenderDefinition>(_definitionJson, WriteOptions); |
| | 0 | 213 | | if (definition == null) |
| | | 214 | | { |
| | 0 | 215 | | _jsonError = "Deserialized to null."; |
| | 0 | 216 | | return; |
| | | 217 | | } |
| | | 218 | | |
| | 0 | 219 | | _activeDefinition = definition; |
| | 0 | 220 | | _definitionDirty = false; |
| | 0 | 221 | | _jsonError = null; |
| | 0 | 222 | | Logger.LogInformation("Applied edited definition: {Sections} sections", definition.Sections.Count); |
| | 0 | 223 | | } |
| | 0 | 224 | | catch (JsonException ex) |
| | | 225 | | { |
| | 0 | 226 | | _jsonError = $"Cannot apply — invalid JSON: {ex.Message}"; |
| | 0 | 227 | | } |
| | 0 | 228 | | } |
| | | 229 | | } |