@page "/" @page "/{Username}" @rendermode @(new InteractiveWebAssemblyRenderMode(prerender: false)) @implements IAsyncDisposable @inject HttpClient Http @inject ISnackbar Snackbar @inject LangService Lang
@if (!string.IsNullOrEmpty(_username)) { @_username }
@if (_isManager) { } @* SIM / REAL toggle — chỉ hiện khi tài khoản có quyền real *@ @if (_canUseReal) { @(_useReal ? "REAL" : "SIM") } else { SIM }
@{ var wfBlocker = WfBlocker; } @if (wfBlocker is not null) {
WORKFLOW MODE — @wfBlocker.Name @Lang.T("đang điều khiển · Bạn không thể thao tác", "is controlling · You cannot perform actions")
}
@if (!State.System.IsSimulation || _isManager) {
}
@code { [Parameter] public string? Username { get; set; } [Inject] StateService State { get; set; } = default!; bool _notifMuted = true; bool _isManager = false; bool _canUseReal = false; bool _canWorkflow = false; bool _useReal = false; bool _stackBusy = false; string _username = ""; string _userId = ""; string _role = ""; protected override async Task OnInitializedAsync() { await State.StartAsync(); State.SystemChanged += Refresh; State.LockStateChanged += Refresh; State.StackChanged += OnStackChanged; Lang.LangChanged += Refresh; await Lang.LoadAsync(Http); try { var me = await Http.GetFromJsonAsync("/api/auth/me"); _isManager = me?.Role is "Manager" or "Admin"; _username = me?.Username ?? Username ?? ""; _canUseReal = me?.CanUseReal == true; _canWorkflow = me?.CanWorkflow == true; _useReal = me?.UseReal == true; _userId = me?.UserId ?? ""; _role = me?.Role ?? ""; } catch { } try { var prefs = await Http.GetFromJsonAsync("/api/prefs"); if (prefs is not null) _notifMuted = prefs.NotifMuted; // Lang already loaded via Lang.LoadAsync above } catch { } } private async Task ToggleStack() { if (_stackBusy || !_canUseReal) return; _stackBusy = true; try { bool newUseReal = !_useReal; var url = newUseReal ? "/api/mode/real" : "/api/mode/simulation"; var resp = await Http.PostAsync(url, null); if (resp.IsSuccessStatusCode) { _useReal = newUseReal; await State.SwitchStackAsync(newUseReal); } } catch { } finally { _stackBusy = false; } } private void Refresh() => InvokeAsync(StateHasChanged); private void OnStackChanged(bool useReal) { _useReal = useReal; InvokeAsync(StateHasChanged); } public ValueTask DisposeAsync() { State.SystemChanged -= Refresh; State.LockStateChanged -= Refresh; State.StackChanged -= OnStackChanged; Lang.LangChanged -= Refresh; return ValueTask.CompletedTask; } /// Workflow lock holder khi KHÔNG phải mình — cần hiện banner cảnh báo. LockHolderDto? WfBlocker => !State.System.IsSimulation && State.LockState?.Workflow is { } h && h.UserId != _userId ? h : null; private async Task ToggleLang() => await Lang.ToggleAsync(Http); private async Task ToggleNotif() { _notifMuted = !_notifMuted; try { await Http.PutAsJsonAsync("/api/prefs", new { Lang = (string?)null, NotifMuted = (bool?)_notifMuted }); } catch { } } record MeDto(string Role, string Username, bool CanUseReal, bool UseReal, string? UserId, bool CanWorkflow); record PrefsDto(string Lang, bool NotifMuted); }