Skip to content

Add React.memo to ChunkCard in source detail panel #1012

@MODSetter

Description

@MODSetter

Description

ChunkCard in source-detail-panel.tsx renders a MarkdownViewer (which uses Streamdown + KaTeX) for each document chunk. When activeChunkIndex or scroll state changes, all chunk cards re-render even though their markdown content hasn't changed. Since MarkdownViewer is expensive, memoizing ChunkCard can significantly reduce wasted work.

Vercel React Best Practices Rule: rerender-memo (5.5)

File to change

  • surfsense_web/components/new-chat/source-detail-panel.tsx

What to do

  1. Add memo to the React import (alongside the existing forwardRef).

  2. Wrap the existing forwardRef component in memo (lines 58-105):

// Before
const ChunkCard = forwardRef<HTMLDivElement, ChunkCardProps>(
  ({ chunk, index, totalChunks, isCited }, ref) => {
    return ( ... );
  }
);

// After
const ChunkCard = memo(forwardRef<HTMLDivElement, ChunkCardProps>(
  ({ chunk, index, totalChunks, isCited }, ref) => {
    return ( ... );
  }
));

Acceptance criteria

  • ChunkCard is wrapped in memo
  • Cited chunk highlighting still works
  • Scrolling to cited chunk still works
  • Non-active chunks don't re-render when active chunk changes (verifiable in React DevTools Profiler)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions