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(gallery): follow preview navigation across paginated pages

Josh Corbett1c55aebe293b
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryBoardsPanel.browser.test.tsx b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryBoardsPanel.browser.test.tsx
index 2d52cd930c..1a00c01f0d 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryBoardsPanel.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryBoardsPanel.browser.test.tsx
@@ -76,8 +76,10 @@ const createGallery = (settings: Partial<GallerySettings> = {}): GalleryStateVie
galleryView: 'images',
isLoading: false,
items: [],
+ page: 0,
pendingPlaceholders: [],
projectBoardId: null,
+ revealTargetPage: null,
searchTerm: '',
selectedBoardId: 'dogs',
semanticImageQuery: null,
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryImageGrid.browser.test.tsx b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryImageGrid.browser.test.tsx
index da1340f38a..517f21c8d0 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryImageGrid.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryImageGrid.browser.test.tsx
@@ -42,6 +42,7 @@ const mocks = vi.hoisted(() => ({
fetchNames: vi.fn(),
measure: vi.fn(),
scrollToIndex: vi.fn(),
+ setPage: vi.fn(),
virtualizerOptions: [] as Array<{
count: number;
estimateSize: (index: number) => number;
@@ -218,7 +219,9 @@ const createGallery = (overrides: Partial<GalleryStateView> = {}): GalleryStateV
galleryView: 'images',
isLoading: false,
items,
+ page: 0,
pendingPlaceholders: [],
+ revealTargetPage: null,
projectBoardId: null,
searchTerm: '',
selectedBoardId: board.id,
@@ -319,7 +322,7 @@ const createAdapter = (): GalleryUiAdapter =>
setCompareImage: noop,
setCompareItem: noop,
setItemMultiSelection: noop,
- setPage: noop,
+ setPage: mocks.setPage,
setPageInfo: noop,
setSearchTerm: noop,
setView: noop,
@@ -1093,6 +1096,93 @@ describe('GalleryImageGrid reveal requests', () => {
expect(mocks.scrollToIndex).toHaveBeenCalledTimes(1);
});
+ it('follows the selection onto its paginated page when the revealed item is not loaded', async () => {
+ const gallery = createGallery({
+ items: [createItem('image', 'page-zero.png')],
+ revealTargetPage: 2,
+ selectedItemKey: null,
+ selectedItemKeys: ['image:deep.png'],
+ });
+
+ await renderGallery(gallery);
+ await interact(() => requestGalleryItemReveal('image:deep.png'));
+
+ expect(mocks.setPage).toHaveBeenCalledWith(2);
+ expect(mocks.scrollToIndex).not.toHaveBeenCalled();
+
+ // The page arrives; the still-pending reveal settles by scrolling.
+ await renderGallery({
+ ...gallery,
+ items: [createItem('image', 'deep.png')],
+ page: 2,
+ selectedItemKey: 'image:deep.png',
+ });
+
+ expect(mocks.scrollToIndex).toHaveBeenCalledTimes(1);
+ });
+
+ it('follows a reveal onto its page at most once, so a missing item cannot pull the user back', async () => {
+ const gallery = createGallery({
+ items: [createItem('image', 'page-zero.png')],
+ revealTargetPage: 2,
+ selectedItemKey: null,
+ selectedItemKeys: ['image:deep.png'],
+ });
+
+ await renderGallery(gallery);
+ await interact(() => requestGalleryItemReveal('image:deep.png'));
+ expect(mocks.setPage).toHaveBeenCalledTimes(1);
+
+ // The stamped page arrives without the item, then the user pages away.
+ await renderGallery({ ...gallery, items: [createItem('image', 'page-two.png')], page: 2 });
+ await renderGallery({ ...gallery, items: [createItem('image', 'page-four.png')], page: 4 });
+
+ expect(mocks.setPage).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not page-follow a selection stamped for a different listing', async () => {
+ const gallery = createGallery({
+ items: [createItem('image', 'page-zero.png')],
+ revealTargetPage: null,
+ selectedItemKey: null,
+ selectedItemKeys: ['image:deep.png'],
+ });
+
+ await renderGallery(gallery);
+ await interact(() => requestGalleryItemReveal('image:deep.png'));
+
+ expect(mocks.setPage).not.toHaveBeenCalled();
+ });
+
+ it('retires a pending reveal once the persisted selection moves to another off-page item', async () => {
+ const gallery = createGallery({
+ items: [createItem('image', 'page-zero.png')],
+ revealTargetPage: null,
+ selectedItemKey: null,
+ selectedItemKeys: ['image:deep.png'],
+ });
+
+ await renderGallery(gallery);
+ await interact(() => requestGalleryItemReveal('image:deep.png'));
+
+ // An auto-selected fresh image replaces the persisted selection while
+ // both stay off-page (the visible key is null throughout).
+ await renderGallery({
+ ...gallery,
+ items: [createItem('image', 'page-zero.png')],
+ selectedItemKeys: ['image:fresh.png'],
+ });
+
+ // The revealed item arriving later must not scroll a retired reveal.
+ await renderGallery({
+ ...gallery,
+ items: [createItem('image', 'deep.png')],
+ selectedItemKeys: ['image:fresh.png'],
+ });
+
+ expect(mocks.scrollToIndex).not.toHaveBeenCalled();
+ });
+
it('retires a pending reveal once a different selection lands', async () => {
const items = [createItem('image', 'first.png')];
const gallery = createGallery({ items, selectedItemKey: null, selectedItemKeys: [] });
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryImageGrid.tsx b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryImageGrid.tsx
index cf51aa9852..9bb95f166e 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryImageGrid.tsx
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryImageGrid.tsx
@@ -50,6 +50,9 @@ import { useGalleryUploadInput } from './useGalleryUploadInput';
const viewportWidthCache = new Map<string, number>();
const STARRED_TRIGGER_HOVER_STYLES = { color: 'fg' } as const;
+// Module-scoped so a grid remount cannot replay an already-followed reveal.
+let lastPageFollowedRevealToken = 0;
+
const dragEventContainsFiles = (event: DragEvent): boolean => Array.from(event.dataTransfer.types).includes('Files');
/** The disclosure row above the starred items, styled to match board rows. */
@@ -247,8 +250,12 @@ export const GalleryImageGrid = () => {
// A different selection landing after the reveal (the user clicked
// something else) retires it — a late page load must not scroll away
- // from what they chose.
- if (gallery.selectedItemKey !== null && gallery.selectedItemKey !== pending.itemKey) {
+ // from what they chose. The persisted set covers off-page selections,
+ // whose visible key is null.
+ if (
+ (gallery.selectedItemKey !== null && gallery.selectedItemKey !== pending.itemKey) ||
+ (gallery.selectedItemKeys.length > 0 && !gallery.selectedItemKeys.includes(pending.itemKey))
+ ) {
pendingRevealRef.current = null;
return;
@@ -262,6 +269,20 @@ export const GalleryImageGrid = () => {
// instead of honoring it when the section is expanded again.
if (itemIndex >= 0 && scrollToItemIndex(itemIndex)) {
pendingRevealRef.current = null;
+
+ return;
+ }
+
+ // The item may live on another paginated page: follow once per reveal, so
+ // a reveal whose item never materializes cannot keep pulling the user back.
+ if (
+ itemIndex < 0 &&
+ gallery.revealTargetPage !== null &&
+ gallery.revealTargetPage !== gallery.page &&
+ lastPageFollowedRevealToken !== pending.token
+ ) {
+ lastPageFollowedRevealToken = pending.token;
+ galleryCommands.setPage(gallery.revealTargetPage);
}
});
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.test.ts b/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.test.ts
index 7ff9570661..aca14bd9ba 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.test.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.test.ts
@@ -203,6 +203,57 @@ describe('gallery state view', () => {
});
});
+ it('exposes the selection page only when its stamp names the listing the grid shows', () => {
+ const stamped = {
+ galleryPage: 0,
+ paginationMode: 'paginated',
+ selectedBoardId: 'none',
+ selectedImageQuery: {
+ boardId: 'none',
+ galleryView: 'images',
+ imageOrderDir: 'DESC',
+ page: 2,
+ paginationMode: 'paginated',
+ searchTerm: '',
+ },
+ };
+
+ expect(getGalleryStateView(stamped, boards, [], false).revealTargetPage).toBe(2);
+ expect(getGalleryStateView(stamped, boards, [], false).page).toBe(0);
+ expect(
+ getGalleryStateView({ ...stamped, selectedBoardId: 'board-1' }, boards, [], false).revealTargetPage
+ ).toBeNull();
+ expect(
+ getGalleryStateView({ ...stamped, paginationMode: 'infinite' }, boards, [], false).revealTargetPage
+ ).toBeNull();
+ expect(
+ getGalleryStateView(
+ { ...stamped, selectedImageQuery: { ...stamped.selectedImageQuery, galleryView: 'assets' } },
+ boards,
+ [],
+ false
+ ).revealTargetPage
+ ).toBeNull();
+ expect(
+ getGalleryStateView(
+ { ...stamped, selectedImageQuery: { ...stamped.selectedImageQuery, paginationMode: 'infinite' } },
+ boards,
+ [],
+ false
+ ).revealTargetPage
+ ).toBeNull();
+ expect(getGalleryStateView({ ...stamped, imageOrderDir: 'ASC' }, boards, [], false).revealTargetPage).toBeNull();
+ expect(getGalleryStateView({ ...stamped, searchTerm: 'cats' }, boards, [], false).revealTargetPage).toBeNull();
+ expect(
+ getGalleryStateView(
+ { ...stamped, semanticImageQuery: { imageName: 'ref.png', kind: 'image' } },
+ boards,
+ [],
+ false
+ ).revealTargetPage
+ ).toBeNull();
+ });
+
it('derives starred-first from the pagination mode: sectioned infinite window, flat paginated pages', () => {
expect(getGalleryStateView({ paginationMode: 'infinite' }, boards, [], false).settings.starredFirst).toBe(true);
expect(getGalleryStateView({ paginationMode: 'paginated' }, boards, [], false).settings.starredFirst).toBe(false);
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.ts b/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.ts
index e78c322088..173dbd02fc 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/galleryStateView.ts
@@ -86,8 +86,15 @@ export interface GalleryStateView {
galleryView: GalleryView;
items: GalleryItem[];
isLoading: boolean;
+ /** The grid's current page in paginated mode; the window anchor otherwise. */
+ page: number;
pendingPlaceholders: GalleryQueuePlaceholder[];
projectBoardId: string | null;
+ /**
+ * The selection's stamped paginated page, when the stamp names the listing
+ * the grid is showing; null otherwise. Reveals follow it across pages.
+ */
+ revealTargetPage: number | null;
searchTerm: string;
selectedBoardId: string;
selectedItemKey: GalleryItemKey | null;
@@ -466,6 +473,17 @@ export const getGalleryStateView = (
liveFollowEnabled,
selectedItemKey: visibleSelectedItemKey,
});
+ const selectedImageQuery = getGallerySelectedImageQuery(values);
+ const revealTargetPage =
+ settings.paginationMode === 'paginated' &&
+ selectedImageQuery.paginationMode === 'paginated' &&
+ semanticImageQuery === null &&
+ selectedImageQuery.boardId === selectedBoardId &&
+ selectedImageQuery.galleryView === galleryView &&
+ selectedImageQuery.imageOrderDir === settings.imageOrderDir &&
+ selectedImageQuery.searchTerm === searchTerm
+ ? selectedImageQuery.page
+ : null;
return {
anchoredWindowPage: isAnchoredInfiniteWindow ? page : 0,
@@ -475,6 +493,7 @@ export const getGalleryStateView = (
galleryView,
items,
isLoading,
+ page,
pendingPlaceholders:
settings.showPendingItems && semanticImageQuery === null && showsIncomingItemLanding
? getVisibleGalleryQueuePlaceholders(generationSequence.chronologicalSlots, {
@@ -485,6 +504,7 @@ export const getGalleryStateView = (
})
: [],
projectBoardId: getGalleryProjectBoardId(values),
+ revealTargetPage,
searchTerm,
selectedBoardId,
selectedItemKey: visibleSelectedItemKey,
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 19de26e1d5..c2746b2738 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
@@ -5,6 +5,7 @@ import type { WidgetViewProps } from '@workbench/widgetContracts';
import { ChakraProvider } from '@chakra-ui/react';
import { DndContext } from '@dnd-kit/core';
+import { requestGalleryItemReveal } from '@features/gallery/contracts';
import { QueryClient, QueryClientProvider, type InfiniteData } from '@tanstack/react-query';
import { system } from '@theme/system';
import i18next from 'i18next';
@@ -183,6 +184,11 @@ vi.mock('@features/gallery/queries', () => ({
},
}));
+vi.mock('@features/gallery/contracts', async (importOriginal) => ({
+ ...(await importOriginal<Record<string, unknown>>()),
+ requestGalleryItemReveal: vi.fn(),
+}));
+
vi.mock('@workbench/image-actions', () => ({
EMPTY_IMAGE_RECALL_CAPABILITIES: {},
ImageContextMenu: () => null,
@@ -489,6 +495,13 @@ describe('preview keyboard navigation boundary', () => {
}
});
+ it('reveals each navigated item so the gallery grid can follow', async () => {
+ await render();
+ await pressArrow('ArrowRight');
+
+ expect(vi.mocked(requestGalleryItemReveal)).toHaveBeenCalledWith('image:oldest');
+ });
+
it('keeps a just-completed batch navigable before the backend refetch lands', async () => {
// The batch finished and its queue item is already gone; the backend list
// (staleTime + coalesced invalidation) still only has the older image.
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
index a4f36b4800..30e3b3e7b0 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
@@ -27,6 +27,7 @@ import {
isGalleryImageItem,
legacyGeneratedImageToGalleryItem,
normalizeGalleryImage,
+ requestGalleryItemReveal,
toGalleryItemKey,
toGalleryItemRef,
type GalleryQueuePlaceholder,
@@ -275,7 +276,11 @@ export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
[account]
);
const selectGalleryItemAtPage = useCallback(
- (item: GalleryItem, selectionPage: number) => gallery.selectItem(item, undefined, selectionPage, true),
+ (item: GalleryItem, selectionPage: number) => {
+ gallery.selectItem(item, undefined, selectionPage, true);
+ // Deliberate navigation: the grid follows it, unlike auto-selection.
+ requestGalleryItemReveal(toGalleryItemKey(item));
+ },
[gallery]
);
const {
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
index 3710b6c75d..e2b63e6c05 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
@@ -3318,6 +3318,19 @@ describe('workbenchReducer Phase 5 generation flow', () => {
expect(galleryValues.selectedImageName).toBe('image:gallery-image.png');
});
+ it('normalizes the gallery page to a non-negative integer', () => {
+ let state = primeGenerate();
+
+ state = workbenchReducer(state, { page: 2.9, type: 'setGalleryPage' });
+ expect(getProjectWidgetValues(getActiveProject(state), 'gallery').galleryPage).toBe(2);
+
+ state = workbenchReducer(state, { page: -3, type: 'setGalleryPage' });
+ expect(getProjectWidgetValues(getActiveProject(state), 'gallery').galleryPage).toBe(0);
+
+ state = workbenchReducer(state, { page: Number.NaN, type: 'setGalleryPage' });
+ expect(getProjectWidgetValues(getActiveProject(state), 'gallery').galleryPage).toBe(0);
+ });
+
it('releases a mid-board infinite window anchor when a result lands on the viewed board', () => {
// A deep reveal from the image map anchors the infinite window mid-board.
// New images land at the TOP of that listing, which the anchored window
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.ts
index f42844d36a..d1940a095a 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchState.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.ts
@@ -4692,11 +4692,9 @@ export const __workbenchReducerInternal = (
);
}
case 'setGalleryPage': {
- return updateGalleryValues(
- state,
- (values) => ({ ...values, galleryPage: Math.max(0, action.page) }),
- action.projectId
- );
+ const galleryPage = Number.isFinite(action.page) ? Math.max(0, Math.floor(action.page)) : 0;
+
+ return updateGalleryValues(state, (values) => ({ ...values, galleryPage }), action.projectId);
}
case 'setGalleryPageInfo': {
if (!Number.isFinite(action.totalImages)) {