using DeepDrftModels.DTOs;
using DeepDrftPublic.Client.ViewModels;
using Microsoft.AspNetCore.Components;
namespace DeepDrftPublic.Client.Pages;
///
/// Shared load + prerender-bridge logic for the single-release detail pages (Session, Mix).
/// Subclasses supply only their markup; this base loads the release through
/// and bridges the prerendered release across the prerender ->
/// WASM seam so the WASM pass does not re-fetch (see the TracksView seam). The playable track is
/// re-resolved on a restore miss only.
///
public abstract class ReleaseDetailBase : ComponentBase, IDisposable
{
[Parameter] public long Id { get; set; }
[Inject] public required ReleaseDetailViewModel ViewModel { get; set; }
[Inject] public required PersistentComponentState PersistentState { get; set; }
private PersistingComponentStateSubscription _persistingSubscription;
// Distinct keys per medium so a Session restore never lands on a Mix page.
protected abstract string PersistKey { get; }
protected override async Task OnInitializedAsync()
{
_persistingSubscription = PersistentState.RegisterOnPersisting(Persist);
// The bridged payload carries both the release and its resolved track so the interactive
// pass renders identically without a second round-trip.
if (PersistentState.TryTakeFromJson(PersistKey, out var restored) && restored?.Release is not null)
{
ViewModel.Restore(restored.Release, restored.Track);
}
else
{
await ViewModel.Load(Id);
}
}
private Task Persist()
{
if (ViewModel.Release is not null)
PersistentState.PersistAsJson(PersistKey, new BridgedDetail(ViewModel.Release, ViewModel.Track));
return Task.CompletedTask;
}
public void Dispose() => _persistingSubscription.Dispose();
// JSON-serializable bridge payload. Round-trips through PersistentComponentState's serializer.
protected sealed record BridgedDetail(ReleaseDto Release, TrackDto? Track);
}