Files
Heimdall/src/Heimdall.Web/Components/Pages/EventsPage.razor
T

253 lines
10 KiB
Plaintext
Raw Normal View History

@page "/events"
@attribute [Authorize]
@layout MainLayout
@inject HeimdallApiClient Api
@inject ISnackbar Snackbar
@inject NavigationManager Nav
@using Microsoft.AspNetCore.WebUtilities
<MudText Typo="Typo.h1" Class="mb-2">
<i class="fas fa-bell mr-2"></i>
Événements
</MudText>
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-4">
@(_total > 0 ? $"{_total} événement(s) trouvé(s)" : "Historique des détections")
</MudText>
@* ─── Barre de filtres fonctionnelle ─── *@
<MudPaper Elevation="1" Class="pa-4 mb-4" Style="border-radius: var(--hmd-radius-md);">
<MudGrid>
<MudItem xs="12" sm="12" md="4">
<MudTextField @bind-Value="_search" Label="Rechercher (label, caméra…)" Variant="Variant.Outlined"
Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Filled.Search"
OnAdornmentClick="@ApplyFilters" OnKeyDown="@HandleSearchKey"
Immediate="true" Class="mb-2 mb-md-0" />
</MudItem>
<MudItem xs="12" sm="6" md="3">
<MudSelect T="string" @bind-Value="_selectedType" Label="Type" Variant="Variant.Outlined"
Class="mb-2 mb-md-0">
<MudSelectItem T="string" Value="@("")">Tous les types</MudSelectItem>
<MudSelectItem T="string" Value="@("MotionDetected")">Mouvement</MudSelectItem>
<MudSelectItem T="string" Value="@("PersonDetected")">Personne</MudSelectItem>
<MudSelectItem T="string" Value="@("VehicleDetected")">Véhicule</MudSelectItem>
<MudSelectItem T="string" Value="@("FaceRecognized")">Visage</MudSelectItem>
<MudSelectItem T="string" Value="@("CameraOffline")">Hors ligne</MudSelectItem>
</MudSelect>
</MudItem>
<MudItem xs="12" sm="6" md="3">
<MudDateRangePicker @bind-DateRange="_dateRange" Label="Période" Variant="Variant.Outlined"
Class="mb-2 mb-md-0" />
</MudItem>
<MudItem xs="12" md="2" Class="d-flex align-center justify-end">
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@ApplyFilters" Class="w-100">
<MudIcon Icon="@Icons.Material.Filled.Search" Class="mr-1" Size="Size.Small" /> Filtrer
</MudButton>
</MudItem>
</MudGrid>
</MudPaper>
@* ─── 4 états : loading / error / empty / data ─── *@
@if (_loading)
{
<MudPaper Elevation="0" Style="border-radius: var(--hmd-radius-md);">
<MudSkeleton Variant="SkeletonVariant.Rectangular" Height="120px" />
<MudSkeleton Variant="SkeletonVariant.Rectangular" Height="120px" Class="mt-2" />
<MudSkeleton Variant="SkeletonVariant.Rectangular" Height="120px" Class="mt-2" />
</MudPaper>
}
else if (_error)
{
<MudAlert Severity="Severity.Error" Variant="Variant.Filled" Class="mt-4">
<MudText>Impossible de charger les événements. Réessayez dans un instant.</MudText>
<MudButton Variant="Variant.Text" Color="Color.Inherit" OnClick="@LoadEvents" Class="mt-2">
<MudIcon Icon="@Icons.Material.Filled.Refresh" Class="mr-1" Size="Size.Small" /> Réessayer
</MudButton>
</MudAlert>
}
else if (_events is null || _events.Count == 0)
{
<MudPaper Elevation="0" Class="pa-8 text-center" Style="border-radius: var(--hmd-radius-md); border: 1px dashed var(--mud-palette-lines-default);">
<MudIcon Icon="@Icons.Material.Filled.NotificationsOff" Size="Size.Large" Color="Color.Secondary" />
<MudText Typo="Typo.h6" Color="Color.Secondary" Class="mt-4">Aucun événement trouvé</MudText>
<MudText Typo="Typo.body2" Color="Color.Secondary" Class="mb-4">
@if (_hasActiveFilters)
{
<text>Modifiez vos filtres ou</text>
}
else
{
<text>Les détections apparaîtront ici dès que vos caméras repèrent un mouvement.</text>
}
</MudText>
@if (_hasActiveFilters)
{
<MudButton Variant="Variant.Outlined" Color="Color.Default" OnClick="@ClearFilters">
<MudIcon Icon="@Icons.Material.Filled.Clear" Class="mr-1" /> Effacer les filtres
</MudButton>
}
</MudPaper>
}
else
{
@* ─── Timeline : liste structurée avec badges colorés ─── *@
<MudPaper Elevation="1" Class="pa-2" Style="border-radius: var(--hmd-radius-md);">
@foreach (var ev in _events)
{
<div class="d-flex align-center pa-3 event-row" style="border-bottom: 1px solid var(--mud-palette-lines-default);">
<div class="d-flex flex-column mr-4" style="min-width: 150px;">
<MudText Typo="Typo.body2" Class="font-weight-medium">
@ev.OccurredAt.ToLocalTime().ToString("dd/MM HH:mm")
</MudText>
<MudText Typo="Typo.caption" Color="Color.Secondary">
@ev.OccurredAt.ToLocalTime().ToString("ss") s
</MudText>
</div>
<MudChip T="string" Size="Size.Small" Color="@GetEventColor(ev.Type)" Variant="Variant.Filled"
Class="mr-3" Style="min-width: 110px; justify-content: center;">
@FormatEventType(ev.Type)
</MudChip>
<div class="d-flex flex-column" style="flex:1; min-width:0;">
<MudLink Href="@($"/cameras/{ev.CameraId}")" Typo="Typo.body2" Class="text-truncate">
@ev.Label ?? "Caméra " + ev.CameraId.ToString()[..8]
</MudLink>
<MudText Typo="Typo.caption" Color="Color.Secondary">
@($"Confiance {(ev.Confidence * 100):0}%")
</MudText>
</div>
<MudIconButton Icon="@Icons.Material.Filled.Image" Size="Size.Small" aria-label="Voir le snapshot"
Disabled="@(ev.SnapshotUrl is null)"
OnClick="@(() => ShowSnapshot(ev))" />
</div>
}
</MudPaper>
<PaginationBar Page="@_page" TotalPages="@_totalPages" Total="@_total"
Previous="@(async () => await ChangePageAsync(_page - 1))"
Next="@(async () => await ChangePageAsync(_page + 1))" />
}
<MudDialog @bind-Visible="_showSnapshot" Options="_dialogOptions">
<TitleContent>
<MudText Typo="Typo.h6">Snapshot — @FormatEventType(_showSnapshotEvent!.Type)</MudText>
</TitleContent>
<DialogContent>
<img src="@_showSnapshotEvent!.SnapshotUrl" alt="Snapshot" style="width:100%" class="rounded-lg" />
</DialogContent>
<DialogActions>
<MudButton Variant="Variant.Text" OnClick="@(() => _showSnapshot = false)">Fermer</MudButton>
</DialogActions>
</MudDialog>
@code {
private const int PageSize = 50;
private List<EventItem>? _events;
private bool _loading;
private bool _error;
private bool _showSnapshot;
private EventItem? _showSnapshotEvent;
private string _search = "";
private string _selectedType = "";
private DateRange? _dateRange;
private int _page = 1;
private int _totalPages = 1;
private int _total;
private bool _hasActiveFilters =>
!string.IsNullOrWhiteSpace(_search) || !string.IsNullOrEmpty(_selectedType) || _dateRange?.Start is not null;
private readonly DialogOptions _dialogOptions = new()
{
FullWidth = true,
MaxWidth = MaxWidth.Large,
CloseButton = true
};
protected override async Task OnInitializedAsync()
{
var uri = Nav.Uri;
var query = uri.Contains('?') ? uri[(uri.IndexOf('?') + 1)..] : "";
var q = QueryHelpers.ParseQuery(query).TryGetValue("q", out var qv) ? qv.ToString() : null;
if (!string.IsNullOrWhiteSpace(q)) _search = q;
await LoadEvents();
}
private async Task ApplyFilters()
{
_page = 1;
await LoadEvents();
}
private async Task ClearFilters()
{
_search = "";
_selectedType = "";
_dateRange = null;
_page = 1;
await LoadEvents();
}
private async Task ChangePageAsync(int page)
{
_page = page;
await LoadEvents();
}
private async Task LoadEvents()
{
_loading = true;
_error = false;
try
{
var from = _dateRange?.Start is { } start ? start.ToUniversalTime() : (DateTime?)null;
var to = _dateRange?.End is { } end ? end.ToUniversalTime().AddDays(1) : (DateTime?)null;
var result = await Api.GetEventsAsync(null, null, _page, PageSize,
string.IsNullOrWhiteSpace(_search) ? null : _search,
string.IsNullOrEmpty(_selectedType) ? null : _selectedType,
from, to);
_events = result?.Items ?? [];
_total = result?.TotalCount ?? 0;
_totalPages = Math.Max(1, (int)Math.Ceiling(_total / (double)PageSize));
}
catch (Exception ex)
{
_error = true;
Snackbar.Add($"Erreur : {ex.Message}", Severity.Error);
}
finally { _loading = false; }
}
private async Task HandleSearchKey(KeyboardEventArgs e)
{
if (e.Key == "Enter") await ApplyFilters();
}
private void ShowSnapshot(EventItem ev)
{
_showSnapshotEvent = ev;
_showSnapshot = true;
}
private static string FormatEventType(string type) => type switch
{
"MotionDetected" => "Mouvement",
"PersonDetected" => "Personne",
"VehicleDetected" => "Véhicule",
"FaceRecognized" => "Visage",
"CameraOffline" => "Hors ligne",
_ => type
};
private static Color GetEventColor(string type) => type switch
{
"MotionDetected" => Color.Warning,
"PersonDetected" => Color.Error,
"VehicleDetected" => Color.Info,
"FaceRecognized" => Color.Success,
_ => Color.Default
};
}