using DeepDrftModels.DTOs; using DeepDrftModels.Enums; using DeepDrftPublic.Client.Common; 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 release's dedicated detail page via /// (a Cut routes to /cuts/{entryKey}), the single source for medium→route resolution (Phase 11 §2). /// 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; } // 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 MediumBrowseBase). // 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; } // Cut cards show track count where the shared card otherwise shows the artist. private static string TrackCountLabel(ReleaseDto album) => $"{album.TrackCount} {(album.TrackCount == 1 ? "track" : "tracks")}"; public void Dispose() => _persistingSubscription.Dispose(); }