Compare commits

..
Author SHA1 Message Date
Cursor AgentandJulius Marminge d9380cdecb Optimize expanded image dialog rerenders
Co-authored-by: Julius Marminge <[email protected]>
2026-05-15 16:09:12 +00:00
7 changed files with 40 additions and 35 deletions

No files matched your search

+2 -5
View File
@@ -147,10 +147,7 @@ import { ExpandedImageDialog } from "./chat/ExpandedImageDialog";
import { PullRequestThreadDialog } from "./PullRequestThreadDialog";
import { MessagesTimeline } from "./chat/MessagesTimeline";
import { ChatHeader } from "./chat/ChatHeader";
import {
getExpandedImagePreviewIdentityKey,
type ExpandedImagePreview,
} from "./chat/ExpandedImagePreview";
import { getExpandedImagePreviewKey, type ExpandedImagePreview } from "./chat/ExpandedImagePreview";
import { NoActiveThreadState } from "./NoActiveThreadState";
import { resolveEffectiveEnvMode, resolveEnvironmentOptionLabel } from "./BranchToolbar.logic";
import { ProviderStatusBanner } from "./chat/ProviderStatusBanner";
@@ -3777,7 +3774,7 @@ export default function ChatView(props: ChatViewProps) {
{expandedImage && (
<ExpandedImageDialog
key={getExpandedImagePreviewIdentityKey(expandedImage)}
key={getExpandedImagePreviewKey(expandedImage)}
preview={expandedImage}
onClose={closeExpandedImage}
/>
@@ -1,4 +1,4 @@
import { memo, useCallback, useEffect, useEffectEvent, useState } from "react";
import { memo, useCallback, useEffect, useState } from "react";
import { ChevronLeftIcon, ChevronRightIcon, XIcon } from "lucide-react";
import { Button } from "../ui/button";
import type { ExpandedImagePreview } from "./ExpandedImagePreview";
@@ -8,65 +8,76 @@ interface ExpandedImageDialogProps {
onClose: () => void;
}
function useExpandedImageDialogKeyboardShortcuts(input: {
readonly imageCount: number;
readonly onClose: () => void;
readonly onNavigate: (direction: -1 | 1) => void;
}) {
const close = useEffectEvent(input.onClose);
const navigate = useEffectEvent(input.onNavigate);
function clampImageIndex(index: number, imageCount: number): number {
if (imageCount <= 0) return 0;
return Math.min(Math.max(index, 0), imageCount - 1);
}
function useExpandedImageDialogKeyboardNavigation({
imageCount,
navigateImage,
onClose,
}: {
imageCount: number;
navigateImage: (direction: -1 | 1) => void;
onClose: () => void;
}) {
useEffect(() => {
const onKeyDown = (event: globalThis.KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
event.stopPropagation();
close();
onClose();
return;
}
if (input.imageCount <= 1) return;
if (imageCount <= 1) return;
if (event.key === "ArrowLeft") {
event.preventDefault();
event.stopPropagation();
navigate(-1);
navigateImage(-1);
return;
}
if (event.key !== "ArrowRight") return;
event.preventDefault();
event.stopPropagation();
navigate(1);
navigateImage(1);
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [close, input.imageCount, navigate]);
}, [imageCount, navigateImage, onClose]);
}
export const ExpandedImageDialog = memo(function ExpandedImageDialog({
preview,
onClose,
}: ExpandedImageDialogProps) {
const [navigationOffset, setNavigationOffset] = useState(0);
const imageCount = preview.images.length;
const activeIndex =
imageCount > 0 ? (preview.index + navigationOffset + imageCount) % imageCount : preview.index;
const [imageIndex, setImageIndex] = useState(() => clampImageIndex(preview.index, imageCount));
const activeImageIndex = clampImageIndex(imageIndex, imageCount);
const navigateImage = useCallback(
(direction: -1 | 1) => {
setNavigationOffset((offset) => {
if (imageCount <= 1) return offset;
return offset + direction;
setImageIndex((existing) => {
if (imageCount <= 1) return existing;
return (existing + direction + imageCount) % imageCount;
});
},
[imageCount],
);
const navigateToPreviousImage = useCallback(() => {
navigateImage(-1);
}, [navigateImage]);
const navigateToNextImage = useCallback(() => {
navigateImage(1);
}, [navigateImage]);
useExpandedImageDialogKeyboardShortcuts({
useExpandedImageDialogKeyboardNavigation({
imageCount,
navigateImage,
onClose,
onNavigate: navigateImage,
});
const item = preview.images[activeIndex];
const item = preview.images[activeImageIndex];
if (!item) return null;
return (
@@ -89,7 +100,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
variant="ghost"
className="absolute left-2 top-1/2 z-20 -translate-y-1/2 text-white/90 hover:bg-white/10 hover:text-white sm:left-6"
aria-label="Previous image"
onClick={() => navigateImage(-1)}
onClick={navigateToPreviousImage}
>
<ChevronLeftIcon className="size-5" />
</Button>
@@ -113,7 +124,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
/>
<p className="mt-2 max-w-[92vw] truncate text-center text-xs text-muted-foreground/80">
{item.name}
{imageCount > 1 ? ` (${activeIndex + 1}/${imageCount})` : ""}
{imageCount > 1 ? ` (${activeImageIndex + 1}/${imageCount})` : ""}
</p>
</div>
{imageCount > 1 && (
@@ -123,7 +134,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
variant="ghost"
className="absolute right-2 top-1/2 z-20 -translate-y-1/2 text-white/90 hover:bg-white/10 hover:text-white sm:right-6"
aria-label="Next image"
onClick={() => navigateImage(1)}
onClick={navigateToNextImage}
>
<ChevronRightIcon className="size-5" />
</Button>
@@ -8,12 +8,9 @@ export interface ExpandedImagePreview {
index: number;
}
export function getExpandedImagePreviewIdentityKey(preview: ExpandedImagePreview): string {
export function getExpandedImagePreviewKey(preview: ExpandedImagePreview): string {
const selectedImage = preview.images[preview.index];
const selectedImageKey = selectedImage
? `${selectedImage.name.length}:${selectedImage.name}:${selectedImage.src.length}:${selectedImage.src}`
: "missing";
return `${preview.images.length}:${preview.index}:${selectedImageKey}`;
return `${preview.index}:${preview.images.length}:${selectedImage?.src ?? ""}`;
}
export function buildExpandedImagePreview(
Binary file not shown.
Binary file not shown.