From d16a77907a2873d4e42f7805095054923c95d44f Mon Sep 17 00:00:00 2001 From: JakeBreath Date: Fri, 18 Sep 2026 17:52:50 -0500 Subject: [PATCH] Keep the online detail's download state on its own post MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two stale-state bugs came from react-router reusing the detail component between posts (parent/child links hit the same /detail/ route): - the previously viewed post's download panel kept rendering, so a freshly opened post could claim 'Downloaded to the library J-xx'. The task view is now gated on the task's post_id as well, and DetailPage keys the detail views per item — component state (download panel, delete confirmation, optimizer modal) cannot survive a post change any more. - 'Your last download for this post finished' appeared on every revisit. It now only shows when this visit actually saw the download running (derived state, set during render), which still reports a re-attached download finishing while staying quiet on later visits; the 'In library' button remains the persistent indicator. Library detail gets the same per-item key, so its delete confirmation and optimizer modal reset between items too. tsc, oxlint and the build are clean (the derived-state pattern was chosen over a ref read in render / setState-in-effect, both flagged by the linter). --- frontend/src/features/detail/DetailPage.tsx | 7 +++-- frontend/src/features/online/OnlineDetail.tsx | 28 +++++++++++++++---- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/frontend/src/features/detail/DetailPage.tsx b/frontend/src/features/detail/DetailPage.tsx index ba269ea..fc772b9 100644 --- a/frontend/src/features/detail/DetailPage.tsx +++ b/frontend/src/features/detail/DetailPage.tsx @@ -14,11 +14,14 @@ export default function DetailPage() { if (J_ID_RE.test(itemId)) { if (!hasBackend()) return ; const normalized = `J-${itemId.slice(2)}`; - return ; + // Key per item: remount so local state (delete confirm, optimizer, task + // view) cannot leak from the previously viewed item. + return ; } if (/^\d+$/.test(itemId)) { - return ; + // Key per post: the download task view must not survive a post change. + return ; } return ( diff --git a/frontend/src/features/online/OnlineDetail.tsx b/frontend/src/features/online/OnlineDetail.tsx index 44c52dc..de164b5 100644 --- a/frontend/src/features/online/OnlineDetail.tsx +++ b/frontend/src/features/online/OnlineDetail.tsx @@ -121,6 +121,10 @@ export function OnlineDetail({ postId }: { postId: number }) { }); const [downloadTaskId, setDownloadTaskId] = useState(null); + // True once this visit has seen the download running. Only then is "your + // last download finished" worth showing; a revisit stays quiet (the "In + // library" button already says where the file is). + const [watchedDownload, setWatchedDownload] = useState(false); // Server-side tasks outlive this page; re-attach to the newest one for this // post so navigating away and back keeps the progress view. @@ -186,6 +190,19 @@ export function OnlineDetail({ postId }: { postId: number }) { const downloadStatus = downloadTask?.status; const downloadActive = downloadStatus === "pending" || downloadStatus === "downloading"; + // Never render another post's task — this covers the single frame before the + // remount (DetailPage keys this component per post). A null post_id (older or + // hand-made tasks) stays visible rather than hiding a real download. + const downloadTaskMatchesPost = + !downloadTask || + downloadTask.post_id === null || + downloadTask.post_id === post?.id; + + // Derived state (safe to set during render): remember that this visit saw + // the download running. + if (downloadActive && !watchedDownload) { + setWatchedDownload(true); + } useEffect(() => { if (!downloadStatus) return; @@ -340,7 +357,8 @@ export function OnlineDetail({ postId }: { postId: number }) { - {downloadTask && + {downloadTaskMatchesPost && + downloadTask && (downloadTask.status === "pending" || downloadTask.status === "downloading") ? (
@@ -372,7 +390,7 @@ export function OnlineDetail({ postId }: { postId: number }) { cancel
- ) : downloadTask?.status === "complete" ? ( + ) : downloadTaskMatchesPost && downloadTask?.status === "complete" ? (

Downloaded to the library @@ -385,13 +403,13 @@ export function OnlineDetail({ postId }: { postId: number }) { ) : null}

- ) : downloadTask?.status === "cancelled" ? ( + ) : downloadTaskMatchesPost && downloadTask?.status === "cancelled" ? (

Download cancelled.

- ) : downloadTask?.status === "error" ? ( + ) : downloadTaskMatchesPost && downloadTask?.status === "error" ? (

{downloadTask.error || "Download failed."}

- ) : remoteFinishedTask && !downloadMutation.isPending ? ( + ) : remoteFinishedTask && watchedDownload && !downloadMutation.isPending ? (

Your last download for this post finished