4
0

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