| | | 1 | | @using Chronicis.Shared.DTOs |
| | | 2 | | @using Chronicis.Shared.Enums |
| | | 3 | | @inject ICampaignApiService CampaignApi |
| | | 4 | | @inject IArticleApiService ArticleApi |
| | | 5 | | @inject ITreeStateService TreeState |
| | | 6 | | @inject ISnackbar Snackbar |
| | | 7 | | @inject NavigationManager Navigation |
| | | 8 | | |
| | 0 | 9 | | @if (_showButton) |
| | | 10 | | { |
| | | 11 | | <div class="quick-add-session-container"> |
| | | 12 | | <MudButton Variant="Variant.Filled" |
| | | 13 | | Color="Color.Primary" |
| | | 14 | | FullWidth="true" |
| | | 15 | | OnClick="CreateSessionNote" |
| | | 16 | | Disabled="_isCreating" |
| | | 17 | | Class="quick-add-session-btn"> |
| | 0 | 18 | | @if (_isCreating) |
| | | 19 | | { |
| | | 20 | | <MudProgressCircular Size="Size.Small" Indeterminate="true" Class="mr-2" /> |
| | | 21 | | <span>Creating...</span> |
| | | 22 | | } |
| | | 23 | | else |
| | | 24 | | { |
| | | 25 | | <span>New Session Note</span> |
| | | 26 | | } |
| | | 27 | | </MudButton> |
| | 0 | 28 | | @if (!string.IsNullOrEmpty(_contextLabel)) |
| | | 29 | | { |
| | | 30 | | <MudText Typo="Typo.caption" Class="quick-add-context-label"> |
| | 0 | 31 | | @_contextLabel |
| | | 32 | | </MudText> |
| | | 33 | | } |
| | | 34 | | </div> |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | @code { |
| | | 38 | | private bool _showButton = false; |
| | | 39 | | private bool _isCreating = false; |
| | 0 | 40 | | private string _contextLabel = string.Empty; |
| | | 41 | | private ActiveContextDto? _activeContext; |
| | | 42 | | private Guid? _worldId; |
| | | 43 | | |
| | | 44 | | protected override void OnInitialized() |
| | | 45 | | { |
| | 0 | 46 | | TreeState.OnStateChanged += OnTreeStateChanged; |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | private async void OnTreeStateChanged() |
| | | 50 | | { |
| | 0 | 51 | | await CheckActiveContextAsync(); |
| | 0 | 52 | | await InvokeAsync(StateHasChanged); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | private async Task CheckActiveContextAsync() |
| | | 56 | | { |
| | 0 | 57 | | _showButton = false; |
| | 0 | 58 | | _contextLabel = string.Empty; |
| | 0 | 59 | | _activeContext = null; |
| | 0 | 60 | | _worldId = null; |
| | | 61 | | |
| | | 62 | | // Get all world nodes from tree state |
| | 0 | 63 | | var worldNodes = TreeState.RootNodes.Where(n => n.NodeType == TreeNodeType.World).ToList(); |
| | 0 | 64 | | if (!worldNodes.Any()) return; |
| | | 65 | | |
| | | 66 | | try |
| | | 67 | | { |
| | | 68 | | // Check each world for an active context |
| | 0 | 69 | | foreach (var worldNode in worldNodes) |
| | | 70 | | { |
| | 0 | 71 | | var context = await CampaignApi.GetActiveContextAsync(worldNode.Id); |
| | | 72 | | |
| | 0 | 73 | | if (context != null && context.HasActiveContext) |
| | | 74 | | { |
| | 0 | 75 | | _activeContext = context; |
| | 0 | 76 | | _worldId = worldNode.Id; |
| | 0 | 77 | | _showButton = true; |
| | | 78 | | |
| | | 79 | | // Build context label |
| | 0 | 80 | | var parts = new List<string>(); |
| | 0 | 81 | | if (!string.IsNullOrEmpty(_activeContext.CampaignName)) |
| | 0 | 82 | | parts.Add(_activeContext.CampaignName); |
| | 0 | 83 | | if (!string.IsNullOrEmpty(_activeContext.ArcName)) |
| | 0 | 84 | | parts.Add(_activeContext.ArcName); |
| | | 85 | | |
| | 0 | 86 | | _contextLabel = string.Join(" › ", parts); |
| | 0 | 87 | | break; // Found an active context, stop searching |
| | | 88 | | } |
| | 0 | 89 | | } |
| | 0 | 90 | | } |
| | 0 | 91 | | catch (Exception) |
| | | 92 | | { |
| | | 93 | | // Silently fail - button just won't show |
| | 0 | 94 | | } |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | private async Task CreateSessionNote() |
| | | 98 | | { |
| | 0 | 99 | | if (_activeContext?.ArcId == null || _activeContext?.CampaignId == null || _worldId == null || _isCreating) |
| | 0 | 100 | | return; |
| | | 101 | | |
| | 0 | 102 | | _isCreating = true; |
| | 0 | 103 | | StateHasChanged(); |
| | | 104 | | |
| | | 105 | | try |
| | | 106 | | { |
| | | 107 | | // Generate session name as YYYY-MM-DD |
| | 0 | 108 | | var sessionName = DateTime.Now.ToString("yyyy-MM-dd"); |
| | | 109 | | |
| | 0 | 110 | | var createDto = new ArticleCreateDto |
| | 0 | 111 | | { |
| | 0 | 112 | | Title = sessionName, |
| | 0 | 113 | | WorldId = _worldId, |
| | 0 | 114 | | CampaignId = _activeContext.CampaignId, |
| | 0 | 115 | | ArcId = _activeContext.ArcId, |
| | 0 | 116 | | Type = ArticleType.Session |
| | 0 | 117 | | }; |
| | | 118 | | |
| | 0 | 119 | | var newArticle = await ArticleApi.CreateArticleAsync(createDto); |
| | | 120 | | |
| | 0 | 121 | | if (newArticle != null) |
| | | 122 | | { |
| | | 123 | | // Refresh tree to show new session |
| | 0 | 124 | | await TreeState.RefreshAsync(); |
| | 0 | 125 | | TreeState.ExpandPathToAndSelect(newArticle.Id); |
| | | 126 | | |
| | | 127 | | // Fetch the full article to get breadcrumbs for navigation |
| | 0 | 128 | | var articleDetail = await ArticleApi.GetArticleDetailAsync(newArticle.Id); |
| | 0 | 129 | | if (articleDetail != null && articleDetail.Breadcrumbs.Any()) |
| | | 130 | | { |
| | 0 | 131 | | var path = string.Join("/", articleDetail.Breadcrumbs.Select(b => b.Slug)); |
| | 0 | 132 | | Navigation.NavigateTo($"/article/{path}"); |
| | | 133 | | } |
| | | 134 | | |
| | 0 | 135 | | Snackbar.Add($"Session '{sessionName}' created", Severity.Success); |
| | | 136 | | } |
| | | 137 | | else |
| | | 138 | | { |
| | 0 | 139 | | Snackbar.Add("Failed to create session", Severity.Error); |
| | | 140 | | } |
| | 0 | 141 | | } |
| | 0 | 142 | | catch (Exception ex) |
| | | 143 | | { |
| | 0 | 144 | | Snackbar.Add($"Error: {ex.Message}", Severity.Error); |
| | 0 | 145 | | } |
| | | 146 | | finally |
| | | 147 | | { |
| | 0 | 148 | | _isCreating = false; |
| | 0 | 149 | | StateHasChanged(); |
| | | 150 | | } |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | public void Dispose() |
| | | 154 | | { |
| | 0 | 155 | | TreeState.OnStateChanged -= OnTreeStateChanged; |
| | 0 | 156 | | } |
| | | 157 | | } |