Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(chat): an image preview's zoom resets whenever the shown image changes

Absterrg0ad06b4cd40ef
diff --git a/src/components/AttachmentPreview.test.ts b/src/components/AttachmentPreview.test.ts
index 97e950e3bb..e9ff7b17b1 100644
--- a/src/components/AttachmentPreview.test.ts
+++ b/src/components/AttachmentPreview.test.ts
@@ -418,6 +418,15 @@ describe("image preview wheel", () => {
expect(picture().alt).toBe("next.png");
expect(picture().style.transform).toBe("");
+ await wheel(-200);
+ expect(picture().style.transform).not.toBe("");
+ const replaced = [images[0]!, { src: "/api/files/replaced.png", name: "replaced.png" }];
+ await act(async () => {
+ root.render(createElement("div", { onWheel: chatWheel }, createElement(AttachmentPreviewDialog, { image: replaced[0]!, images: replaced, onClose: () => {} })));
+ });
+ expect(picture().alt).toBe("replaced.png");
+ expect(picture().style.transform).toBe("");
+
expect(chatWheel).not.toHaveBeenCalled();
} finally {
act(() => root.unmount());
diff --git a/src/components/AttachmentPreview.tsx b/src/components/AttachmentPreview.tsx
index 49e9ddac16..2208254fb1 100644
--- a/src/components/AttachmentPreview.tsx
+++ b/src/components/AttachmentPreview.tsx
@@ -463,6 +463,7 @@ export function AttachmentPreviewDialog({
const [failedSource, setFailedSource] = useState<string | null>(null);
const [attempt, setAttempt] = useState(0);
const zoom = usePreviewZoom();
+ useLayoutEffect(() => zoom.reset(), [current.src, zoom.reset]);
useLayoutEffect(() => {
closeRef.current = onClose;
@@ -471,8 +472,7 @@ export function AttachmentPreviewDialog({
const navigate = useCallback((step: -1 | 1) => {
setIndex((value) => wrappedImageIndex(value, step, items.length));
setFailedSource(null);
- zoom.reset();
- }, [items.length, zoom.reset]);
+ }, [items.length]);
useEffect(() => {
const previousFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null;