From e5b3aa58fb63d7595a3b14098634b73cc4a59201 Mon Sep 17 00:00:00 2001 From: Ho Ngoc Hai Date: Sun, 1 Mar 2026 00:11:23 +0700 Subject: [PATCH] fix(admin-layout): fix Guest display, error bar, empty shops - Subscribe to AuthState.OnChange to refresh user info after login - Call TryRestoreSessionAsync on first render to restore from localStorage - Wrap Body in ErrorBoundary with custom UI (replaces red error bar) - Properly unsubscribe OnChange in Dispose to prevent memory leaks --- .../Layout/AdminLayout.razor | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/apps/web-client-tpos-net/src/WebClientTpos.Client/Layout/AdminLayout.razor b/apps/web-client-tpos-net/src/WebClientTpos.Client/Layout/AdminLayout.razor index 5c9fd982..4f831cf8 100644 --- a/apps/web-client-tpos-net/src/WebClientTpos.Client/Layout/AdminLayout.razor +++ b/apps/web-client-tpos-net/src/WebClientTpos.Client/Layout/AdminLayout.razor @@ -134,14 +134,32 @@ @* ═══ MAIN AREA ═══ *@
- - @Body - + + + + @Body + + + +
+
+ +
+

Đã xảy ra lỗi

+

Trang gặp sự cố. Vui lòng tải lại.

+ +
+
+
@code { private bool _sidebarOpen = false; + private ErrorBoundary? _errorBoundary; // EN: Shop context detection — parsed from URL // VI: Phát hiện context cửa hàng — parse từ URL @@ -153,16 +171,29 @@ protected override void OnInitialized() { NavigationManager.LocationChanged += OnLocationChanged; + // EN: Subscribe to auth state changes so user display refreshes after login/logout + // VI: Subscribe auth state để cập nhật user display sau login/logout + AuthState.OnChange += OnAuthStateChanged; DetectShopContext(); } protected override async Task OnAfterRenderAsync(bool firstRender) { + if (firstRender) + { + // EN: Restore session from localStorage on first render (Blazor WASM ready) + // VI: Khôi phục session từ localStorage khi render lần đầu (Blazor WASM sẵn sàng) + await AuthSvc.TryRestoreSessionAsync(); + } // EN: Re-init Lucide icons after every render (Blazor navigation replaces DOM) // VI: Khởi tạo lại Lucide icons sau mỗi lần render (Blazor navigation thay đổi DOM) try { await JS.InvokeVoidAsync("lucide.createIcons"); } catch { } } + private void OnAuthStateChanged() => InvokeAsync(StateHasChanged); + + private void RecoverError() => _errorBoundary?.Recover(); + private void OnLocationChanged(object? sender, Microsoft.AspNetCore.Components.Routing.LocationChangedEventArgs e) { DetectShopContext(); @@ -251,5 +282,6 @@ public void Dispose() { NavigationManager.LocationChanged -= OnLocationChanged; + AuthState.OnChange -= OnAuthStateChanged; } }