Compare commits

..
Author SHA1 Message Date
Cursor AgentandJulius Marminge 43501dcb3e Add expanded image React Scan recordings
Co-authored-by: Julius Marminge <[email protected]>
2026-05-17 16:11:07 +00:00
Cursor AgentandJulius Marminge 0e1d87f791 Stabilize expanded image keyboard listener
Co-authored-by: Julius Marminge <[email protected]>
2026-05-17 16:07:44 +00:00
Cursor AgentandJulius Marminge 19adca2085 Avoid derived image dialog rerenders
Co-authored-by: Julius Marminge <[email protected]>
2026-05-17 16:06:45 +00:00
7 changed files with 64 additions and 67 deletions

No files matched your search

+2 -6
View File
@@ -147,7 +147,7 @@ import { ExpandedImageDialog } from "./chat/ExpandedImageDialog";
import { PullRequestThreadDialog } from "./PullRequestThreadDialog";
import { MessagesTimeline } from "./chat/MessagesTimeline";
import { ChatHeader } from "./chat/ChatHeader";
import { getExpandedImagePreviewKey, type ExpandedImagePreview } from "./chat/ExpandedImagePreview";
import { type ExpandedImagePreview } from "./chat/ExpandedImagePreview";
import { NoActiveThreadState } from "./NoActiveThreadState";
import { resolveEffectiveEnvMode, resolveEnvironmentOptionLabel } from "./BranchToolbar.logic";
import { ProviderStatusBanner } from "./chat/ProviderStatusBanner";
@@ -3773,11 +3773,7 @@ export default function ChatView(props: ChatViewProps) {
) : null}
{expandedImage && (
<ExpandedImageDialog
key={getExpandedImagePreviewKey(expandedImage)}
preview={expandedImage}
onClose={closeExpandedImage}
/>
<ExpandedImageDialog preview={expandedImage} onClose={closeExpandedImage} />
)}
</div>
);
@@ -1,4 +1,4 @@
import { memo, useCallback, useEffect, useState } from "react";
import { memo, useCallback, useEffect, useEffectEvent, useState } from "react";
import { ChevronLeftIcon, ChevronRightIcon, XIcon } from "lucide-react";
import { Button } from "../ui/button";
import type { ExpandedImagePreview } from "./ExpandedImagePreview";
@@ -8,76 +8,82 @@ interface ExpandedImageDialogProps {
onClose: () => void;
}
function clampImageIndex(index: number, imageCount: number): number {
if (imageCount <= 0) return 0;
return Math.min(Math.max(index, 0), imageCount - 1);
function useExpandedImagePreviewNavigation(sourcePreview: ExpandedImagePreview) {
const [navigationState, setNavigationState] = useState(() => ({
sourcePreview,
index: sourcePreview.index,
}));
const index =
navigationState.sourcePreview === sourcePreview ? navigationState.index : sourcePreview.index;
const navigateImage = useCallback(
(direction: -1 | 1) => {
setNavigationState((existing) => {
if (sourcePreview.images.length <= 1) return existing;
const currentIndex =
existing.sourcePreview === sourcePreview ? existing.index : sourcePreview.index;
const nextIndex =
(currentIndex + direction + sourcePreview.images.length) % sourcePreview.images.length;
if (nextIndex === currentIndex && existing.sourcePreview === sourcePreview) {
return existing;
}
return { sourcePreview, index: nextIndex };
});
},
[sourcePreview],
);
return {
images: sourcePreview.images,
index,
navigateImage,
};
}
function useExpandedImageDialogKeyboardNavigation({
imageCount,
navigateImage,
onClose,
}: {
function useExpandedImageKeyboardShortcuts(input: {
imageCount: number;
navigateImage: (direction: -1 | 1) => void;
onClose: () => void;
}) {
useEffect(() => {
const onKeyDown = (event: globalThis.KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
event.stopPropagation();
onClose();
return;
}
if (imageCount <= 1) return;
if (event.key === "ArrowLeft") {
event.preventDefault();
event.stopPropagation();
navigateImage(-1);
return;
}
if (event.key !== "ArrowRight") return;
const onKeyDown = useEffectEvent((event: globalThis.KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
event.stopPropagation();
navigateImage(1);
};
input.onClose();
return;
}
if (input.imageCount <= 1) return;
if (event.key === "ArrowLeft") {
event.preventDefault();
event.stopPropagation();
input.navigateImage(-1);
return;
}
if (event.key !== "ArrowRight") return;
event.preventDefault();
event.stopPropagation();
input.navigateImage(1);
});
useEffect(() => {
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [imageCount, navigateImage, onClose]);
}, []);
}
export const ExpandedImageDialog = memo(function ExpandedImageDialog({
preview,
preview: initialPreview,
onClose,
}: ExpandedImageDialogProps) {
const imageCount = preview.images.length;
const [imageIndex, setImageIndex] = useState(() => clampImageIndex(preview.index, imageCount));
const activeImageIndex = clampImageIndex(imageIndex, imageCount);
const { images, index, navigateImage } = useExpandedImagePreviewNavigation(initialPreview);
const navigateImage = useCallback(
(direction: -1 | 1) => {
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]);
useExpandedImageDialogKeyboardNavigation({
imageCount,
useExpandedImageKeyboardShortcuts({
imageCount: images.length,
navigateImage,
onClose,
});
const item = preview.images[activeImageIndex];
const item = images[index];
if (!item) return null;
return (
@@ -93,14 +99,14 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({
aria-label="Close image preview"
onClick={onClose}
/>
{imageCount > 1 && (
{images.length > 1 && (
<Button
type="button"
size="icon"
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={navigateToPreviousImage}
onClick={() => navigateImage(-1)}
>
<ChevronLeftIcon className="size-5" />
</Button>
@@ -124,17 +130,17 @@ 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 ? ` (${activeImageIndex + 1}/${imageCount})` : ""}
{images.length > 1 ? ` (${index + 1}/${images.length})` : ""}
</p>
</div>
{imageCount > 1 && (
{images.length > 1 && (
<Button
type="button"
size="icon"
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={navigateToNextImage}
onClick={() => navigateImage(1)}
>
<ChevronRightIcon className="size-5" />
</Button>
@@ -8,11 +8,6 @@ export interface ExpandedImagePreview {
index: number;
}
export function getExpandedImagePreviewKey(preview: ExpandedImagePreview): string {
const selectedImage = preview.images[preview.index];
return `${preview.index}:${preview.images.length}:${selectedImage?.src ?? ""}`;
}
export function buildExpandedImagePreview(
images: ReadonlyArray<{ id: string; name: string; previewUrl?: string }>,
selectedImageId: string,
Binary file not shown.