diff --git a/web/src/App.tsx b/web/src/App.tsx index 627cb93..c2efe0f 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -11,6 +11,7 @@ import { useRef, useState, } from "react"; +import { flushSync } from "react-dom"; import { LAYER, LiveContext, type Nav, NavContext, type TvDeckRequest, useEscape } from "./context"; import { type Download, fetchDownloads } from "./downloads"; import { type Probe, probeHealth } from "./health"; @@ -185,6 +186,7 @@ export function App() { const inputRef = useRef(null); const wordmark = useRef(null); const debounce = useRef(undefined); + const routeTicket = useRef(0); // where back and Esc land, and which control gets focus back const overlayParent = useRef(LIBRARY); const overlayOrigin = useRef(null); @@ -205,6 +207,10 @@ export function App() { const applyRoute = useCallback( async (next: Route) => { window.clearTimeout(debounce.current); + // a later route, or a click, outranks a deep-link fetch still out + routeTicket.current += 1; + const ticket = routeTicket.current; + const stale = () => ticket !== routeTicket.current; if (isBase(next)) { // ยง9.7: "/" is the library โ€” clear any stale query so the rail input // matches the route on a cold load or back-navigation @@ -223,7 +229,11 @@ export function App() { overlayOrigin.current = wordmark.current; setInput(""); if (next.kind === "movie" || next.kind === "releases") { - if ((await fetchMovie(next.movieId)) === null) { + const found = await fetchMovie(next.movieId); + if (stale()) { + return; + } + if (found === null) { home(); return; } @@ -236,7 +246,11 @@ export function App() { return; } if (next.kind === "series") { - if ((await fetchSeries(next.seriesId)) === null) { + const found = await fetchSeries(next.seriesId); + if (stale()) { + return; + } + if (found === null) { home(); return; } @@ -253,6 +267,9 @@ export function App() { let tv: TvDeckRequest; if (next.kind === "seasonReleases") { const title = (await fetchSeries(next.seriesId))?.title ?? ""; + if (stale()) { + return; + } if (title === "") { home(); return; @@ -267,6 +284,9 @@ export function App() { } else { const episode = await fetchEpisode(next.episodeId); const title = episode === null ? "" : ((await fetchSeries(episode.series_id))?.title ?? ""); + if (stale()) { + return; + } if (episode === null || title === "") { home(); return; @@ -308,6 +328,7 @@ export function App() { const nav = useMemo