| | | 1 | | using Blazored.LocalStorage; |
| | | 2 | | using Chronicis.Client.Models; |
| | | 3 | | |
| | | 4 | | namespace Chronicis.Client.Services.Tree; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Manages UI-related state for the tree: expansion, selection, search filtering, and persistence. |
| | | 8 | | /// This component does not perform any API calls - it operates purely on the in-memory node index. |
| | | 9 | | /// </summary> |
| | | 10 | | internal sealed class TreeUiState |
| | | 11 | | { |
| | | 12 | | private readonly ILocalStorageService _localStorage; |
| | | 13 | | private readonly ILogger _logger; |
| | | 14 | | |
| | | 15 | | private const string ExpandedNodesStorageKey = "chronicis_expanded_nodes"; |
| | | 16 | | |
| | | 17 | | // Shared node index (owned by TreeStateService, passed in) |
| | 56 | 18 | | private TreeNodeIndex _nodeIndex = new(); |
| | | 19 | | |
| | | 20 | | // UI State |
| | 56 | 21 | | private readonly HashSet<Guid> _expandedNodeIds = new(); |
| | | 22 | | private Guid? _selectedNodeId; |
| | | 23 | | private Guid? _pendingSelectionId; |
| | 56 | 24 | | private string _searchQuery = string.Empty; |
| | | 25 | | |
| | | 26 | | public TreeUiState(ILocalStorageService localStorage, ILogger logger) |
| | | 27 | | { |
| | 56 | 28 | | _localStorage = localStorage; |
| | 56 | 29 | | _logger = logger; |
| | 56 | 30 | | } |
| | | 31 | | |
| | | 32 | | // ============================================ |
| | | 33 | | // State Properties |
| | | 34 | | // ============================================ |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the currently selected node ID. |
| | | 38 | | /// </summary> |
| | 20 | 39 | | public Guid? SelectedNodeId => _selectedNodeId; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets the current search query. |
| | | 43 | | /// </summary> |
| | 5 | 44 | | public string SearchQuery => _searchQuery; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets whether a search filter is currently active. |
| | | 48 | | /// </summary> |
| | 26 | 49 | | public bool IsSearchActive => !string.IsNullOrWhiteSpace(_searchQuery); |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the pending selection ID (for selection before tree is initialized). |
| | | 53 | | /// </summary> |
| | 3 | 54 | | public Guid? PendingSelectionId => _pendingSelectionId; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the set of expanded node IDs. |
| | | 58 | | /// </summary> |
| | 15 | 59 | | public IReadOnlySet<Guid> ExpandedNodeIds => _expandedNodeIds; |
| | | 60 | | |
| | | 61 | | // ============================================ |
| | | 62 | | // Initialization |
| | | 63 | | // ============================================ |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Sets the node index reference. Called after tree is built. |
| | | 67 | | /// </summary> |
| | | 68 | | public void SetNodeIndex(TreeNodeIndex nodeIndex) |
| | | 69 | | { |
| | 57 | 70 | | _nodeIndex = nodeIndex; |
| | 57 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Resets UI state (called before tree rebuild). |
| | | 75 | | /// </summary> |
| | | 76 | | public void Reset() |
| | | 77 | | { |
| | 1 | 78 | | _expandedNodeIds.Clear(); |
| | 1 | 79 | | _selectedNodeId = null; |
| | 1 | 80 | | _searchQuery = string.Empty; |
| | 1 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Clears the pending selection. |
| | | 85 | | /// </summary> |
| | | 86 | | public void ClearPendingSelection() |
| | | 87 | | { |
| | 1 | 88 | | _pendingSelectionId = null; |
| | 1 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Checks if there's a pending selection and returns it, clearing the pending state. |
| | | 93 | | /// </summary> |
| | | 94 | | public Guid? ConsumePendingSelection() |
| | | 95 | | { |
| | 15 | 96 | | var pending = _pendingSelectionId; |
| | 15 | 97 | | _pendingSelectionId = null; |
| | 15 | 98 | | return pending; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | // ============================================ |
| | | 102 | | // Node Operations |
| | | 103 | | // ============================================ |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Expands a node to show its children. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <returns>True if the node was found and expanded.</returns> |
| | | 109 | | public bool ExpandNode(Guid nodeId) |
| | | 110 | | { |
| | 15 | 111 | | if (_nodeIndex.TryGetNode(nodeId, out var node) && node != null) |
| | | 112 | | { |
| | 14 | 113 | | node.IsExpanded = true; |
| | 14 | 114 | | _expandedNodeIds.Add(nodeId); |
| | 14 | 115 | | _ = SaveExpandedStateAsync(); |
| | 14 | 116 | | return true; |
| | | 117 | | } |
| | 1 | 118 | | return false; |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Collapses a node to hide its children. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <returns>True if the node was found and collapsed.</returns> |
| | | 125 | | public bool CollapseNode(Guid nodeId) |
| | | 126 | | { |
| | 5 | 127 | | if (_nodeIndex.TryGetNode(nodeId, out var node) && node != null) |
| | | 128 | | { |
| | 4 | 129 | | node.IsExpanded = false; |
| | 4 | 130 | | _expandedNodeIds.Remove(nodeId); |
| | 4 | 131 | | _ = SaveExpandedStateAsync(); |
| | 4 | 132 | | return true; |
| | | 133 | | } |
| | 1 | 134 | | return false; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Toggles a node's expanded state. |
| | | 139 | | /// </summary> |
| | | 140 | | /// <returns>True if the node was found and toggled.</returns> |
| | | 141 | | public bool ToggleNode(Guid nodeId) |
| | | 142 | | { |
| | 6 | 143 | | if (_nodeIndex.TryGetNode(nodeId, out var node) && node != null) |
| | | 144 | | { |
| | 5 | 145 | | if (node.IsExpanded) |
| | 1 | 146 | | return CollapseNode(nodeId); |
| | | 147 | | else |
| | 4 | 148 | | return ExpandNode(nodeId); |
| | | 149 | | } |
| | 1 | 150 | | return false; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Selects a node. For virtual groups, toggles expansion instead. |
| | | 155 | | /// </summary> |
| | | 156 | | /// <returns>True if selection changed or node was toggled.</returns> |
| | | 157 | | public bool SelectNode(Guid nodeId) |
| | | 158 | | { |
| | | 159 | | // Deselect previous |
| | 24 | 160 | | if (_selectedNodeId.HasValue && _nodeIndex.TryGetNode(_selectedNodeId.Value, out var previousNode) && previousNo |
| | | 161 | | { |
| | 5 | 162 | | previousNode.IsSelected = false; |
| | | 163 | | } |
| | | 164 | | |
| | 24 | 165 | | if (_nodeIndex.TryGetNode(nodeId, out var node) && node != null) |
| | | 166 | | { |
| | 20 | 167 | | var isMapsVirtualGroup = node.NodeType == TreeNodeType.VirtualGroup && |
| | 20 | 168 | | node.VirtualGroupType == VirtualGroupType.Maps; |
| | | 169 | | |
| | | 170 | | // Selectable node types: Article, World, Campaign, Arc, Session, Map, Maps virtual group |
| | 20 | 171 | | if (node.NodeType == TreeNodeType.Article || |
| | 20 | 172 | | node.NodeType == TreeNodeType.World || |
| | 20 | 173 | | node.NodeType == TreeNodeType.Campaign || |
| | 20 | 174 | | node.NodeType == TreeNodeType.Arc || |
| | 20 | 175 | | node.NodeType == TreeNodeType.Session || |
| | 20 | 176 | | node.NodeType == TreeNodeType.Map || |
| | 20 | 177 | | isMapsVirtualGroup) |
| | | 178 | | { |
| | 19 | 179 | | node.IsSelected = true; |
| | 19 | 180 | | _selectedNodeId = nodeId; |
| | | 181 | | |
| | | 182 | | // Auto-expand if node has children |
| | 19 | 183 | | if (node.HasChildren && !node.IsExpanded) |
| | | 184 | | { |
| | 3 | 185 | | node.IsExpanded = true; |
| | 3 | 186 | | _expandedNodeIds.Add(nodeId); |
| | 3 | 187 | | _ = SaveExpandedStateAsync(); |
| | | 188 | | } |
| | 19 | 189 | | return true; |
| | | 190 | | } |
| | | 191 | | else |
| | | 192 | | { |
| | | 193 | | // For virtual groups, just toggle expand |
| | 1 | 194 | | ToggleNode(nodeId); |
| | 1 | 195 | | _selectedNodeId = null; |
| | 1 | 196 | | return true; |
| | | 197 | | } |
| | | 198 | | } |
| | | 199 | | else |
| | | 200 | | { |
| | 4 | 201 | | _selectedNodeId = null; |
| | 4 | 202 | | return false; |
| | | 203 | | } |
| | | 204 | | } |
| | | 205 | | |
| | | 206 | | /// <summary> |
| | | 207 | | /// Expands the path to a node, collapses nodes not in the path, and selects the target. |
| | | 208 | | /// If tree is not initialized, stores as pending selection. |
| | | 209 | | /// </summary> |
| | | 210 | | /// <param name="nodeId">The node to navigate to.</param> |
| | | 211 | | /// <param name="isInitialized">Whether the tree is initialized.</param> |
| | | 212 | | /// <returns>True if the operation was performed, false if deferred as pending.</returns> |
| | | 213 | | public bool ExpandPathToAndSelect(Guid nodeId, bool isInitialized) |
| | | 214 | | { |
| | 13 | 215 | | if (!isInitialized) |
| | | 216 | | { |
| | 4 | 217 | | _pendingSelectionId = nodeId; |
| | 4 | 218 | | _selectedNodeId = nodeId; |
| | 4 | 219 | | return false; |
| | | 220 | | } |
| | | 221 | | |
| | 9 | 222 | | if (!_nodeIndex.TryGetNode(nodeId, out var targetNode) || targetNode == null) |
| | | 223 | | { |
| | | 224 | | // Some content (e.g., tutorial/system articles) is intentionally excluded |
| | | 225 | | // from the navigation tree but still needs to be loadable in the editor. |
| | 3 | 226 | | if (_selectedNodeId.HasValue && |
| | 3 | 227 | | _nodeIndex.TryGetNode(_selectedNodeId.Value, out var previousNode) && |
| | 3 | 228 | | previousNode != null) |
| | | 229 | | { |
| | 1 | 230 | | previousNode.IsSelected = false; |
| | | 231 | | } |
| | | 232 | | |
| | 3 | 233 | | _selectedNodeId = nodeId; |
| | 3 | 234 | | _logger.LogInformation("ExpandPathToAndSelect: Node {NodeId} not found in tree; selected directly", nodeId); |
| | 3 | 235 | | return false; |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | // Build path from root to target |
| | 6 | 239 | | var path = BuildPathToNode(targetNode); |
| | 6 | 240 | | var pathNodeIds = new HashSet<Guid>(path.Select(n => n.Id)); |
| | | 241 | | |
| | | 242 | | // Collapse all nodes that are NOT in the path to the target |
| | 72 | 243 | | foreach (var node in _nodeIndex.AllNodes) |
| | | 244 | | { |
| | 30 | 245 | | if (node.IsExpanded && !pathNodeIds.Contains(node.Id)) |
| | | 246 | | { |
| | 1 | 247 | | node.IsExpanded = false; |
| | 1 | 248 | | _expandedNodeIds.Remove(node.Id); |
| | | 249 | | } |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | // Expand all ancestors in the path (except the target itself) |
| | 20 | 253 | | for (int i = 0; i < path.Count - 1; i++) |
| | | 254 | | { |
| | 4 | 255 | | var node = path[i]; |
| | 4 | 256 | | node.IsExpanded = true; |
| | 4 | 257 | | _expandedNodeIds.Add(node.Id); |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | // Select the target |
| | 6 | 261 | | SelectNode(nodeId); |
| | | 262 | | |
| | 6 | 263 | | _ = SaveExpandedStateAsync(); |
| | 6 | 264 | | return true; |
| | | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Builds the path from root to the given node. |
| | | 269 | | /// </summary> |
| | | 270 | | private List<TreeNode> BuildPathToNode(TreeNode targetNode) |
| | | 271 | | { |
| | 6 | 272 | | var path = new List<TreeNode>(); |
| | 6 | 273 | | var current = targetNode; |
| | 6 | 274 | | var visitedNodes = new HashSet<TreeNode>(); |
| | | 275 | | |
| | 16 | 276 | | while (current != null) |
| | | 277 | | { |
| | 11 | 278 | | if (!visitedNodes.Add(current)) |
| | | 279 | | { |
| | 1 | 280 | | _logger.LogWarning( |
| | 1 | 281 | | "Detected cycle while building tree path for node {NodeId}. This can happen when legacy Session and |
| | 1 | 282 | | targetNode.Id); |
| | 1 | 283 | | break; |
| | | 284 | | } |
| | | 285 | | |
| | 10 | 286 | | path.Insert(0, current); |
| | | 287 | | |
| | 10 | 288 | | TreeNode? next = null; |
| | | 289 | | |
| | 10 | 290 | | if (current.ParentId.HasValue && |
| | 10 | 291 | | _nodeIndex.TryGetNode(current.ParentId.Value, out var parent) && |
| | 10 | 292 | | parent != null && |
| | 10 | 293 | | !ReferenceEquals(parent, current)) |
| | | 294 | | { |
| | 2 | 295 | | next = parent; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | // Duplicate IDs (legacy Session article + Session entity) can cause the index lookup |
| | | 299 | | // to return the current node instead of its structural parent. Fall back to tree traversal. |
| | 10 | 300 | | next ??= _nodeIndex.FindParentNode(current); |
| | | 301 | | |
| | 10 | 302 | | current = next; |
| | | 303 | | } |
| | | 304 | | |
| | 6 | 305 | | return path; |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | /// <summary> |
| | | 309 | | /// Clears the current selection. |
| | | 310 | | /// </summary> |
| | | 311 | | public void ClearSelection() |
| | | 312 | | { |
| | 3 | 313 | | if (_selectedNodeId.HasValue && _nodeIndex.TryGetNode(_selectedNodeId.Value, out var node) && node != null) |
| | | 314 | | { |
| | 2 | 315 | | node.IsSelected = false; |
| | | 316 | | } |
| | 3 | 317 | | _selectedNodeId = null; |
| | 3 | 318 | | } |
| | | 319 | | |
| | | 320 | | // ============================================ |
| | | 321 | | // Search/Filter |
| | | 322 | | // ============================================ |
| | | 323 | | |
| | | 324 | | /// <summary> |
| | | 325 | | /// Sets the search query and filters the tree. |
| | | 326 | | /// </summary> |
| | | 327 | | public void SetSearchQuery(string query) |
| | | 328 | | { |
| | 13 | 329 | | _searchQuery = query?.Trim() ?? string.Empty; |
| | 13 | 330 | | ApplySearchFilter(); |
| | 13 | 331 | | } |
| | | 332 | | |
| | | 333 | | /// <summary> |
| | | 334 | | /// Clears the search filter and makes all nodes visible. |
| | | 335 | | /// </summary> |
| | | 336 | | public void ClearSearch() |
| | | 337 | | { |
| | 4 | 338 | | _searchQuery = string.Empty; |
| | | 339 | | |
| | 32 | 340 | | foreach (var node in _nodeIndex.AllNodes) |
| | | 341 | | { |
| | 12 | 342 | | node.IsVisible = true; |
| | | 343 | | } |
| | 4 | 344 | | } |
| | | 345 | | |
| | | 346 | | /// <summary> |
| | | 347 | | /// Applies the current search filter to the tree. |
| | | 348 | | /// </summary> |
| | | 349 | | public void ApplySearchFilter() |
| | | 350 | | { |
| | 14 | 351 | | if (!IsSearchActive) |
| | | 352 | | { |
| | 2 | 353 | | ClearSearch(); |
| | 2 | 354 | | return; |
| | | 355 | | } |
| | | 356 | | |
| | 12 | 357 | | var searchLower = _searchQuery.ToLowerInvariant(); |
| | 12 | 358 | | var matchingNodeIds = new HashSet<Guid>(); |
| | | 359 | | |
| | | 360 | | // Find all matching nodes (articles + sessions) |
| | 100 | 361 | | foreach (var node in _nodeIndex.AllNodes) |
| | | 362 | | { |
| | 38 | 363 | | if ((node.NodeType == TreeNodeType.Article || node.NodeType == TreeNodeType.Session) && |
| | 38 | 364 | | node.Title.Contains(searchLower, StringComparison.OrdinalIgnoreCase)) |
| | | 365 | | { |
| | 11 | 366 | | AddNodeAndAncestors(node, matchingNodeIds); |
| | | 367 | | } |
| | | 368 | | } |
| | | 369 | | |
| | | 370 | | // Set visibility |
| | 100 | 371 | | foreach (var node in _nodeIndex.AllNodes) |
| | | 372 | | { |
| | 38 | 373 | | node.IsVisible = matchingNodeIds.Contains(node.Id); |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | // Expand nodes that have visible children |
| | 100 | 377 | | foreach (var node in _nodeIndex.AllNodes) |
| | | 378 | | { |
| | 38 | 379 | | if (!matchingNodeIds.Contains(node.Id)) |
| | | 380 | | { |
| | | 381 | | continue; |
| | | 382 | | } |
| | | 383 | | |
| | 18 | 384 | | if (node.Children.Any(c => c.IsVisible)) |
| | | 385 | | { |
| | 7 | 386 | | node.IsExpanded = true; |
| | 7 | 387 | | _expandedNodeIds.Add(node.Id); |
| | | 388 | | } |
| | | 389 | | } |
| | 12 | 390 | | } |
| | | 391 | | |
| | | 392 | | /// <summary> |
| | | 393 | | /// Recursively adds a node and all its ancestors to the set. |
| | | 394 | | /// </summary> |
| | | 395 | | private void AddNodeAndAncestors(TreeNode node, HashSet<Guid> set) |
| | | 396 | | { |
| | 18 | 397 | | set.Add(node.Id); |
| | | 398 | | |
| | | 399 | | // Add direct parent |
| | 18 | 400 | | if (node.ParentId.HasValue && _nodeIndex.TryGetNode(node.ParentId.Value, out var parent) && parent != null) |
| | | 401 | | { |
| | 2 | 402 | | AddNodeAndAncestors(parent, set); |
| | | 403 | | } |
| | | 404 | | |
| | | 405 | | // Also find the containing node (for virtual groups, etc.) |
| | 18 | 406 | | var container = _nodeIndex.FindParentNode(node); |
| | 18 | 407 | | if (container != null && !set.Contains(container.Id)) |
| | | 408 | | { |
| | 5 | 409 | | AddNodeAndAncestors(container, set); |
| | | 410 | | } |
| | 18 | 411 | | } |
| | | 412 | | |
| | | 413 | | // ============================================ |
| | | 414 | | // Persistence |
| | | 415 | | // ============================================ |
| | | 416 | | |
| | | 417 | | /// <summary> |
| | | 418 | | /// Gets the IDs of all currently expanded nodes. |
| | | 419 | | /// </summary> |
| | 1 | 420 | | public IReadOnlySet<Guid> GetExpandedNodeIds() => _expandedNodeIds; |
| | | 421 | | |
| | | 422 | | /// <summary> |
| | | 423 | | /// Restores expanded state from a set of node IDs. |
| | | 424 | | /// </summary> |
| | | 425 | | public void RestoreExpandedNodes(IEnumerable<Guid> nodeIds) |
| | | 426 | | { |
| | 17 | 427 | | _expandedNodeIds.Clear(); |
| | | 428 | | |
| | 42 | 429 | | foreach (var nodeId in nodeIds) |
| | | 430 | | { |
| | 4 | 431 | | if (_nodeIndex.TryGetNode(nodeId, out var node) && node != null) |
| | | 432 | | { |
| | 3 | 433 | | node.IsExpanded = true; |
| | 3 | 434 | | _expandedNodeIds.Add(nodeId); |
| | | 435 | | } |
| | | 436 | | } |
| | 17 | 437 | | } |
| | | 438 | | |
| | | 439 | | /// <summary> |
| | | 440 | | /// Restores expanded state from a previously saved set, preserving additional expanded nodes. |
| | | 441 | | /// Used during refresh to maintain state. |
| | | 442 | | /// </summary> |
| | | 443 | | public void RestoreExpandedNodesPreserving(IEnumerable<Guid> nodeIds) |
| | | 444 | | { |
| | 22 | 445 | | foreach (var nodeId in nodeIds) |
| | | 446 | | { |
| | 4 | 447 | | if (_nodeIndex.TryGetNode(nodeId, out var node) && node != null) |
| | | 448 | | { |
| | 2 | 449 | | node.IsExpanded = true; |
| | 2 | 450 | | _expandedNodeIds.Add(nodeId); |
| | | 451 | | } |
| | | 452 | | } |
| | 7 | 453 | | } |
| | | 454 | | |
| | | 455 | | /// <summary> |
| | | 456 | | /// Saves the current expanded state to localStorage. |
| | | 457 | | /// </summary> |
| | | 458 | | public async Task SaveExpandedStateAsync() |
| | | 459 | | { |
| | | 460 | | try |
| | | 461 | | { |
| | | 462 | | await _localStorage.SetItemAsync(ExpandedNodesStorageKey, _expandedNodeIds.ToList()); |
| | | 463 | | } |
| | | 464 | | catch (Exception ex) |
| | | 465 | | { |
| | | 466 | | _logger.LogWarning(ex, "Failed to save expanded state"); |
| | | 467 | | } |
| | | 468 | | } |
| | | 469 | | |
| | | 470 | | /// <summary> |
| | | 471 | | /// Restores expanded state from localStorage. |
| | | 472 | | /// </summary> |
| | | 473 | | public async Task RestoreExpandedStateFromStorageAsync() |
| | | 474 | | { |
| | | 475 | | try |
| | | 476 | | { |
| | | 477 | | var savedIds = await _localStorage.GetItemAsync<List<Guid>>(ExpandedNodesStorageKey); |
| | | 478 | | |
| | | 479 | | if (savedIds != null) |
| | | 480 | | { |
| | | 481 | | RestoreExpandedNodes(savedIds); |
| | | 482 | | } |
| | | 483 | | } |
| | | 484 | | catch (Exception ex) |
| | | 485 | | { |
| | | 486 | | _logger.LogWarning(ex, "Failed to restore expanded state"); |
| | | 487 | | } |
| | | 488 | | } |
| | | 489 | | |
| | | 490 | | /// <summary> |
| | | 491 | | /// Gets the localStorage key used for expanded nodes persistence. |
| | | 492 | | /// Exposed for testing purposes. |
| | | 493 | | /// </summary> |
| | 1 | 494 | | public static string GetExpandedNodesStorageKey() => ExpandedNodesStorageKey; |
| | | 495 | | } |