diff --git a/web/src/screens/MediaBrowseScreen.tsx b/web/src/screens/MediaBrowseScreen.tsx index 12247b839..876b9b7cf 100644 --- a/web/src/screens/MediaBrowseScreen.tsx +++ b/web/src/screens/MediaBrowseScreen.tsx @@ -79,6 +79,7 @@ export function MediaBrowseScreen() { const [notice, setNotice] = useState(null); const activeRef = useRef(true); const seqRef = useRef(0); + const lastQueryRef = useRef(initialQuery.trim()); useEffect(() => { activeRef.current = true; @@ -88,10 +89,17 @@ export function MediaBrowseScreen() { }, []); // Debounce the per-kind free-text box; it composes straight into the browse (Lucene) query. + // The no-change guard matters: this timer also fires once on mount (and after kind changes), + // and must not reset the page or wipe an in-progress selection when the query is unchanged. useEffect(() => { const handle = window.setTimeout(() => { + const next = queryInput.trim(); + if (lastQueryRef.current === next) { + return; + } + lastQueryRef.current = next; setPageNum(0); - setQuery(queryInput.trim()); + setQuery(next); setSelected(new Map()); }, 300); @@ -123,6 +131,7 @@ export function MediaBrowseScreen() { setPageNum(0); setQueryInput(''); setQuery(''); + lastQueryRef.current = ''; setSelected(new Map()); // Keep the URL shareable/back-navigable without remounting the whole screen. window.history.replaceState(null, '', `/app/media?kind=${next.slug}`); diff --git a/web/src/screens/SearchScreen.tsx b/web/src/screens/SearchScreen.tsx index b3b28775c..027a0f8a5 100644 --- a/web/src/screens/SearchScreen.tsx +++ b/web/src/screens/SearchScreen.tsx @@ -73,6 +73,7 @@ export function SearchScreen() { const [notice, setNotice] = useState(null); const activeRef = useRef(true); const seqRef = useRef(0); + const lastQueryRef = useRef(initialQuery.trim()); const hasQuery = query.trim().length > 0; useEffect(() => { @@ -82,9 +83,15 @@ export function SearchScreen() { }; }, []); + // The no-change guard matters: this timer also fires once on mount and must not wipe an + // in-progress selection when the query is unchanged. useEffect(() => { const handle = window.setTimeout(() => { const trimmed = queryInput.trim(); + if (lastQueryRef.current === trimmed) { + return; + } + lastQueryRef.current = trimmed; setQuery(trimmed); // A new query means a new result set — drop any stale selection (mirrors MediaBrowseScreen). setSelected(new Map());