Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@
import { PullRequestThreadDialog } from "./PullRequestThreadDialog";
import { MessagesTimeline } from "./chat/MessagesTimeline";
import { ChatHeader } from "./chat/ChatHeader";
import { type ExpandedImagePreview } from "./chat/ExpandedImagePreview";
import {
getExpandedImagePreviewIdentityKey,
type ExpandedImagePreview,
} from "./chat/ExpandedImagePreview";
import { NoActiveThreadState } from "./NoActiveThreadState";
import { resolveEffectiveEnvMode, resolveEnvironmentOptionLabel } from "./BranchToolbar.logic";
import { ProviderStatusBanner } from "./chat/ProviderStatusBanner";
Expand Down Expand Up @@ -1780,7 +1783,7 @@
);

const focusComposer = useCallback(() => {
composerRef.current?.focusAtEnd();

Check warning on line 1786 in apps/web/src/components/ChatView.tsx

View workflow job for this annotation

GitHub Actions / Format, Lint, Typecheck, Test, Browser Test, Build

react-hooks(exhaustive-deps)

React Hook useCallback has a missing dependency: 'composerRef.current'
}, []);
const scheduleComposerFocus = useCallback(() => {
window.requestAnimationFrame(() => {
Expand All @@ -1788,7 +1791,7 @@
});
}, [focusComposer]);
const addTerminalContextToDraft = useCallback((selection: TerminalContextSelection) => {
composerRef.current?.addTerminalContext(selection);

Check warning on line 1794 in apps/web/src/components/ChatView.tsx

View workflow job for this annotation

GitHub Actions / Format, Lint, Typecheck, Test, Browser Test, Build

react-hooks(exhaustive-deps)

React Hook useCallback has a missing dependency: 'composerRef.current'
}, []);
const setTerminalOpen = useCallback(
(open: boolean) => {
Expand Down Expand Up @@ -2467,7 +2470,7 @@
const shortcutContext = {
terminalFocus: isTerminalFocused(),
terminalOpen: Boolean(terminalState.terminalOpen),
modelPickerOpen: composerRef.current?.isModelPickerOpen() ?? false,

Check warning on line 2473 in apps/web/src/components/ChatView.tsx

View workflow job for this annotation

GitHub Actions / Format, Lint, Typecheck, Test, Browser Test, Build

react-hooks(exhaustive-deps)

React Hook useEffect has a missing dependency: 'composerRef.current'
};

const command = resolveShortcutCommand(event, keybindings, {
Expand Down Expand Up @@ -3019,7 +3022,7 @@
};
});
promptRef.current = "";
composerRef.current?.resetCursorState({ cursor: 0 });

Check warning on line 3025 in apps/web/src/components/ChatView.tsx

View workflow job for this annotation

GitHub Actions / Format, Lint, Typecheck, Test, Browser Test, Build

react-hooks(exhaustive-deps)

React Hook useCallback has a missing dependency: 'composerRef.current'
},
[activePendingProgress?.activeQuestion, activePendingUserInput],
);
Expand All @@ -3046,7 +3049,7 @@
),
},
}));
const snapshot = composerRef.current?.readSnapshot();

Check warning on line 3052 in apps/web/src/components/ChatView.tsx

View workflow job for this annotation

GitHub Actions / Format, Lint, Typecheck, Test, Browser Test, Build

react-hooks(exhaustive-deps)

React Hook useCallback has a missing dependency: 'composerRef.current'
if (
snapshot?.value !== value ||
snapshot.cursor !== nextCursor ||
Expand Down Expand Up @@ -3109,7 +3112,7 @@
return;
}

const sendCtx = composerRef.current?.getSendContext();

Check warning on line 3115 in apps/web/src/components/ChatView.tsx

View workflow job for this annotation

GitHub Actions / Format, Lint, Typecheck, Test, Browser Test, Build

react-hooks(exhaustive-deps)

React Hook useCallback has a missing dependency: 'composerRef.current'
if (!sendCtx) {
return;
}
Expand Down Expand Up @@ -3246,7 +3249,7 @@
return;
}

const sendCtx = composerRef.current?.getSendContext();

Check warning on line 3252 in apps/web/src/components/ChatView.tsx

View workflow job for this annotation

GitHub Actions / Format, Lint, Typecheck, Test, Browser Test, Build

react-hooks(exhaustive-deps)

React Hook useCallback has a missing dependency: 'composerRef.current'
if (!sendCtx) {
return;
}
Expand Down Expand Up @@ -3773,7 +3776,11 @@
) : null}

{expandedImage && (
<ExpandedImageDialog preview={expandedImage} onClose={closeExpandedImage} />
<ExpandedImageDialog
key={getExpandedImagePreviewIdentityKey(expandedImage)}
preview={expandedImage}
onClose={closeExpandedImage}
/>
)}
</div>
);
Expand Down
73 changes: 43 additions & 30 deletions apps/web/src/components/chat/ExpandedImageDialog.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -8,52 +8,65 @@
onClose: () => void;
}

