SystemSelectionModal.razor 5.9 KB

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