From 63c316fc4b26d626be7c716c846801516bdebf42 Mon Sep 17 00:00:00 2001 From: Richard R Date: Wed, 21 Jan 2026 13:32:04 -0700 Subject: [PATCH] fix(pdfviewer): improve sentence highlight cleanup when current sentence is null Handle case where currentSentence is null by canceling retry loops and clearing stale highlights to prevent lingering highlights. Remove redundant check inside the highlight function. --- src/components/PDFViewer.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/components/PDFViewer.tsx b/src/components/PDFViewer.tsx index 6668299..cfcd958 100644 --- a/src/components/PDFViewer.tsx +++ b/src/components/PDFViewer.tsx @@ -96,6 +96,14 @@ export function PDFViewer({ zoomLevel }: PDFViewerProps) { clearSentenceHighlightTimeouts(); + if (!currentSentence) { + // Cancel any in-flight retry loops and ensure stale highlights don't remain + // when the current sentence becomes null/undefined. + sentenceHighlightSeqRef.current += 1; + clearHighlights(); + return; + } + const seq = ++sentenceHighlightSeqRef.current; const isLayoutChange = layoutKey !== lastSentenceLayoutKeyRef.current; lastSentenceLayoutKeyRef.current = layoutKey; @@ -108,7 +116,6 @@ export function PDFViewer({ zoomLevel }: PDFViewerProps) { if (seq !== sentenceHighlightSeqRef.current) return; const container = containerRef.current; if (!container) return; - if (!currentSentence) return; const spans = container.querySelectorAll('.react-pdf__Page__textContent span'); if (!spans.length) {