export const ExpandedImageDialog = memo(function ExpandedImageDialog({
preview: initialPreview,
onClose,
}: ExpandedImageDialogProps) {
const [preview, setPreview] = useState(initialPreview);

// Sync when the parent hands us a new preview reference.
useEffect(() => {
setPreview(initialPreview);
}, [initialPreview]);

const navigateImage = useCallback((direction: -1 | 1) => {
setPreview((existing) => {
if (existing.images.length <= 1) return existing;
const nextIndex =
(existing.index + direction + existing.images.length) % existing.images.length;
if (nextIndex === existing.index) return existing;
return { ...existing, index: nextIndex };
});
}, []);
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);

useEffect(() => {
const onKeyDown = (event: globalThis.KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
event.stopPropagation();
onClose();
close();
return;
}
if (preview.images.length <= 1) return;
if (input.imageCount <= 1) return;
if (event.key === "ArrowLeft") {
event.preventDefault();
event.stopPropagation();
navigateImage(-1);
navigate(-1);
return;
}
if (event.key !== "ArrowRight") return;
event.preventDefault();
event.stopPropagation();
navigateImage(1);
navigate(1);
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [navigateImage, onClose, preview.images.length]);
}, [close, input.imageCount, navigate]);

Check warning on line 41 in apps/web/src/components/chat/ExpandedImageDialog.tsx

View workflow job for this annotation

GitHub Actions / Format, Lint, Typecheck, Test, Browser Test, Build

react-hooks(exhaustive-deps)

Functions returned from `useEffectEvent` must not be included in the dependency array.
}

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;
Comment on lines +50 to +51
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Critical chat/ExpandedImageDialog.tsx:50

When navigationOffset is negative and larger than imageCount, the modulo calculation (preview.index + navigationOffset + imageCount) % imageCount produces a negative activeIndex. For example, with imageCount = 3, preview.index = 0, and navigationOffset = -5, the result is -2, causing preview.images[-2] to be undefined and the component to return null unexpectedly.

-  const activeIndex =
-    imageCount > 0 ? (preview.index + navigationOffset + imageCount) % imageCount : preview.index;
+  const activeIndex =
+    imageCount > 0 ? (((preview.index + navigationOffset) % imageCount) + imageCount) % imageCount : preview.index;
🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
In file apps/web/src/components/chat/ExpandedImageDialog.tsx around lines 50-51:

When `navigationOffset` is negative and larger than `imageCount`, the modulo calculation `(preview.index + navigationOffset + imageCount) % imageCount` produces a negative `activeIndex`. For example, with `imageCount = 3`, `preview.index = 0`, and `navigationOffset = -5`, the result is `-2`, causing `preview.images[-2]` to be `undefined` and the component to return `null` unexpectedly.

Evidence trail:
apps/web/src/components/chat/ExpandedImageDialog.tsx lines 48-70 (REVIEWED_COMMIT): `navigationOffset` state starts at 0 and is incremented/decremented without clamping (lines 53-61). The activeIndex calculation at line 50-51 adds only one `imageCount` before applying JS `%` remainder. JavaScript `%` preserves sign of dividend: `(-2) % 3 === -2`. With sufficient left-arrow presses, activeIndex becomes negative, `preview.images[negativeIndex]` is undefined, and line 70 returns null.


const navigateImage = useCallback(
(direction: -1 | 1) => {
setNavigationOffset((offset) => {
if (imageCount <= 1) return offset;
return offset + direction;
});
},
[imageCount],
);

useExpandedImageDialogKeyboardShortcuts({
imageCount,
onClose,
onNavigate: navigateImage,
});

const item = preview.images[preview.index];
const item = preview.images[activeIndex];
if (!item) return null;

return (
Expand All @@ -69,7 +82,7 @@
aria-label="Close image preview"
onClick={onClose}
/>
{preview.images.length > 1 && (
{imageCount > 1 && (
<Button
type="button"
size="icon"
Expand Down Expand Up @@ -100,10 +113,10 @@
/>
<p className="mt-2 max-w-[92vw] truncate text-center text-xs text-muted-foreground/80">
{item.name}
{preview.images.length > 1 ? ` (${preview.index + 1}/${preview.images.length})` : ""}
{imageCount > 1 ? ` (${activeIndex + 1}/${imageCount})` : ""}
</p>
</div>
{preview.images.length > 1 && (
{imageCount > 1 && (
<Button
type="button"
size="icon"
Expand Down
8 changes: 8 additions & 0 deletions apps/web/src/components/chat/ExpandedImagePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export interface ExpandedImagePreview {
index: number;
}

export function getExpandedImagePreviewIdentityKey(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}`;
}

export function buildExpandedImagePreview(
images: ReadonlyArray<{ id: string; name: string; previewUrl?: string }>,
selectedImageId: string,
Expand Down
Binary file not shown.
Binary file not shown.
Loading