Files
deepdrft/DeepDrftPublic.Client/Pages/TrackDetail.razor.cs
T
daniel-c-harvey f02f370ed9 fix(detail): reload track on route-param change so Play uses the right track
Detail pages loaded only in OnInitialized, which doesn't re-run when an
InteractiveAuto component instance is reused across same-template navigations,
leaving a stale track that Play streamed. Move load to OnParametersSetAsync
keyed on the route id, and guard the prerender bridge restore against an id mismatch.
2026-06-15 12:47:57 -04:00

66 lines
2.5 KiB
C#

using DeepDrftModels.DTOs;
using DeepDrftPublic.Client.ViewModels;
using Microsoft.AspNetCore.Components;
namespace DeepDrftPublic.Client.Pages;
public partial class TrackDetail : ComponentBase, IDisposable
{
private const string PersistKey = "track-detail";
[Parameter] public required string EntryKey { get; set; }
[Inject] public required TrackDetailViewModel ViewModel { get; set; }
[Inject] public required PersistentComponentState PersistentState { get; set; }
private PersistingComponentStateSubscription _persistingSubscription;
// The entry key the ViewModel currently holds. Tracks param-only navigations
// (e.g. /track/A -> /track/B) which reuse this component instance and fire
// OnParametersSet without re-running OnInitialized — without this, the page keeps
// the prior track and Play streams the wrong audio.
private string? _loadedEntryKey;
protected override void OnInitialized()
// Carry the prerendered track across the prerender -> interactive (WASM) seam.
// Without this, the WASM pass gets a fresh scoped ViewModel, re-renders the
// skeleton, and re-fetches. Mirror the TracksView bridge: persist on the way
// out of prerender, restore on the interactive pass, and only fetch on a miss.
=> _persistingSubscription = PersistentState.RegisterOnPersisting(PersistTrack);
protected override async Task OnParametersSetAsync()
{
// Re-run whenever the route key changes. Component instances are reused across
// same-template navigations, so the load decision must live here, not in
// OnInitialized (which fires once per instance).
if (_loadedEntryKey == EntryKey) return;
// Guard the bridge on the key: a payload for a different track must not seed this
// page (stale-bridge bleed across navigation).
if (PersistentState.TryTakeFromJson<TrackDto>(PersistKey, out var restored)
&& restored is not null
&& restored.EntryKey == EntryKey)
{
ViewModel.Track = restored;
ViewModel.NotFound = false;
ViewModel.IsLoading = false;
}
else
{
await ViewModel.Load(EntryKey);
}
_loadedEntryKey = EntryKey;
}
private Task PersistTrack()
{
if (ViewModel.Track is not null)
{
PersistentState.PersistAsJson(PersistKey, ViewModel.Track);
}
return Task.CompletedTask;
}
public void Dispose() => _persistingSubscription.Dispose();
}