diff --git a/src/Heimdall.Web/Components/Shared/NotificationBadge.razor b/src/Heimdall.Web/Components/Shared/NotificationBadge.razor
index 0fd3064..c38e9fa 100644
--- a/src/Heimdall.Web/Components/Shared/NotificationBadge.razor
+++ b/src/Heimdall.Web/Components/Shared/NotificationBadge.razor
@@ -2,23 +2,39 @@
@implements IAsyncDisposable
@inject NotificationsHubClient Hub
@inject NavigationManager Navigation
+@inject AuthenticationStateProvider AuthenticationStateProvider
@inject Heimdall.Web.Services.UiText Text
-
- @if (Hub.UnreadCount > 0)
- {
-
- }
-
+
+
+
@code {
protected override async Task OnInitializedAsync()
{
Hub.UnreadCountChanged += OnUnreadChanged;
+ AuthenticationStateProvider.AuthenticationStateChanged += OnAuthenticationStateChanged;
await Hub.StartAsync();
+ await Hub.RefreshUnreadCountAsync();
+ }
+
+ private void OnAuthenticationStateChanged(Task authenticationStateTask)
+ {
+ _ = InvokeAsync(async () =>
+ {
+ var authenticationState = await authenticationStateTask;
+ if (authenticationState.User.Identity?.IsAuthenticated == true)
+ {
+ await Hub.StartAsync();
+ await Hub.RefreshUnreadCountAsync();
+ }
+ StateHasChanged();
+ });
}
private void OnUnreadChanged(int _) => InvokeAsync(StateHasChanged);
@@ -26,6 +42,7 @@
public async ValueTask DisposeAsync()
{
Hub.UnreadCountChanged -= OnUnreadChanged;
+ AuthenticationStateProvider.AuthenticationStateChanged -= OnAuthenticationStateChanged;
await Hub.DisposeAsync();
}
}
diff --git a/src/Heimdall.Web/Hubs/NotificationsHub.cs b/src/Heimdall.Web/Hubs/NotificationsHub.cs
index f546c1c..415edee 100644
--- a/src/Heimdall.Web/Hubs/NotificationsHub.cs
+++ b/src/Heimdall.Web/Hubs/NotificationsHub.cs
@@ -20,9 +20,16 @@ public class NotificationsHub : Hub
private readonly ILogger _logger;
private readonly ResourceAccess _access;
private readonly LiveSubscriptions _subscriptions;
+ private readonly INotificationService _notifications;
- public NotificationsHub(ILogger logger, ResourceAccess access, LiveSubscriptions subscriptions)
- { _logger = logger; _access = access; _subscriptions = subscriptions; }
+ public NotificationsHub(ILogger logger, ResourceAccess access,
+ LiveSubscriptions subscriptions, INotificationService notifications)
+ {
+ _logger = logger;
+ _access = access;
+ _subscriptions = subscriptions;
+ _notifications = notifications;
+ }
///
/// Clients call this to join their personal notification channel.
@@ -36,6 +43,8 @@ public class NotificationsHub : Hub
var userId = requester.Id.ToString();
_subscriptions.Join("notifications", Context.ConnectionId, userId, Context.User!);
_logger.LogDebug("Client {ConnectionId} joined personal notifications for user {UserId}", Context.ConnectionId, userId);
+ var unreadCount = await _notifications.GetUnreadCountAsync(requester.Id, Context.ConnectionAborted);
+ await Clients.Caller.SendAsync("UnreadCount", new { count = unreadCount }, Context.ConnectionAborted);
}
public Task LeavePersonalNotifications()
diff --git a/src/Heimdall.Web/Services/HeimdallApiClient.cs b/src/Heimdall.Web/Services/HeimdallApiClient.cs
index d465f96..1b96cae 100644
--- a/src/Heimdall.Web/Services/HeimdallApiClient.cs
+++ b/src/Heimdall.Web/Services/HeimdallApiClient.cs
@@ -124,6 +124,16 @@ public class HeimdallApiClient : IDisposable
return await client.GetFromJsonAsync("api/users/me", Json);
}
+ public async Task GetUnreadNotificationCountAsync()
+ {
+ using var response = await CreateClient().GetAsync("api/notifications/unread-count");
+ if (!response.IsSuccessStatusCode)
+ return 0;
+
+ var result = await response.Content.ReadFromJsonAsync(Json);
+ return result?.Count ?? 0;
+ }
+
public async Task UpdateMyProfileAsync(string displayName, string? theme, string? language)
{
var client = CreateClient();
diff --git a/src/Heimdall.Web/Services/NotificationsHubClient.cs b/src/Heimdall.Web/Services/NotificationsHubClient.cs
index fef44e2..177296d 100644
--- a/src/Heimdall.Web/Services/NotificationsHubClient.cs
+++ b/src/Heimdall.Web/Services/NotificationsHubClient.cs
@@ -1,3 +1,4 @@
+using System.Text.Json;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.SignalR.Client;
@@ -6,13 +7,13 @@ namespace Heimdall.Web.Services;
///
/// Client hub SignalR pour les notifications temps réel (badge compteur).
/// S'abonne au groupe personnel "notif_{userId}" et expose l'événement
-/// . Le polling est le fallback : si la
-/// connexion échoue (pas de JWT en session cookie, hub indisponible), le
-/// badge reste silencieux sans casser l'UI.
+/// . Un chargement API initial garantit le
+/// compteur même si la connexion temps réel n'est pas encore disponible.
///
public class NotificationsHubClient : IAsyncDisposable, IDisposable
{
private readonly AuthState _authState;
+ private readonly HeimdallApiClient _api;
private readonly NavigationManager _navigation;
private readonly ILogger _logger;
private HubConnection? _connection;
@@ -20,10 +21,12 @@ public class NotificationsHubClient : IAsyncDisposable, IDisposable
public NotificationsHubClient(
AuthState authState,
+ HeimdallApiClient api,
NavigationManager navigation,
ILogger logger)
{
_authState = authState;
+ _api = api;
_navigation = navigation;
_logger = logger;
}
@@ -33,6 +36,20 @@ public class NotificationsHubClient : IAsyncDisposable, IDisposable
public event Action? UnreadCountChanged;
+ public async Task RefreshUnreadCountAsync()
+ {
+ try
+ {
+ var count = await _api.GetUnreadNotificationCountAsync();
+ UnreadCount = count;
+ UnreadCountChanged?.Invoke(count);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogDebug(ex, "NotificationsHubClient: chargement initial du compteur impossible");
+ }
+ }
+
public async Task StartAsync()
{
if (_started || !_authState.IsLoggedIn)
@@ -52,18 +69,18 @@ public class NotificationsHubClient : IAsyncDisposable, IDisposable
.WithAutomaticReconnect()
.Build();
- _connection.On