Skip to content

Optimize expanded image dialog rerenders#2715

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/react-component-health-4e89
Draft

Optimize expanded image dialog rerenders#2715
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/react-component-health-4e89

Conversation

@cursor
Copy link
Copy Markdown
Contributor

@cursor cursor Bot commented May 15, 2026

What Changed

  • Removed the derived preview-state sync effect from ExpandedImageDialog.
  • Kept only local image index state in the dialog and keyed dialog mounts from the selected preview.
  • Moved the keyboard listener effect into the named useExpandedImageDialogKeyboardNavigation hook.
  • Added React Scan recordings for the same preview-switch interaction:
    • Before: docs/performance/expanded-image-dialog-react-scan-before.webm
    • After: docs/performance/expanded-image-dialog-react-scan-after.webm

Why

react-doctor flagged ExpandedImageDialog for derived state in an effect, which caused an extra render after the parent selected a different preview. Deriving the preview from props and keeping only local navigation state removes that effect-driven rerender while preserving arrow-key/button navigation.

react-doctor result for touched component:

  • Before: src/components/chat/ExpandedImageDialog.tsx:no-derived-state-effect
  • After: cleared

UI Changes

No intended visual change. The committed before/after React Scan recordings above show the preview switch interaction with React Scan enabled.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes
  • I included a video for animation/interaction changes

Validation:

  • bun fmt
  • bun lint
  • bun typecheck
  • bunx --bun react-doctor --json --full --offline --fail-on none /workspace
Open in Web View Automation 

Note

Reduce rerenders in ExpandedImageDialog by tracking image index instead of full preview state

  • Replaces full preview prop mirroring in ExpandedImageDialog with a single imageIndex state, clamped via a new clampImageIndex helper to handle out-of-range or zero-count cases.
  • Extracts keyboard navigation (Escape, ArrowLeft, ArrowRight) into a useExpandedImageDialogKeyboardNavigation hook in ExpandedImageDialog.tsx.
  • Adds getExpandedImagePreviewKey in ExpandedImagePreview.tsx and uses it as a React key in ChatView.tsx, so the dialog unmounts/remounts on image selection changes instead of syncing via an effect.
  • Behavioral Change: the dialog now resets fully (unmounts) when switching images rather than updating in place, and initial out-of-range indices are clamped to a valid position.

Macroscope summarized d9380cd.

Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
@github-actions github-actions Bot added size:M 30-99 changed lines (additions + deletions). vouch:trusted PR author is trusted by repo permissions or the VOUCHED list. labels May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 changed lines (additions + deletions). vouch:trusted PR author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant