Files
Heimdall/src/Heimdall.Web/Components/Shared/NotificationBadge.razor
T

48 lines
1.8 KiB
Plaintext
Raw Normal View History

@* NotificationBadge.razor — cloche avec badge compteur temps réel (SignalR). *@
@implements IAsyncDisposable
@inject NotificationsHubClient Hub
@inject NavigationManager Navigation
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject Heimdall.Web.Services.UiText Text
<MudTooltip Text="@Text["SharedNotificationBadge_Title"]">
<MudBadge Color="Color.Error" Overlap="true" Bordered="true" Content="@Hub.UnreadCount"
Visible="@(Hub.UnreadCount > 0)">
<MudIconButton Icon="@Icons.Material.Filled.Notifications" Color="Color.Inherit"
aria-label="@Text["SharedNotificationBadge_Title"]"
OnClick="@(() => Navigation.NavigateTo("/events"))" />
</MudBadge>
</MudTooltip>
@code {
protected override async Task OnInitializedAsync()
{
Hub.UnreadCountChanged += OnUnreadChanged;
AuthenticationStateProvider.AuthenticationStateChanged += OnAuthenticationStateChanged;
await Hub.StartAsync();
await Hub.RefreshUnreadCountAsync();
}
private void OnAuthenticationStateChanged(Task<AuthenticationState> 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);
public async ValueTask DisposeAsync()
{
Hub.UnreadCountChanged -= OnUnreadChanged;
AuthenticationStateProvider.AuthenticationStateChanged -= OnAuthenticationStateChanged;
await Hub.DisposeAsync();
}
}