| | | 1 | | using Chronicis.Client.Abstractions; |
| | | 2 | | using Chronicis.Client.Components.Dialogs; |
| | | 3 | | using Chronicis.Client.Services; |
| | | 4 | | using Chronicis.Shared.DTOs; |
| | | 5 | | using Chronicis.Shared.Extensions; |
| | | 6 | | using MudBlazor; |
| | | 7 | | |
| | | 8 | | namespace Chronicis.Client.ViewModels; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// ViewModel for the Dashboard page. |
| | | 12 | | /// Coordinates data loading, world ordering, and user actions. |
| | | 13 | | /// Implements <see cref="IDisposable"/> to clean up the tree state subscription. |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class DashboardViewModel : ViewModelBase, IDisposable |
| | | 16 | | { |
| | | 17 | | private readonly IDashboardApiService _dashboardApi; |
| | | 18 | | private readonly IUserApiService _userApi; |
| | | 19 | | private readonly IWorldApiService _worldApi; |
| | | 20 | | private readonly IArticleApiService _articleApi; |
| | | 21 | | private readonly IQuoteService _quoteService; |
| | | 22 | | private readonly ITreeStateService _treeState; |
| | | 23 | | private readonly IDialogService _dialogService; |
| | | 24 | | private readonly IAppNavigator _navigator; |
| | | 25 | | private readonly IUserNotifier _notifier; |
| | | 26 | | private readonly ILogger<DashboardViewModel> _logger; |
| | | 27 | | |
| | 29 | 28 | | private bool _isLoading = true; |
| | | 29 | | private DashboardDto? _dashboard; |
| | 29 | 30 | | private List<DashboardWorldDto> _orderedWorlds = new(); |
| | | 31 | | private Quote? _quote; |
| | | 32 | | |
| | 29 | 33 | | public DashboardViewModel( |
| | 29 | 34 | | IDashboardApiService dashboardApi, |
| | 29 | 35 | | IUserApiService userApi, |
| | 29 | 36 | | IWorldApiService worldApi, |
| | 29 | 37 | | IArticleApiService articleApi, |
| | 29 | 38 | | IQuoteService quoteService, |
| | 29 | 39 | | ITreeStateService treeState, |
| | 29 | 40 | | IDialogService dialogService, |
| | 29 | 41 | | IAppNavigator navigator, |
| | 29 | 42 | | IUserNotifier notifier, |
| | 29 | 43 | | ILogger<DashboardViewModel> logger) |
| | | 44 | | { |
| | 29 | 45 | | _dashboardApi = dashboardApi; |
| | 29 | 46 | | _userApi = userApi; |
| | 29 | 47 | | _worldApi = worldApi; |
| | 29 | 48 | | _articleApi = articleApi; |
| | 29 | 49 | | _quoteService = quoteService; |
| | 29 | 50 | | _treeState = treeState; |
| | 29 | 51 | | _dialogService = dialogService; |
| | 29 | 52 | | _navigator = navigator; |
| | 29 | 53 | | _notifier = notifier; |
| | 29 | 54 | | _logger = logger; |
| | | 55 | | |
| | 29 | 56 | | _treeState.OnStateChanged += OnTreeStateChanged; |
| | 29 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary>Whether the initial data load is in progress.</summary> |
| | | 60 | | public bool IsLoading |
| | | 61 | | { |
| | 8 | 62 | | get => _isLoading; |
| | 26 | 63 | | private set => SetField(ref _isLoading, value); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary>The loaded dashboard data, or <c>null</c> if loading failed.</summary> |
| | | 67 | | public DashboardDto? Dashboard |
| | | 68 | | { |
| | 28 | 69 | | get => _dashboard; |
| | 12 | 70 | | private set => SetField(ref _dashboard, value); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary>Worlds ordered by activity for display.</summary> |
| | | 74 | | public List<DashboardWorldDto> OrderedWorlds |
| | | 75 | | { |
| | 6 | 76 | | get => _orderedWorlds; |
| | 10 | 77 | | private set => SetField(ref _orderedWorlds, value); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary>The motivational quote shown in the hero section.</summary> |
| | | 81 | | public Quote? Quote |
| | | 82 | | { |
| | 8 | 83 | | get => _quote; |
| | 8 | 84 | | private set => SetField(ref _quote, value); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Runs onboarding redirect check, subscribes to tree state, and loads dashboard + quote in parallel. |
| | | 89 | | /// </summary> |
| | | 90 | | public async Task InitializeAsync() |
| | | 91 | | { |
| | | 92 | | var profile = await _userApi.GetUserProfileAsync(); |
| | | 93 | | if (profile != null && !profile.HasCompletedOnboarding) |
| | | 94 | | { |
| | | 95 | | _navigator.NavigateTo("/getting-started", replace: true); |
| | | 96 | | return; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | await Task.WhenAll(LoadDashboardAsync(), LoadQuoteAsync()); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary>Loads (or reloads) the dashboard data and updates <see cref="OrderedWorlds"/>.</summary> |
| | | 103 | | public async Task LoadDashboardAsync() |
| | | 104 | | { |
| | | 105 | | IsLoading = true; |
| | | 106 | | |
| | | 107 | | try |
| | | 108 | | { |
| | | 109 | | var data = await _dashboardApi.GetDashboardAsync(); |
| | | 110 | | Dashboard = data; |
| | | 111 | | |
| | | 112 | | if (data != null) |
| | | 113 | | { |
| | | 114 | | OrderedWorlds = data.Worlds |
| | | 115 | | .OrderByDescending(w => w.Campaigns.Any(c => c.IsActive)) |
| | | 116 | | .ThenByDescending(w => w.Campaigns |
| | | 117 | | .Where(c => c.CurrentArc?.LatestSessionDate != null) |
| | | 118 | | .Select(c => c.CurrentArc!.LatestSessionDate) |
| | | 119 | | .DefaultIfEmpty(DateTime.MinValue) |
| | | 120 | | .Max()) |
| | | 121 | | .ThenByDescending(w => w.CreatedAt) |
| | | 122 | | .ToList(); |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | catch (Exception ex) |
| | | 126 | | { |
| | | 127 | | _logger.LogErrorSanitized(ex, "Error loading dashboard"); |
| | | 128 | | _notifier.Error("Failed to load dashboard"); |
| | | 129 | | } |
| | | 130 | | finally |
| | | 131 | | { |
| | | 132 | | IsLoading = false; |
| | | 133 | | } |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary>Loads the motivational quote. Failures are swallowed silently.</summary> |
| | | 137 | | public async Task LoadQuoteAsync() |
| | | 138 | | { |
| | | 139 | | try |
| | | 140 | | { |
| | | 141 | | Quote = await _quoteService.GetRandomQuoteAsync(); |
| | | 142 | | } |
| | | 143 | | catch (Exception ex) |
| | | 144 | | { |
| | | 145 | | _logger.LogErrorSanitized(ex, "Error loading quote"); |
| | | 146 | | } |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary>Creates a new world and navigates to it.</summary> |
| | | 150 | | public async Task CreateNewWorldAsync() |
| | | 151 | | { |
| | | 152 | | try |
| | | 153 | | { |
| | | 154 | | var createDto = new WorldCreateDto |
| | | 155 | | { |
| | | 156 | | Name = "New World", |
| | | 157 | | Description = "A new world for your adventures" |
| | | 158 | | }; |
| | | 159 | | |
| | | 160 | | var world = await _worldApi.CreateWorldAsync(createDto); |
| | | 161 | | |
| | | 162 | | if (world != null) |
| | | 163 | | { |
| | | 164 | | _notifier.Success($"World '{world.Name}' created!"); |
| | | 165 | | await _treeState.RefreshAsync(); |
| | | 166 | | |
| | | 167 | | if (world.WorldRootArticleId.HasValue) |
| | | 168 | | { |
| | | 169 | | _treeState.ShouldFocusTitle = true; |
| | | 170 | | _navigator.NavigateTo($"/world/{world.Slug}"); |
| | | 171 | | } |
| | | 172 | | else |
| | | 173 | | { |
| | | 174 | | await LoadDashboardAsync(); |
| | | 175 | | } |
| | | 176 | | } |
| | | 177 | | else |
| | | 178 | | { |
| | | 179 | | _notifier.Error("Failed to create world"); |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | catch (Exception ex) |
| | | 183 | | { |
| | | 184 | | _logger.LogErrorSanitized(ex, "Error creating world"); |
| | | 185 | | _notifier.Error($"Error: {ex.Message}"); |
| | | 186 | | } |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary>Opens the join-world dialog and handles the result.</summary> |
| | | 190 | | public async Task JoinWorldAsync() |
| | | 191 | | { |
| | | 192 | | var dialog = await _dialogService.ShowAsync<JoinWorldDialog>("Join a World"); |
| | | 193 | | var result = await dialog.Result; |
| | | 194 | | |
| | | 195 | | if (result != null && !result.Canceled && result.Data is WorldJoinResultDto joinResult) |
| | | 196 | | { |
| | | 197 | | _notifier.Success($"Welcome to {joinResult.WorldName}!"); |
| | | 198 | | await _treeState.RefreshAsync(); |
| | | 199 | | await LoadDashboardAsync(); |
| | | 200 | | |
| | | 201 | | if (joinResult.WorldId.HasValue) |
| | | 202 | | { |
| | | 203 | | _navigator.NavigateTo($"/world/{joinResult.WorldId}"); |
| | | 204 | | } |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | /// <summary>Resolves a character's article path and navigates to it.</summary> |
| | | 209 | | public async Task NavigateToCharacterAsync(Guid characterId) |
| | | 210 | | { |
| | | 211 | | var article = await _articleApi.GetArticleDetailAsync(characterId); |
| | | 212 | | if (article != null && article.Breadcrumbs.Any()) |
| | | 213 | | { |
| | | 214 | | var path = string.Join("/", article.Breadcrumbs.Select(b => b.Slug)); |
| | | 215 | | _navigator.NavigateTo($"/article/{path}"); |
| | | 216 | | } |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | /// <summary>Handles a dashboard prompt click, navigating if the prompt has an action URL.</summary> |
| | | 220 | | public void HandlePromptClick(PromptDto prompt) |
| | | 221 | | { |
| | 3 | 222 | | if (!string.IsNullOrEmpty(prompt.ActionUrl)) |
| | | 223 | | { |
| | 1 | 224 | | _navigator.NavigateTo(prompt.ActionUrl); |
| | | 225 | | } |
| | 3 | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary>Returns the CSS class for a prompt category.</summary> |
| | | 229 | | public static string GetCategoryClass(PromptCategory category) => |
| | 6 | 230 | | category switch |
| | 6 | 231 | | { |
| | 2 | 232 | | PromptCategory.MissingFundamental => "missing-fundamental", |
| | 1 | 233 | | PromptCategory.NeedsAttention => "needs-attention", |
| | 2 | 234 | | PromptCategory.Suggestion => "suggestion", |
| | 1 | 235 | | _ => string.Empty |
| | 6 | 236 | | }; |
| | | 237 | | |
| | 1 | 238 | | private void OnTreeStateChanged() => RaisePropertyChanged(nameof(OrderedWorlds)); |
| | | 239 | | |
| | | 240 | | /// <inheritdoc /> |
| | | 241 | | public void Dispose() |
| | | 242 | | { |
| | 6 | 243 | | _treeState.OnStateChanged -= OnTreeStateChanged; |
| | 6 | 244 | | } |
| | | 245 | | } |