Skip to content

Clear scroll-to-citation setTimeouts on panel close/unmount in source-detail-panel #1092

@MODSetter

Description

@MODSetter

Description

In source-detail-panel.tsx, when opening the panel to a cited chunk, multiple setTimeouts are fired at staggered delays (50ms, 150ms, 300ms, 600ms, 1000ms) to scroll to the cited chunk. None of these are tracked or cleared — if the user closes the panel before all fire, setHasScrolledToCited and setActiveChunkIndex are called on a closed/unmounted panel.

File to change

  • surfsense_web/components/new-chat/source-detail-panel.tsx (lines 230-245)

Current code

const scrollAttempts = [50, 150, 300, 600, 1000];
scrollAttempts.forEach((delay) => {
  setTimeout(() => { scrollToCitedChunk(); }, delay);
});
setTimeout(() => {
  setHasScrolledToCited(true);
  setActiveChunkIndex(citedChunkIndex);
}, scrollAttempts[scrollAttempts.length - 1] + 50);

What to do

  1. Store timeout IDs in a ref: const scrollTimersRef = useRef<ReturnType<typeof setTimeout>[]>([]);
  2. Push each setTimeout return value into scrollTimersRef.current
  3. Clear all timers when open becomes false or on unmount:
useEffect(() => {
  if (!open) {
    scrollTimersRef.current.forEach(clearTimeout);
    scrollTimersRef.current = [];
  }
  return () => {
    scrollTimersRef.current.forEach(clearTimeout);
    scrollTimersRef.current = [];
  };
}, [open]);

Acceptance criteria

  • All scroll timeouts are cleared when the panel closes or unmounts
  • Scroll-to-cited-chunk still works when panel stays open

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions