Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(workbench): harden image context menu targets
joshistoastc7b8ba6e84d5
diff --git a/invokeai/frontend/webv2/src/workbench/components/ImageContextMenu.test.ts b/invokeai/frontend/webv2/src/workbench/components/ImageContextMenu.test.tsnew file mode 100644index 0000000000..be660b44c7--- /dev/null+++ b/invokeai/frontend/webv2/src/workbench/components/ImageContextMenu.test.ts@@ -0,0 +1,47 @@+import type { GalleryImage } from '@workbench/gallery/api';++import { describe, expect, it } from 'vitest';++import { getImageContextMenuImages, getImageContextMenuRecallRequestKey } from './ImageContextMenu';++const image: GalleryImage = {+ boardId: 'none',+ height: 512,+ imageCategory: 'general',+ imageName: 'image.png',+ imageUrl: '/image.png',+ queuedAt: '2026-06-15T00:00:00Z',+ sourceQueueItemId: 'queue-item',+ starred: false,+ thumbnailUrl: '/thumb.png',+ width: 512,+};++describe('getImageContextMenuImages', () => {+ it('returns only usable images from a context menu target', () => {+ expect(getImageContextMenuImages({ images: [image], x: 0, y: 0 })).toEqual([image]);+ expect(+ getImageContextMenuImages({ images: [image, { imageName: 'missing-urls.png' } as GalleryImage], x: 0, y: 0 })+ ).toEqual([image]);+ });++ it('tolerates stale or malformed targets', () => {+ expect(getImageContextMenuImages(null)).toEqual([]);+ expect(getImageContextMenuImages({ images: undefined as unknown as GalleryImage[], x: 0, y: 0 })).toEqual([]);+ });+});++describe('getImageContextMenuRecallRequestKey', () => {+ it('uses stable image primitives instead of object identity', () => {+ const refreshedImage = { ...image };++ expect(getImageContextMenuRecallRequestKey(image, false)).toBe(+ getImageContextMenuRecallRequestKey(refreshedImage, false)+ );+ });++ it('does not request single-image recall for empty or bulk menu targets', () => {+ expect(getImageContextMenuRecallRequestKey(null, false)).toBeNull();+ expect(getImageContextMenuRecallRequestKey(image, true)).toBeNull();+ });+});diff --git a/invokeai/frontend/webv2/src/workbench/components/ImageContextMenu.tsx b/invokeai/frontend/webv2/src/workbench/components/ImageContextMenu.tsxindex 4dee686bc7..720bb1777f 100644--- a/invokeai/frontend/webv2/src/workbench/components/ImageContextMenu.tsx+++ b/invokeai/frontend/webv2/src/workbench/components/ImageContextMenu.tsx@@ -49,6 +49,37 @@ const MENU_CONTENT_PROPS = {p: '0',} as const;+const isUsableGalleryImage = (value: unknown): value is GalleryImage =>+ Boolean(value) &&+ typeof value === 'object' &&+ typeof (value as GalleryImage).imageName === 'string' &&+ typeof (value as GalleryImage).imageUrl === 'string' &&+ typeof (value as GalleryImage).thumbnailUrl === 'string';++export const getImageContextMenuImages = (target: ImageContextMenuTarget | null): GalleryImage[] => {+ if (!Array.isArray(target?.images)) {+ return [];+ }++ return target.images.filter(isUsableGalleryImage);+};++export const getImageContextMenuRecallRequestKey = (image: GalleryImage | null, isBulk: boolean): string | null => {+ if (!image || isBulk) {+ return null;+ }++ return `${image.imageName}:${image.width}:${image.height}`;+};++const areRecallCapabilitiesEqual = (left: ImageRecallCapabilities, right: ImageRecallCapabilities): boolean =>+ left.all === right.all &&+ left.clipSkip === right.clipSkip &&+ left.dimensions === right.dimensions &&+ left.prompts === right.prompts &&+ left.remix === right.remix &&+ left.seed === right.seed;+/*** Shared right-click menu for backend images, usable from any widget (gallery* grid, preview, ...). Anchored to the cursor through a virtual rect, so it@@ -72,27 +103,38 @@ export const ImageContextMenu = ({);const [isLoadingRecallCapabilities, setIsLoadingRecallCapabilities] = useState(false);const targetRef = useRef(target);+ const imageRef = useRef<GalleryImage | null>(null);targetRef.current = target;- const images = target?.images ?? [];+ const images = getImageContextMenuImages(target);const image = images[0] ?? null;const isBulk = images.length > 1;const imageNames = images.map((candidate) => candidate.imageName);const menuTarget = image ? target : null;+ const recallRequestKey = getImageContextMenuRecallRequestKey(image, isBulk);+ const getImageRecallCapabilities = actions.getImageRecallCapabilities;++ imageRef.current = image;useEffect(() => {- if (!image || isBulk) {- setRecallCapabilities(EMPTY_IMAGE_RECALL_CAPABILITIES);- setIsLoadingRecallCapabilities(false);+ if (!recallRequestKey) {+ setRecallCapabilities((current) =>+ areRecallCapabilitiesEqual(current, EMPTY_IMAGE_RECALL_CAPABILITIES) ? current : EMPTY_IMAGE_RECALL_CAPABILITIES+ );+ setIsLoadingRecallCapabilities((current) => (current ? false : current));return;}let isCancelled = false;+ const recallImage = imageRef.current;++ if (!recallImage) {+ return;+ }- setIsLoadingRecallCapabilities(true);- actions- .getImageRecallCapabilities(image)+ setIsLoadingRecallCapabilities((current) => (current ? current : true));+ getImageRecallCapabilities(recallImage).then((capabilities) => {if (!isCancelled) {setRecallCapabilities(capabilities);@@ -112,7 +154,7 @@ export const ImageContextMenu = ({return () => {isCancelled = true;};- }, [actions, image, isBulk]);+ }, [getImageRecallCapabilities, recallRequestKey]);const requestDeletion = (names: string[]) => {if (confirmImageDeletion) {diff --git a/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryImageGrid.tsx b/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryImageGrid.tsxindex f198f63bc9..1a51a6cb1a 100644--- a/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryImageGrid.tsx+++ b/invokeai/frontend/webv2/src/workbench/widgets/gallery/GalleryImageGrid.tsx@@ -138,6 +138,18 @@ export const GalleryImageGrid = ({ layout }: { layout: 'stacked' | 'wide' }) =>}}, [actions, lastVisibleRowIndex, paginationMode, rowCount]);+ useEffect(() => {+ const primaryTargetImageName = contextMenuTarget?.images[0]?.imageName;++ if (!primaryTargetImageName) {+ return;+ }++ if (!gallery.images.some((image) => image.imageName === primaryTargetImageName)) {+ setContextMenuTarget(null);+ }+ }, [contextMenuTarget, gallery.images]);+const handleThumbnailClick = (image: GalleryImage, event: MouseEvent) => {if (event.shiftKey) {const targetIndex = gallery.images.findIndex((candidate) => candidate.imageName === image.imageName);