Files
Khaemhat/src/Khaemhat.Web/Shell/Feedback.cs
T
Alby96andClaude Opus 5.5 d958e7372d feat: Kh component library, record pages and profile security (M3, part 2)
Adds the building blocks every later screen uses, so modules are built from
one set of components instead of ad hoc markup:

- Form components (KhForm, KhTextBox, KhNumberBox, KhDateBox, KhSelect,
  KhCheck, KhSwitch, KhHotlink) with Italian number and date input, inline
  calculation (=10*3), field messages plus a summary that focuses the field,
  and service errors added by field name.
- Display and window components (panels, tiles, badges, chips, money, tabs
  with Alt+1..9, steps, dialog, side drawer and DrawerService).
- KhIconToolbar that keeps one line and moves less used commands into the
  "..." menu, KhDocHead, and the RecordPage/DocumentPage/MasterDataPage bases
  with standard commands, shortcuts and save/cancel/delete flow (#27, #29).
- Development-only component catalog and sample document page.
- Profile: personal data, password change that ends other sessions, TOTP
  enable/disable and recovery codes through ProfileService (#31).

Refs #27 #29 #31

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:11:16 +01:00

159 lines
5.5 KiB
C#

namespace Khaemhat.Web.Shell;
/// <summary>The weight of a toast.</summary>
public enum ToastKind
{
/// <summary>Information.</summary>
Info = 0,
/// <summary>Done.</summary>
Success = 1,
/// <summary>Attention.</summary>
Warning = 2,
/// <summary>Failure.</summary>
Error = 3,
}
/// <summary>A short message in the bottom right corner (SPEC §10.7 <c>KhToast</c>).</summary>
/// <param name="Id">The identifier.</param>
/// <param name="Kind">The weight.</param>
/// <param name="Title">The title.</param>
/// <param name="Text">The text, or <see langword="null"/>.</param>
public sealed record Toast(Guid Id, ToastKind Kind, string Title, string? Text);
/// <summary>Shows toasts; <c>KhToastHost</c> in the layout renders them and removes them after a few seconds.</summary>
public sealed class ToastService
{
private readonly List<Toast> _toasts = [];
/// <summary>Raised when toasts appear or go.</summary>
public event Action? Changed;
/// <summary>The toasts on screen.</summary>
public IReadOnlyList<Toast> Toasts => _toasts;
/// <summary>Shows a toast.</summary>
/// <param name="kind">The weight.</param>
/// <param name="title">The title.</param>
/// <param name="text">The text.</param>
/// <returns>The toast.</returns>
public Toast Show(ToastKind kind, string title, string? text = null)
{
var toast = new Toast(Guid.NewGuid(), kind, title, text);
_toasts.Add(toast);
if (_toasts.Count > 5)
{
_toasts.RemoveAt(0);
}
Changed?.Invoke();
return toast;
}
/// <summary>Removes a toast.</summary>
/// <param name="id">The toast.</param>
public void Dismiss(Guid id)
{
if (_toasts.RemoveAll(t => t.Id == id) > 0)
{
Changed?.Invoke();
}
}
}
/// <summary>A question that needs a yes or no (SPEC §10.7 <c>KhConfirm</c>).</summary>
/// <param name="Title">The title.</param>
/// <param name="Text">The question.</param>
/// <param name="ConfirmText">The text of the confirming button.</param>
/// <param name="CancelText">The text of the cancelling button.</param>
/// <param name="Danger">Whether confirming destroys something (red button).</param>
public sealed record ConfirmRequest(string Title, string Text, string ConfirmText, string CancelText, bool Danger = false);
/// <summary>Asks confirmations; <c>KhDialogHost</c> in the layout shows them one at a time.</summary>
public sealed class DialogService
{
private TaskCompletionSource<bool>? _pending;
/// <summary>Raised when a question appears or is answered.</summary>
public event Action? Changed;
/// <summary>The question on screen, or <see langword="null"/>.</summary>
public ConfirmRequest? Current { get; private set; }
/// <summary>Asks a question and waits for the answer.</summary>
/// <param name="request">The question.</param>
/// <returns><see langword="true"/> when the user confirms.</returns>
public Task<bool> ConfirmAsync(ConfirmRequest request)
{
ArgumentNullException.ThrowIfNull(request);
_pending?.TrySetResult(false);
_pending = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
Current = request;
Changed?.Invoke();
return _pending.Task;
}
/// <summary>Called by the dialog with the answer.</summary>
/// <param name="confirmed">The answer.</param>
public void Answer(bool confirmed)
{
var pending = _pending;
_pending = null;
Current = null;
Changed?.Invoke();
pending?.TrySetResult(confirmed);
}
}
/// <summary>A side panel opened from code (SPEC §10.7 <c>DrawerService</c>).</summary>
/// <param name="Title">The title.</param>
/// <param name="Icon">The icon.</param>
/// <param name="Content">The content.</param>
/// <param name="Footer">The buttons, or <see langword="null"/>.</param>
/// <param name="Wide">A wider panel.</param>
public sealed record DrawerRequest(string Title, string? Icon, Microsoft.AspNetCore.Components.RenderFragment Content,
Microsoft.AspNetCore.Components.RenderFragment? Footer = null, bool Wide = false);
/// <summary>
/// Opens one side panel at a time from code; <c>KhDrawerHost</c> in the layout shows it. A page that owns its panel
/// can also write <c>&lt;KhDrawer Open="..."&gt;</c> directly.
/// </summary>
public sealed class DrawerService
{
private TaskCompletionSource? _closed;
/// <summary>Raised when the panel opens, closes or must be drawn again.</summary>
public event Action? Changed;
/// <summary>The panel shown, or <see langword="null"/>.</summary>
public DrawerRequest? Current { get; private set; }
/// <summary>Shows a panel, closing the previous one.</summary>
/// <param name="request">The panel.</param>
/// <returns>A task that ends when the panel is closed.</returns>
public Task ShowAsync(DrawerRequest request)
{
ArgumentNullException.ThrowIfNull(request);
_closed?.TrySetResult();
_closed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
Current = request;
Changed?.Invoke();
return _closed.Task;
}
/// <summary>Closes the panel.</summary>
public void Close()
{
var closed = _closed;
_closed = null;
Current = null;
Changed?.Invoke();
closed?.TrySetResult();
}
/// <summary>Draws the panel again after its data changed.</summary>
public void Refresh() => Changed?.Invoke();
}