History

Derive image preview index during render

Cursor Agent committed 5 months ago

Commit 100a9781 parent 92604221 file · +12 −9Browse files

Unified · Split

apps/web/src/components/chat/ExpandedImageDialog.tsx+12−9
@@ -45,17 +45,20 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({ · 44 unmodified lines
4545 preview,
4646 onClose,
4747 }: ExpandedImageDialogProps) {
48− const [activeIndex, setActiveIndex] = useState(preview.index);
48+ const [navigationOffset, setNavigationOffset] = useState(0);
49+ const imageCount = preview.images.length;
50+ const activeIndex =
51+ imageCount > 0 ? (preview.index + navigationOffset + imageCount) % imageCount : preview.index;
4952
5053 const navigateImage = useCallback((direction: -1 | 1) => {
51− setActiveIndex((existingIndex) => {
52− if (preview.images.length <= 1) return existingIndex;
53− return (existingIndex + direction + preview.images.length) % preview.images.length;
54+ setNavigationOffset((offset) => {
55+ if (imageCount <= 1) return offset;
56+ return offset + direction;
5457 });
55− }, [preview.images.length]);
58+ }, [imageCount]);
5659
5760 useExpandedImageDialogKeyboardShortcuts({
58− imageCount: preview.images.length,
61+ imageCount,
5962 onClose,
6063 onNavigate: navigateImage,
6164 });
@@ -76,7 +79,7 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({ · 14 unmodified lines
7679 aria-label="Close image preview"
7780 onClick={onClose}
7881 />
79− {preview.images.length > 1 && (
82+ {imageCount > 1 && (
8083 <Button
8184 type="button"
8285 size="icon"
@@ -107,10 +110,10 @@ export const ExpandedImageDialog = memo(function ExpandedImageDialog({ · 24 unmodified lines
107110 />
108111 <p className="mt-2 max-w-[92vw] truncate text-center text-xs text-muted-foreground/80">
109112 {item.name}
110− {preview.images.length > 1 ? ` (${activeIndex + 1}/${preview.images.length})` : ""}
113+ {imageCount > 1 ? ` (${activeIndex + 1}/${imageCount})` : ""}
111114 </p>
112115 </div>
113− {preview.images.length > 1 && (
116+ {imageCount > 1 && (
114117 <Button
115118 type="button"
116119 size="icon"