Skip to content

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.

fix(preview): honour the compare memo only in the query it was recorded in

Lincoln Stein562b0b07b6e5
diff --git a/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.browser.test.tsx
index a13626b57b..219cdbaa57 100644
--- a/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/image-actions/useImageActions.browser.test.tsx
@@ -428,6 +428,7 @@ type ExpectedItemActions = {
loadedItems?: Array<{ fullUrl: string; kind: 'image' | 'video'; name: string }>
): Promise<void>;
moveItemsToBoard(refs: Array<{ kind: 'image' | 'video'; name: string }>, boardId: string): Promise<void>;
+ openItemInPreview(item: { fullUrl: string; kind: 'image' | 'video'; name: string }): void;
setItemsStarred(refs: Array<{ kind: 'image' | 'video'; name: string }>, starred: boolean): Promise<void>;
};
@@ -1042,6 +1043,24 @@ describe('primary successor after confirmed deletion', () => {
expect(mocks.gallerySelectItem).toHaveBeenCalledWith(before, 'project-1', 30, true);
});
+ it('opens an item in Preview at the page the host navigates from', () => {
+ const item = galleryItem('image', 'deep.png');
+
+ currentItemActionContext = {
+ filterIdentity: 'filter-a',
+ getItemSelectionPage: () => 30,
+ items: [item],
+ loadOrderedRefs: () => Promise.resolve([{ kind: 'image' as const, name: item.name }]),
+ selectedItemKey: 'image:deep.png',
+ };
+
+ act(() => {
+ getItemActions().openItemInPreview(item);
+ });
+
+ expect(mocks.gallerySelectItem).toHaveBeenCalledWith(item, 'project-1', 30, true);
+ });
+
it('carries the host page into a retained multi-selection after a partial failure', async () => {
const successor = galleryItem('image', 'successor.png');
const primary = galleryItem('image', 'primary.png');
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewNavigation.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewNavigation.browser.test.tsx
index c4526342a1..6e1d0ab747 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewNavigation.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewNavigation.browser.test.tsx
@@ -820,6 +820,114 @@ describe('preview keyboard navigation boundary', () => {
);
});
+ it('does not restore a remembered page into a different listing', async () => {
+ // Between the swap and the swap back the user moved to another board.
+ // The remembered page named a window of the first board's listing;
+ // stamped into the second board's query it would anchor that listing
+ // 1800 rows down around an image that is not in it.
+ setGalleryValues({
+ compareImage: legacyImage('compare-top', '2026-07-24T00:00:00.000Z'),
+ galleryPage: 0,
+ recentImages: [],
+ selectedImage: legacyImage('deep-newer', '2026-07-20T00:00:02.000Z'),
+ selectedImageName: 'deep-newer',
+ selectedImageQuery: deepQuery,
+ });
+ mocks.galleryItemPages = deepBoardPages([createImageItem('deep-newer', '2026-07-20T00:00:02.000Z')]);
+ mocks.galleryItemPages[0] = { items: [createImageItem('compare-top', '2026-07-24T00:00:00.000Z')], total: 2 };
+
+ await render();
+
+ const swap = () =>
+ act(async () => {
+ registeredCommands.get('viewer.swapImages')?.();
+ await Promise.resolve();
+ });
+
+ await swap();
+ await commitLastSelection();
+ // The grid: another board, a click on one of its images. The compare slot
+ // survives that, still holding the deep image from the first board.
+ setGalleryValues({
+ compareImage: legacyImage('deep-newer', '2026-07-20T00:00:02.000Z'),
+ selectedImage: { ...legacyImage('other-board-image', '2026-07-25T00:00:00.000Z'), boardId: 'board-b' },
+ selectedImageName: 'other-board-image',
+ selectedImageQuery: { ...deepQuery, boardId: 'board-b', page: 0 },
+ });
+ await rerender();
+ await swap();
+
+ expect(mocks.commands.gallery.selectItem).toHaveBeenLastCalledWith(
+ expect.objectContaining({ kind: 'image', name: 'deep-newer' }),
+ undefined,
+ 0,
+ true
+ );
+ });
+
+ it('only restores a remembered page for the item it was remembered for', async () => {
+ setGalleryValues({
+ compareImage: legacyImage('compare-top', '2026-07-24T00:00:00.000Z'),
+ galleryPage: 0,
+ recentImages: [],
+ selectedImage: legacyImage('deep-newer', '2026-07-20T00:00:02.000Z'),
+ selectedImageName: 'deep-newer',
+ selectedImageQuery: deepQuery,
+ });
+ mocks.galleryItemPages = deepBoardPages([createImageItem('deep-newer', '2026-07-20T00:00:02.000Z')]);
+ mocks.galleryItemPages[0] = { items: [createImageItem('compare-top', '2026-07-24T00:00:00.000Z')], total: 2 };
+
+ await render();
+
+ const swap = () =>
+ act(async () => {
+ registeredCommands.get('viewer.swapImages')?.();
+ await Promise.resolve();
+ });
+
+ await swap();
+ await commitLastSelection();
+ // A different image lands in the compare slot before the swap back.
+ setGalleryValues({ compareImage: legacyImage('another-top', '2026-07-24T00:00:01.000Z') });
+ await rerender();
+ await swap();
+
+ expect(mocks.commands.gallery.selectItem).toHaveBeenLastCalledWith(
+ expect.objectContaining({ kind: 'image', name: 'another-top' }),
+ undefined,
+ 0,
+ true
+ );
+ });
+
+ it('hands image actions the window anchor for an item on a later page of the window', async () => {
+ // The stamp is the anchor of the window holding the item, not the page
+ // the item happens to sit on: a successor from page 31 of a window
+ // anchored at page 30 is stamped 30.
+ const deepA = createImageItem('deep-a', '2026-07-20T00:00:04.000Z');
+ const deepB = createImageItem('deep-b', '2026-07-20T00:00:03.000Z');
+ const deepC = createImageItem('deep-c', '2026-07-20T00:00:02.000Z');
+
+ setGalleryValues({
+ galleryPage: 0,
+ recentImages: [],
+ selectedImage: legacyImage('deep-b', '2026-07-20T00:00:03.000Z'),
+ selectedImageName: 'deep-b',
+ selectedImageQuery: deepQuery,
+ });
+ mocks.galleryItemPages = deepBoardPages([deepA, deepB], [deepC]);
+
+ await render();
+ // Cross the boundary so page 31 is part of the window.
+ await pressArrow('ArrowRight');
+ await commitLastSelection();
+
+ const context = mocks.imageActionOptions?.getItemActionContext?.();
+
+ expect(context?.items.map((item) => item.name)).toContain('deep-c');
+ expect(context?.getItemSelectionPage?.(deepC)).toBe(30);
+ });
+
it('fetches the next infinite page before stepping past the loaded backend boundary', async () => {
const newest: GalleryImage = {
...mocks.recentImages[0],
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
index 30cb102045..18cd11092f 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
@@ -332,22 +332,41 @@ export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
// swapping a top-of-board image in moves the window to the top — so a swap
// back could only guess at its page. This is not a guess: it is the window
// the item was navigated in, and restoring it puts the arrows back where
- // they were before the first swap.
- const swappedOutRef = useRef<{ key: GalleryItemKey; page: number } | null>(null);
+ // they were before the first swap. A page names a window of ONE query,
+ // though: restored into a different board, view, order, mode or search it
+ // would anchor that listing 1800 rows down around an image from another,
+ // so the memo is only honoured in the query it was recorded in.
+ const swappedOutRef = useRef<{ key: GalleryItemKey; page: number; queryKey: string } | null>(null);
const swapCompareImages = useCallback(() => {
if (selectedItem?.kind === 'image' && compareImage) {
const compareItem = legacyGeneratedImageToGalleryItem(compareImage);
const swappedOut = swappedOutRef.current;
- swappedOutRef.current = { key: toGalleryItemKey(selectedItem), page: selectedImageQuery.page };
- if (swappedOut && swappedOut.key === toGalleryItemKey(compareItem)) {
+ swappedOutRef.current = {
+ key: toGalleryItemKey(selectedItem),
+ page: selectedImageQuery.page,
+ queryKey: navigationQueryKey,
+ };
+ if (
+ swappedOut &&
+ swappedOut.key === toGalleryItemKey(compareItem) &&
+ swappedOut.queryKey === navigationQueryKey
+ ) {
selectGalleryItemAtPage(compareItem, swappedOut.page);
} else {
selectPreviewItem(compareItem);
}
gallery.setCompareItem(selectedItem);
}
- }, [compareImage, gallery, selectGalleryItemAtPage, selectPreviewItem, selectedImageQuery.page, selectedItem]);
+ }, [
+ compareImage,
+ gallery,
+ navigationQueryKey,
+ selectGalleryItemAtPage,
+ selectPreviewItem,
+ selectedImageQuery.page,
+ selectedItem,
+ ]);
const isItemCurrent = useCallback(
(itemKey: GalleryItemKey) => {
const currentValues = getProjectWidgetValues(queries.getSnapshot().activeProject, 'gallery');
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
index db8a2c83a4..d72b641be3 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
@@ -3510,16 +3510,20 @@ describe('workbenchReducer Phase 5 generation flow', () => {
it('stamps an explicit page into the navigation query already on a multi-selection', () => {
// A host navigating its own window passes the page that keeps the primary
// item in that window — the same contract as selectGalleryItem with
- // preserveNavigationQuery — rather than the grid's page.
+ // preserveNavigationQuery — rather than the grid's page. The query it goes
+ // into is the one already on the selection: the grid may have moved to
+ // another board and search since, and the host's list is not that.
let state = createInitialWorkbenchState();
- state = workbenchReducer(state, { page: 0, type: 'setGalleryPage' });
+ state = workbenchReducer(state, { boardId: 'board-deep', type: 'selectGalleryBoard' });
state = workbenchReducer(state, {
item: createGalleryImageItem('deep.png'),
preserveNavigationQuery: false,
selectionPage: 30,
type: 'selectGalleryItem',
});
+ state = workbenchReducer(state, { boardId: 'board-elsewhere', type: 'selectGalleryBoard' });
+ state = workbenchReducer(state, { searchTerm: 'sunset', type: 'setGallerySearchTerm' });
state = workbenchReducer(state, {
itemKeys: ['image:failed.png', 'image:successor.png'],
primaryItem: createGalleryImageItem('successor.png'),
@@ -3528,9 +3532,12 @@ describe('workbenchReducer Phase 5 generation flow', () => {
});
const values = getProjectWidgetValues(getActiveProject(state), 'gallery');
+ const query = values.selectedImageQuery as { boardId: string; page: number; searchTerm: string };
expect(values.selectedImagePage).toBe(30);
- expect((values.selectedImageQuery as { page: number }).page).toBe(30);
+ expect(query.page).toBe(30);
+ expect(query.boardId).toBe('board-deep');
+ expect(query.searchTerm).toBe('');
expect(values.galleryPage).toBe(0);
});