using DeepDrftModels.DTOs; using DeepDrftModels.Enums; using DeepDrftPublic.Client.Services; using Microsoft.AspNetCore.Components; using Models.Common; namespace DeepDrftPublic.Client.Pages; /// /// Medium-filtered release gallery. Routed at /cuts (Cut releases) and parameterized by /// so the same component can back any medium's card grid without a fork. /// Cards open the track gallery filtered to that release's album title, preserving the original /// /albums ergonomics. /// public partial class AlbumsView : ComponentBase, IDisposable { private const string PersistKeyPrefix = "albums-view-"; [Inject] public required IReleaseDataService ReleaseData { get; set; } [Inject] public required PersistentComponentState PersistentState { get; set; } [Inject] public required NavigationManager Navigation { get; set; } // The medium whose releases this grid shows. Defaults to Cut for the /cuts route; other media // can reuse this component by passing a different value. Drives both the fetch filter and the // per-medium persistence key so prerendered state never bleeds across media. [Parameter] public ReleaseMedium Medium { get; set; } = ReleaseMedium.Cut; private bool _loading = true; private List _albums = []; private PersistingComponentStateSubscription _persistingSubscription; private string PersistKey => $"{PersistKeyPrefix}{Medium}"; protected override async Task OnInitializedAsync() { // Bridge the prerendered fetch across the prerender -> WASM seam (see TracksView). Without // this, the WASM pass re-fetches and replays the card entrance animations. _persistingSubscription = PersistentState.RegisterOnPersisting(PersistAlbums); if (PersistentState.TryTakeFromJson>(PersistKey, out var restored) && restored is not null) { _albums = restored; _loading = false; return; } var result = await ReleaseData.GetPaged(Medium.ToString().ToLowerInvariant(), page: 1, pageSize: 100); if (result is { Success: true, Value: { Items: { } items } }) _albums = items.ToList(); _loading = false; } private Task PersistAlbums() { if (_albums.Count > 0) PersistentState.PersistAsJson(PersistKey, _albums); return Task.CompletedTask; } private void OpenAlbum(string album) => Navigation.NavigateTo($"/tracks?album={Uri.EscapeDataString(album)}"); public void Dispose() => _persistingSubscription.Dispose(); }