| | | 1 | | using Chronicis.Client.Services; |
| | | 2 | | using Chronicis.Shared.DTOs.Quests; |
| | | 3 | | using Chronicis.Shared.Enums; |
| | | 4 | | using Microsoft.AspNetCore.Components; |
| | | 5 | | using Microsoft.AspNetCore.Components.Web; |
| | | 6 | | using Microsoft.JSInterop; |
| | | 7 | | using MudBlazor; |
| | | 8 | | |
| | | 9 | | namespace Chronicis.Client.Components.Quests; |
| | | 10 | | |
| | | 11 | | public partial class ArcQuestEditor : ComponentBase, IAsyncDisposable |
| | | 12 | | { |
| | | 13 | | [Parameter] |
| | 0 | 14 | | public QuestDto? Quest { get; set; } |
| | | 15 | | |
| | | 16 | | [Parameter] |
| | 0 | 17 | | public EventCallback<QuestDto> OnQuestUpdated { get; set; } |
| | | 18 | | |
| | 0 | 19 | | [Inject] private IQuestApiService QuestApi { get; set; } = default!; |
| | 0 | 20 | | [Inject] private IJSRuntime JSRuntime { get; set; } = default!; |
| | 0 | 21 | | [Inject] private ISnackbar Snackbar { get; set; } = default!; |
| | | 22 | | |
| | | 23 | | private QuestDto? _quest; |
| | 0 | 24 | | private string _editTitle = string.Empty; |
| | 0 | 25 | | private string _editDescription = string.Empty; |
| | | 26 | | private QuestStatus _editStatus; |
| | | 27 | | private bool _editIsGmOnly; |
| | | 28 | | private int _editSortOrder; |
| | | 29 | | |
| | | 30 | | private bool _isSaving; |
| | | 31 | | private bool _hasUnsavedChanges; |
| | | 32 | | private bool _editorInitialized; |
| | | 33 | | private bool _disposed; |
| | | 34 | | private DotNetObjectReference<ArcQuestEditor>? _dotNetHelper; |
| | | 35 | | private Timer? _autoSaveTimer; |
| | | 36 | | |
| | 0 | 37 | | private string EditorId => $"quest-desc-editor-{_quest?.Id ?? Guid.Empty}"; |
| | | 38 | | |
| | | 39 | | protected override async Task OnParametersSetAsync() |
| | | 40 | | { |
| | | 41 | | // Quest changed - dispose old editor and initialize new one |
| | 0 | 42 | | if (_quest?.Id != Quest?.Id) |
| | | 43 | | { |
| | 0 | 44 | | await DisposeEditorAsync(); |
| | | 45 | | |
| | 0 | 46 | | _quest = Quest; |
| | | 47 | | |
| | 0 | 48 | | if (_quest != null) |
| | | 49 | | { |
| | 0 | 50 | | _editTitle = _quest.Title; |
| | 0 | 51 | | _editDescription = _quest.Description ?? string.Empty; |
| | 0 | 52 | | _editStatus = _quest.Status; |
| | 0 | 53 | | _editIsGmOnly = _quest.IsGmOnly; |
| | 0 | 54 | | _editSortOrder = _quest.SortOrder; |
| | 0 | 55 | | _hasUnsavedChanges = false; |
| | | 56 | | } |
| | | 57 | | } |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | protected override async Task OnAfterRenderAsync(bool firstRender) |
| | | 61 | | { |
| | 0 | 62 | | if (firstRender) |
| | | 63 | | { |
| | 0 | 64 | | _dotNetHelper = DotNetObjectReference.Create(this); |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | if (_quest != null && !_editorInitialized && !_disposed && _dotNetHelper != null) |
| | | 68 | | { |
| | 0 | 69 | | await Task.Delay(100); // Small delay to ensure DOM is ready |
| | 0 | 70 | | await InitializeEditorAsync(); |
| | | 71 | | } |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | private async Task InitializeEditorAsync() |
| | | 75 | | { |
| | 0 | 76 | | if (_editorInitialized || _disposed || _dotNetHelper == null) |
| | 0 | 77 | | return; |
| | | 78 | | |
| | | 79 | | try |
| | | 80 | | { |
| | 0 | 81 | | await JSRuntime.InvokeVoidAsync("initializeTipTapEditor", EditorId, _editDescription, _dotNetHelper); |
| | | 82 | | |
| | 0 | 83 | | if (_disposed) |
| | 0 | 84 | | return; |
| | | 85 | | |
| | 0 | 86 | | await JSRuntime.InvokeVoidAsync("initializeWikiLinkAutocomplete", EditorId, _dotNetHelper); |
| | 0 | 87 | | _editorInitialized = true; |
| | 0 | 88 | | } |
| | 0 | 89 | | catch (ObjectDisposedException) |
| | | 90 | | { |
| | | 91 | | // Expected during navigation |
| | 0 | 92 | | } |
| | 0 | 93 | | catch (JSDisconnectedException) |
| | | 94 | | { |
| | | 95 | | // Expected during navigation |
| | 0 | 96 | | } |
| | 0 | 97 | | catch (Exception ex) |
| | | 98 | | { |
| | 0 | 99 | | if (!_disposed) |
| | | 100 | | { |
| | 0 | 101 | | Snackbar.Add($"Failed to initialize quest editor: {ex.Message}", Severity.Warning); |
| | | 102 | | } |
| | 0 | 103 | | } |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | private async Task DisposeEditorAsync() |
| | | 107 | | { |
| | 0 | 108 | | if (_editorInitialized && !_disposed) |
| | | 109 | | { |
| | | 110 | | try |
| | | 111 | | { |
| | 0 | 112 | | await JSRuntime.InvokeVoidAsync("destroyTipTapEditor", EditorId); |
| | 0 | 113 | | } |
| | 0 | 114 | | catch |
| | | 115 | | { |
| | | 116 | | // Ignore errors during disposal |
| | 0 | 117 | | } |
| | | 118 | | finally |
| | | 119 | | { |
| | 0 | 120 | | _editorInitialized = false; |
| | | 121 | | } |
| | | 122 | | } |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | [JSInvokable] |
| | | 126 | | public void OnEditorUpdate(string html) |
| | | 127 | | { |
| | 0 | 128 | | _editDescription = html; |
| | 0 | 129 | | _hasUnsavedChanges = true; |
| | | 130 | | |
| | | 131 | | // Debounce auto-save (0.5s delay) |
| | 0 | 132 | | _autoSaveTimer?.Dispose(); |
| | 0 | 133 | | _autoSaveTimer = new Timer(async _ => await AutoSaveAsync(), null, 500, Timeout.Infinite); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | private async Task AutoSaveAsync() |
| | | 137 | | { |
| | 0 | 138 | | await InvokeAsync(async () => |
| | 0 | 139 | | { |
| | 0 | 140 | | if (_hasUnsavedChanges && !_isSaving) |
| | 0 | 141 | | { |
| | 0 | 142 | | await SaveQuestAsync(); |
| | 0 | 143 | | } |
| | 0 | 144 | | }); |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | private async Task OnTitleKeyDown(KeyboardEventArgs e) |
| | | 148 | | { |
| | 0 | 149 | | if (e.Key == "Enter") |
| | | 150 | | { |
| | 0 | 151 | | await SaveTitleAsync(); |
| | | 152 | | } |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | private async Task SaveTitleAsync() |
| | | 156 | | { |
| | 0 | 157 | | if (_quest != null && _editTitle != _quest.Title) |
| | | 158 | | { |
| | 0 | 159 | | _hasUnsavedChanges = true; |
| | 0 | 160 | | await SaveQuestAsync(); |
| | | 161 | | } |
| | 0 | 162 | | } |
| | | 163 | | |
| | | 164 | | private void OnStatusChanged(QuestStatus newStatus) |
| | | 165 | | { |
| | 0 | 166 | | _editStatus = newStatus; |
| | 0 | 167 | | _ = SaveQuestAsync(); |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | private void OnGmOnlyChanged(bool newValue) |
| | | 171 | | { |
| | 0 | 172 | | _editIsGmOnly = newValue; |
| | 0 | 173 | | _ = SaveQuestAsync(); |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | private void OnSortOrderChanged(int newValue) |
| | | 177 | | { |
| | 0 | 178 | | _editSortOrder = newValue; |
| | 0 | 179 | | _ = SaveQuestAsync(); |
| | 0 | 180 | | } |
| | | 181 | | |
| | | 182 | | private async Task SaveQuestAsync() |
| | | 183 | | { |
| | 0 | 184 | | if (_quest == null || _isSaving) |
| | 0 | 185 | | return; |
| | | 186 | | |
| | 0 | 187 | | _isSaving = true; |
| | 0 | 188 | | StateHasChanged(); |
| | | 189 | | |
| | | 190 | | try |
| | | 191 | | { |
| | 0 | 192 | | var editDto = new QuestEditDto |
| | 0 | 193 | | { |
| | 0 | 194 | | Title = string.IsNullOrWhiteSpace(_editTitle) ? null : _editTitle.Trim(), |
| | 0 | 195 | | Description = string.IsNullOrWhiteSpace(_editDescription) ? null : _editDescription, |
| | 0 | 196 | | Status = _editStatus, |
| | 0 | 197 | | IsGmOnly = _editIsGmOnly, |
| | 0 | 198 | | SortOrder = _editSortOrder, |
| | 0 | 199 | | RowVersion = _quest.RowVersion |
| | 0 | 200 | | }; |
| | | 201 | | |
| | 0 | 202 | | var updated = await QuestApi.UpdateQuestAsync(_quest.Id, editDto); |
| | | 203 | | |
| | 0 | 204 | | if (updated != null) |
| | | 205 | | { |
| | | 206 | | // Success - update local state with server response |
| | 0 | 207 | | _quest = updated; |
| | 0 | 208 | | _editTitle = updated.Title; |
| | 0 | 209 | | _editDescription = updated.Description ?? string.Empty; |
| | 0 | 210 | | _editStatus = updated.Status; |
| | 0 | 211 | | _editIsGmOnly = updated.IsGmOnly; |
| | 0 | 212 | | _editSortOrder = updated.SortOrder; |
| | 0 | 213 | | _hasUnsavedChanges = false; |
| | | 214 | | |
| | 0 | 215 | | await OnQuestUpdated.InvokeAsync(updated); |
| | | 216 | | } |
| | | 217 | | else |
| | | 218 | | { |
| | | 219 | | // 409 conflict was already handled by QuestApiService |
| | | 220 | | // Reload from server |
| | 0 | 221 | | var current = await QuestApi.GetQuestAsync(_quest.Id); |
| | 0 | 222 | | if (current != null) |
| | | 223 | | { |
| | 0 | 224 | | _quest = current; |
| | 0 | 225 | | _editTitle = current.Title; |
| | 0 | 226 | | _editDescription = current.Description ?? string.Empty; |
| | 0 | 227 | | _editStatus = current.Status; |
| | 0 | 228 | | _editIsGmOnly = current.IsGmOnly; |
| | 0 | 229 | | _editSortOrder = current.SortOrder; |
| | | 230 | | |
| | | 231 | | // Reinitialize editor with server content |
| | 0 | 232 | | await DisposeEditorAsync(); |
| | 0 | 233 | | await InitializeEditorAsync(); |
| | | 234 | | } |
| | | 235 | | } |
| | 0 | 236 | | } |
| | 0 | 237 | | catch (Exception ex) |
| | | 238 | | { |
| | 0 | 239 | | Snackbar.Add($"Failed to save quest: {ex.Message}", Severity.Error); |
| | 0 | 240 | | } |
| | | 241 | | finally |
| | | 242 | | { |
| | 0 | 243 | | _isSaving = false; |
| | 0 | 244 | | StateHasChanged(); |
| | | 245 | | } |
| | 0 | 246 | | } |
| | | 247 | | |
| | | 248 | | public async ValueTask DisposeAsync() |
| | | 249 | | { |
| | 0 | 250 | | if (_disposed) |
| | 0 | 251 | | return; |
| | | 252 | | |
| | 0 | 253 | | _disposed = true; |
| | 0 | 254 | | _autoSaveTimer?.Dispose(); |
| | | 255 | | |
| | | 256 | | // Properly dispose TipTap editor |
| | 0 | 257 | | await DisposeEditorAsync(); |
| | | 258 | | |
| | 0 | 259 | | _dotNetHelper?.Dispose(); |
| | | 260 | | |
| | 0 | 261 | | GC.SuppressFinalize(this); |
| | 0 | 262 | | } |
| | | 263 | | } |