SystemSelectionModal.razor 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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]
  79. public string Title { get; set; } = "Select system";
  80. /// <summary>
  81. /// Default / pre-selected system name (may be null)
  82. /// </summary>
  83. [Parameter]
  84. public string? Value { get; set; }
  85. /// <summary>
  86. /// Called with the selected system name
  87. /// </summary>
  88. [Parameter]
  89. public EventCallback<string?> OnAccept { get; set; }
  90. /* ---------- State ---------- */
  91. private SelectionFormModel _model = new();
  92. private string _search = string.Empty;
  93. private List<SystemResource> _systems = new();
  94. private bool CanAccept => !string.IsNullOrWhiteSpace(_model.Value);
  95. /* ---------- Lifecycle ---------- */
  96. protected override async Task OnParametersSetAsync()
  97. {
  98. if (IsOpen)
  99. {
  100. _systems = (await SystemRepository.GetAllAsync())
  101. .OrderBy(s => s.Type)
  102. .ThenBy(s => s.Name)
  103. .ToList();
  104. _model = new SelectionFormModel
  105. {
  106. Value = Value
  107. };
  108. _search = string.Empty;
  109. }
  110. }
  111. /* ---------- Computed ---------- */
  112. private Dictionary<string, List<SystemResource>> FilteredSystems =>
  113. _systems
  114. .Where(s =>
  115. string.IsNullOrWhiteSpace(_search) ||
  116. s.Name.Contains(_search.Trim(), StringComparison.OrdinalIgnoreCase))
  117. .GroupBy(s => s.Type ?? "None")
  118. .ToDictionary(
  119. g => g.Key,
  120. g => g.OrderBy(s => s.Name).ToList());
  121. /* ---------- Actions ---------- */
  122. private async Task HandleAccept()
  123. {
  124. await OnAccept.InvokeAsync(_model.Value);
  125. await Close();
  126. }
  127. private async Task Cancel()
  128. {
  129. await Close();
  130. }
  131. private async Task Close()
  132. {
  133. _model = new();
  134. _search = string.Empty;
  135. await IsOpenChanged.InvokeAsync(false);
  136. }
  137. /* ---------- Model ---------- */
  138. private sealed class SelectionFormModel
  139. {
  140. [Required]
  141. 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. }