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.
This commit is contained in:
Richard R 2026-01-21 13:32:04 -07:00
parent 5a40e4b00a
commit 63c316fc4b

View file

@ -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) {