@inherits LayoutComponentBase @implements IDisposable @using BlazorWebAssembly.Services @inject ThemeService ThemeService @inject AuthenticationStateProvider AuthStateProvider @inject IJSRuntime JS @inject LibraryApiClient ApiClient @inject NotificationStateService NotifService @if (NotifService.UnreadCount > 0) { @(NotifService.UnreadCount > 9 ? "9+" : NotifService.UnreadCount) } Logout @Body LibraryLuxe Home Membership Features About Us Contact Sign In Get Started @Body @code { protected override async Task OnInitializedAsync() { await ThemeService.InitializeAsync(); AuthStateProvider.AuthenticationStateChanged += OnAuthStateChanged; NotifService.OnChange += HandleNotifChange; _ = UpdateNotificationCount(); } private async void OnAuthStateChanged(Task task) { await InvokeAsync(UpdateNotificationCount); } private async Task UpdateNotificationCount() { try { var authState = await AuthStateProvider.GetAuthenticationStateAsync(); if (authState.User.Identity?.IsAuthenticated == true) { var count = await ApiClient.GetUnreadNotificationCountAsync(); NotifService.SetUnreadCount(count); // Also update FCM token if not already done var token = await JS.InvokeAsync("getFcmToken"); if (!string.IsNullOrEmpty(token)) { await ApiClient.UpdateFcmTokenAsync(token); } } } catch (Exception ex) { Console.WriteLine($"[MainLayout] Error updating notifications: {ex.Message}"); } } protected override void OnAfterRender(bool firstRender) { // No longer using OnAfterRender for initial fetch as it's handled in OnInitialized and events } private async void HandleNotifChange() => await InvokeAsync(StateHasChanged); public void Dispose() { AuthStateProvider.AuthenticationStateChanged -= OnAuthStateChanged; NotifService.OnChange -= HandleNotifChange; } }