Skip to content

Reset expanded state when chunkId changes in CitationPanelContent #1367

@MODSetter

Description

@MODSetter

Problem

surfsense_web/components/citation-panel/citation-panel.tsx has stale local state when the user opens a different citation without closing the panel.

// Lines 31–35
export const CitationPanelContent: FC<CitationPanelContentProps> = ({ chunkId, onClose }) => {
    const openEditorPanel = useSetAtom(openEditorPanelAtom);
    const [expanded, setExpanded] = useState(false);

    useEffect(() => {
        setExpanded(false);
    }, []); // <-- empty deps; runs only on mount

This component is rendered conditionally inside RightPanel.tsx:251:

{effectiveTab === "citation" && citationOpen && citationState.chunkId != null && (
    <CitationPanelContent chunkId={citationState.chunkId} onClose={closeCitation} />
)}

The panel is not keyed on chunkId, so when the user clicks a different citation in the chat, the same component instance is reused with a new chunkId prop — React does not remount it. The reset effect with [] deps never fires again.

Repro:

  1. Click citation "[1]" → panel opens with default 5-chunk window.
  2. Click "More context" → expanded=true, fetches 50-chunk window for chunk 1.
  3. Click citation "[2]" in the chat.
  4. Panel now shows the 50-chunk window for chunk 2 — the user did not request this.

Files

  • surfsense_web/components/citation-panel/citation-panel.tsx (lines 33–35)

What to do

Change the effect deps so it actually resets when the citation changes:

- useEffect(() => {
-     setExpanded(false);
- }, []);
+ useEffect(() => {
+     setExpanded(false);
+ }, [chunkId]);

That's the entire fix. The useQuery already has chunkId and chunkWindow in its queryKey (line 40), so it'll correctly refetch the default window when expanded flips back to false.

Alternative (also acceptable): key the rendered component on chunkId in RightPanel.tsx:

<CitationPanelContent key={citationState.chunkId} chunkId={citationState.chunkId} onClose={closeCitation} />

This remounts on chunk change, getting the same effect. The useEffect fix is more local and preferred.

Why this matters

  • Real UX bug: opening a new citation should not show a 50-chunk window the user never asked for.
  • Removes a hidden invariant from CitationPanelContent's interface: callers no longer need to know "remount me when chunkId changes."
  • One-line fix.

Acceptance criteria

  • useEffect deps updated to [chunkId]
  • Manually verified the repro above no longer shows expanded window for the second citation
  • No regression in the "More context" toggle when staying on the same citation

Difficulty

Good first issue — 1-line change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions