SystemSelectionModal.razor 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. @using System.ComponentModel.DataAnnotations
  2. @using RackPeek.Domain.Persistence
  3. @using RackPeek.Domain.Resources.SystemResources
  4. @inject IResourceCollection Repo
  5. @if (IsOpen)
  6. {
  7. <div class="fixed inset-0 z-50 flex items-center justify-center">
  8. <!-- Backdrop -->
  9. <div class="absolute inset-0 bg-black/70" @onclick="Cancel"></div>
  10. <!-- Modal -->
  11. <div class="relative bg-zinc-900 border border-zinc-800 rounded w-full max-w-lg p-4">
  12. <!-- Header -->
  13. <div class="flex justify-between items-center mb-4">
  14. <div class="text-zinc-100 text-sm font-medium">
  15. @Title
  16. </div>
  17. <button
  18. class="text-zinc-400 hover:text-zinc-200"
  19. @onclick="Cancel">
  20. ✕
  21. </button>
  22. </div>
  23. <!-- Form -->
  24. <EditForm Model="_model" OnValidSubmit="HandleAccept">
  25. <DataAnnotationsValidator/>
  26. <div class="space-y-3 text-sm">
  27. <!-- Search -->
  28. <div>
  29. @* ReSharper disable once CSharpWarnings::CS8619 *@
  30. <InputText
  31. class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  32. placeholder="Search systems…"
  33. Value="@_search"
  34. ValueChanged="OnSearchChanged"
  35. ValueExpression="() => _search"
  36. @oninput="OnSearchInput"/>
  37. </div>
  38. <!-- Selection -->
  39. <div>
  40. <InputSelect
  41. class="w-full bg-zinc-800 border border-zinc-700 rounded px-2 py-1 text-zinc-100"
  42. @bind-Value="_model.Value">
  43. <option value="">Select system…</option>
  44. @foreach (var group in FilteredSystems)
  45. {
  46. <optgroup label="@group.Key">
  47. @foreach (var system in group.Value)
  48. {
  49. <option value="@system.Name">
  50. @system.Name
  51. </option>
  52. }
  53. </optgroup>
  54. }
  55. </InputSelect>
  56. </div>
  57. </div>
  58. <!-- Actions -->
  59. <div class="flex justify-end items-center mt-5 gap-2">
  60. <button
  61. type="button"
  62. class="px-3 py-1 rounded border border-zinc-700 text-zinc-300 hover:bg-zinc-800"
  63. @onclick="Cancel">
  64. Cancel
  65. </button>
  66. <button
  67. type="submit"
  68. class="px-3 py-1 rounded bg-emerald-600 text-black hover:bg-emerald-500"
  69. disabled="@(!CanAccept)">
  70. Accept
  71. </button>
  72. </div>
  73. </EditForm>
  74. </div>
  75. </div>
  76. }
  77. @code {
  78. /* ---------- Parameters ---------- */
  79. [Parameter] public bool IsOpen { get; set; }
  80. [Parameter] public EventCallback<bool> IsOpenChanged { get; set; }
  81. [Parameter] [EditorRequired] public string Title { get; set; } = "Select system";
  82. /// <summary>
  83. /// Default / pre-selected system name (may be null)
  84. /// </summary>
  85. [Parameter]
  86. public string? Value { get; set; }
  87. /// <summary>
  88. /// Called with the selected system name
  89. /// </summary>
  90. [Parameter]
  91. public EventCallback<string?> OnAccept { get; set; }
  92. /* ---------- State ---------- */
  93. private SelectionFormModel _model = new();
  94. private string _search = string.Empty;
  95. private List<SystemResource> _systems = new();
  96. private bool CanAccept => !string.IsNullOrWhiteSpace(_model.Value);
  97. /* ---------- Lifecycle ---------- */
  98. protected override async Task OnParametersSetAsync()
  99. {
  100. if (IsOpen)
  101. {
  102. _systems = (await Repo.GetAllOfTypeAsync<SystemResource>())
  103. .OrderBy(s => s.Type)
  104. .ThenBy(s => s.Name)
  105. .ToList();
  106. _model = new SelectionFormModel
  107. {
  108. Value = Value
  109. };
  110. _search = string.Empty;
  111. }
  112. }
  113. /* ---------- Computed ---------- */
  114. private Dictionary<string, List<SystemResource>> FilteredSystems =>
  115. _systems
  116. .Where(s =>
  117. string.IsNullOrWhiteSpace(_search) ||
  118. s.Name.Contains(_search.Trim(), StringComparison.OrdinalIgnoreCase))
  119. .GroupBy(s => s.Type ?? "None")
  120. .ToDictionary(
  121. g => g.Key,
  122. g => g.OrderBy(s => s.Name).ToList());
  123. /* ---------- Actions ---------- */
  124. private async Task HandleAccept()
  125. {
  126. await OnAccept.InvokeAsync(_model.Value);
  127. await Close();
  128. }
  129. private async Task Cancel()
  130. {
  131. await Close();
  132. }
  133. private async Task Close()
  134. {
  135. _model = new SelectionFormModel();
  136. _search = string.Empty;
  137. await IsOpenChanged.InvokeAsync(false);
  138. }
  139. /* ---------- Model ---------- */
  140. private sealed class SelectionFormModel
  141. {
  142. [Required] public string? Value { get; set; }
  143. }
  144. /* ---------- Search ---------- */
  145. private void OnSearchChanged(string? value)
  146. {
  147. _search = value ?? string.Empty;
  148. }
  149. private void OnSearchInput(ChangeEventArgs e)
  150. {
  151. _search = e.Value?.ToString() ?? string.Empty;
  152. // Clear selection if it is no longer visible
  153. if (FilteredSystems
  154. .SelectMany(g => g.Value)
  155. .All(s => s.Name != _model.Value))
  156. {
  157. _model.Value = null;
  158. }
  159. }
  160. }