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): step through the list the gallery is actually showing

Lincoln Stein35c17a8fe903
diff --git a/invokeai/frontend/webv2/src/features/gallery/contracts.ts b/invokeai/frontend/webv2/src/features/gallery/contracts.ts
index 4d76802ff4..ca29f42001 100644
--- a/invokeai/frontend/webv2/src/features/gallery/contracts.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/contracts.ts
@@ -34,6 +34,7 @@ export {
type GalleryVideoItem,
} from './core/items';
export { GALLERY_RECENT_IMAGE_LIMIT, getBoundedRecentImages } from './core/recentImages';
+export { gallerySemanticReferenceKey, type GallerySemanticReference } from './core/semanticImageQuery';
export {
DEFAULT_GALLERY_SETTINGS,
getGallerySettings,
@@ -49,6 +50,7 @@ export {
getGalleryPage,
getGallerySelectedImagePage,
getGallerySelectedImageQuery,
+ getGallerySemanticImageQuery,
getGalleryPlaceholderInsertionIndex,
type GalleryCurrentItem,
type GalleryGenerationSequence,
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.test.ts b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.test.ts
index 4219a12c75..288f83f2c8 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.test.ts
@@ -61,6 +61,45 @@ describe('mergePreviewBoardItems', () => {
expect(mergePreviewBoardItems([oldest, newest], [middle, oldest], 'ASC')).toEqual([oldest, middle, newest]);
});
+ it('preserves relevance order for a ranked list', () => {
+ // A similarity result set is ordered by relevance, not by date. Re-sorting
+ // it here would make the arrows walk a different order from the one on
+ // screen. (Local generations never reach this call in ranked mode — the
+ // caller passes only the selection; see the anchor test below.)
+ const first = item('image', 'first', '2026-07-21T00:00:01.000Z');
+ const second = item('image', 'second', '2026-07-21T00:00:03.000Z', true);
+ const third = item('image', 'third', '2026-07-21T00:00:02.000Z');
+ const optimistic = item('image', 'optimistic', '2026-07-21T00:00:09.000Z');
+
+ expect(mergePreviewBoardItems([first, second, third], [], 'DESC', { isRanked: true })).toEqual([
+ first,
+ second,
+ third,
+ ]);
+ // Without the flag the same input is re-sorted (starred first, then
+ // newest) and the optimistic item is spliced in, so the ranked path really
+ // is what preserves the list.
+ expect(mergePreviewBoardItems([first, second, third], [optimistic], 'DESC')).toEqual([
+ second,
+ optimistic,
+ third,
+ first,
+ ]);
+ });
+
+ it('anchors a selection that the ranking does not contain, rather than losing the cursor', () => {
+ // A selection can be made outside the result set — an upload, an image-map
+ // click, or stepping off the live tile. Dropping it would leave the cursor
+ // pointing at nothing, which reads as both arrows going dead; keeping it
+ // at the head lets one press move into the ranked list.
+ const ranked = item('image', 'ranked', '2026-07-21T00:00:01.000Z');
+ const outsider = item('image', 'outsider', '2026-07-21T00:00:09.000Z');
+
+ expect(mergePreviewBoardItems([ranked], [outsider], 'DESC', { isRanked: true })).toEqual([outsider, ranked]);
+ // Already a member: kept once, in its ranked position.
+ expect(mergePreviewBoardItems([ranked], [ranked], 'DESC', { isRanked: true })).toEqual([ranked]);
+ });
+
it('keeps starred backend items ahead of optimistic unstarred items', () => {
const starred = item('video', 'starred', '2026-07-21T00:00:01.000Z', true);
const optimistic = item('image', 'optimistic', '2026-07-21T00:00:03.000Z');
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
index d44a4826b8..74df494db8 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/PreviewWidgetView.tsx
@@ -16,7 +16,9 @@ import {
getGalleryCompareImage,
getGalleryGenerationSequence,
getGalleryLiveSlots,
+ getGalleryPage,
getGallerySelectedImageQuery,
+ getGallerySemanticImageQuery,
getGallerySettings,
getSelectedGalleryItemFromValues,
getBoundedRecentImages,
@@ -205,6 +207,15 @@ export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
const hasSelectedItem = selectedItem !== null;
const { imageOrderDir } = getGallerySettings(galleryValues);
const selectedImageQuery = getGallerySelectedImageQuery(galleryValues);
+ // The gallery's live similarity search: when one is active the grid shows a
+ // ranked result set, and navigation has to walk that same list. Memoized on
+ // the raw value because parsing mints a fresh object each call, which would
+ // otherwise re-derive the whole navigation list on every unrelated gallery
+ // change (every recentImages tick during a generation, for one).
+ const gallerySemanticQuery = useMemo(
+ () => getGallerySemanticImageQuery({ semanticImageQuery: galleryValues.semanticImageQuery }),
+ [galleryValues.semanticImageQuery]
+ );
const selectedItemKey = selectedItem ? toGalleryItemKey(selectedItem) : null;
const isComparing =
selectedItem?.kind === 'image' &&
@@ -222,7 +233,12 @@ export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
[activeProgressTargets, generationSequence.chronologicalSlots]
);
const matchingProgressImage = getMatchingProgressImage(progressImage, activeGalleryPlaceholder);
- const shouldFollowLive = showProgressImagesInViewer && activeGalleryPlaceholder !== null && !isComparing;
+ // Not while a similarity search is active: the grid hides pending items
+ // there entirely, so following the generation would put Preview on a tile
+ // the grid is not showing and, worse, hand the arrows the board listing
+ // while the grid shows a ranking.
+ const shouldFollowLive =
+ showProgressImagesInViewer && activeGalleryPlaceholder !== null && !isComparing && gallerySemanticQuery === null;
const { t } = useTranslation();
const loupeControlsRef = useRef<PreviewLoupeControls | null>(null);
const videoControllerRef = useRef<PreviewVideoFrameController | null>(null);
@@ -267,7 +283,10 @@ export const PreviewWidgetView = ({ region, runtime }: WidgetViewProps) => {
selectGalleryItem: selectGalleryItemAtPage,
selectedImageQuery,
selectedItem,
+ galleryPage: getGalleryPage(galleryValues),
+ galleryPaginationMode: getGallerySettings(galleryValues).paginationMode,
selectedItemKey,
+ semanticQuery: gallerySemanticQuery,
shouldFollowLive,
});
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/preview/usePreviewNavigation.ts b/invokeai/frontend/webv2/src/workbench/widgets/preview/usePreviewNavigation.ts
index aa610f3669..a095da4066 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/preview/usePreviewNavigation.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/preview/usePreviewNavigation.ts
@@ -2,13 +2,14 @@ import type { GalleryImageItem, GalleryItem, GalleryItemKey, GalleryView } from
import type {
GalleryQueuePlaceholder,
GalleryItemsPage,
+ GallerySemanticReference,
getGallerySelectedImageQuery,
} from '@features/gallery/contracts';
import type { QueueItem } from '@features/queue/contracts';
import type { InfiniteData } from '@tanstack/react-query';
import type { KeyboardEvent } from 'react';
-import { compareGalleryItems, toGalleryItemKey } from '@features/gallery/contracts';
+import { compareGalleryItems, gallerySemanticReferenceKey, toGalleryItemKey } from '@features/gallery/contracts';
import {
flattenGalleryItemsData,
GALLERY_MAX_ROWS,
@@ -88,9 +89,24 @@ const getOrderedLocalItems = ({
export const mergePreviewBoardItems = (
backendItems: GalleryItem[],
localItems: GalleryItem[],
- imageOrderDir: 'ASC' | 'DESC'
+ imageOrderDir: 'ASC' | 'DESC',
+ { isRanked = false }: { isRanked?: boolean } = {}
): GalleryItem[] => {
const backendKeys = new Set(backendItems.map(toGalleryItemKey));
+
+ // Relevance order IS the list: re-sorting it by date would reorder what the
+ // user is looking at, and local generations are not members of a ranked
+ // result set at all (the gallery grid overlays none of them either). The
+ // caller still passes the SELECTED item, which is kept when the ranking does
+ // not contain it — a selection made outside the result set (an upload, an
+ // image-map click, stepping off the live tile) would otherwise leave the
+ // cursor pointing at nothing, which reads as both arrows going dead.
+ if (isRanked) {
+ const anchors = localItems.filter((item) => !backendKeys.has(toGalleryItemKey(item)));
+
+ return [...anchors, ...backendItems].slice(0, GALLERY_MAX_ROWS);
+ }
+
const missingLocalItems = localItems.filter((item) => !backendKeys.has(toGalleryItemKey(item)));
if (missingLocalItems.length === 0) {
@@ -122,16 +138,23 @@ export const usePreviewNavigation = ({
isComparing,
localItems,
queueItems,
+ galleryPage,
+ galleryPaginationMode,
selectGalleryItem,
selectedImageQuery,
selectedItem,
selectedItemKey,
+ semanticQuery,
shouldFollowLive,
}: {
/** The live slot from getGalleryGenerationSequence, or null. */
activePlaceholder: GalleryQueuePlaceholder | null;
/** Turns the live-follow preference back on (stepping onto the placeholder). */
enableLiveFollow: () => void;
+ /** The page the gallery grid is on; a ranked list mirrors it (see below). */
+ galleryPage: number;
+ /** The gallery's own pagination mode, likewise mirrored by a ranked list. */
+ galleryPaginationMode: 'infinite' | 'paginated';
/** The gallery's own sort settings, used while following live. */
imageOrderDir: 'ASC' | 'DESC';
isComparing: boolean;
@@ -142,6 +165,8 @@ export const usePreviewNavigation = ({
selectedImageQuery: ReturnType<typeof getGallerySelectedImageQuery>;
selectedItem: GalleryItem | null;
selectedItemKey: GalleryItemKey | null;
+ /** The gallery's active similarity search, or null for the board listing. */
+ semanticQuery: GallerySemanticReference | null;
shouldFollowLive: boolean;
}): PreviewNavigationState => {
const hasSelectedItem = selectedItem !== null;
@@ -153,9 +178,17 @@ export const usePreviewNavigation = ({
shouldFollowLive && activePlaceholder ? activePlaceholder.boardId : selectedImageQuery.boardId;
const navigationGalleryView = shouldFollowLive ? 'images' : selectedImageQuery.galleryView;
const navigationOrderDir = shouldFollowLive ? imageOrderDir : selectedImageQuery.imageOrderDir;
+ // Read from the gallery's CURRENT search, not from a copy stamped onto the
+ // selection: the chip is a view mode, and Preview has to follow it the
+ // moment it is set or cleared or it walks a list that is no longer on
+ // screen. Following live means watching the board a generation is landing
+ // in, which no ranked result set describes, so the search is dropped for
+ // that mode exactly as the board's own search term is.
+ const navigationSemanticQuery = shouldFollowLive ? null : semanticQuery;
+ const navigationSemanticKey = gallerySemanticReferenceKey(navigationSemanticQuery);
const hasNavigationContext = shouldFollowLive || hasSelectedItem;
- const navigationContextKey = `${shouldFollowLive}:${selectedItemKey ?? ''}:${navigationBoardId}:${navigationGalleryView}:${navigationOrderDir}:${selectedImageQuery.paginationMode}:${selectedImageQuery.page}:${selectedImageQuery.searchTerm}`;
- const navigationQueryKey = `${shouldFollowLive}:${navigationBoardId}:${navigationGalleryView}:${navigationOrderDir}:${selectedImageQuery.paginationMode}:${selectedImageQuery.searchTerm}`;
+ const navigationContextKey = `${shouldFollowLive}:${selectedItemKey ?? ''}:${navigationBoardId}:${navigationGalleryView}:${navigationOrderDir}:${selectedImageQuery.paginationMode}:${selectedImageQuery.page}:${selectedImageQuery.searchTerm}:${navigationSemanticKey}`;
+ const navigationQueryKey = `${shouldFollowLive}:${navigationBoardId}:${navigationGalleryView}:${navigationOrderDir}:${selectedImageQuery.paginationMode}:${selectedImageQuery.searchTerm}:${navigationSemanticKey}`;
// Lets a boundary fetch that resolves after the user has moved on compare the
// context it started in against the one now on screen, and drop its stale
@@ -186,6 +219,22 @@ export const usePreviewNavigation = ({
const navigationAnchorPage = hasStaleNavigationAnchor ? selectedImageQuery.page : navigationAnchor.page;
+ // A ranked list mirrors the GRID's paging instead of the stamped context.
+ // The stamped page indexes the board listing, and a board page applied to a
+ // ranking lands on an unrelated slice — or, past the end of a ranking that is
+ // shorter than the board, on an empty one whose only member is the anchored
+ // selection, leaving both arrows with nowhere to step. Setting a search also
+ // resets the grid's page, which the stamped record never sees.
+ const navigationWindow = shouldFollowLive
+ ? ({ kind: 'infinite' } as const)
+ : navigationSemanticQuery !== null
+ ? galleryPaginationMode === 'paginated'
+ ? ({ kind: 'anchor', offset: galleryPage * GALLERY_PAGE_SIZE } as const)
+ : ({ kind: 'infinite' } as const)
+ : selectedImageQuery.paginationMode === 'paginated'
+ ? ({ kind: 'anchor', offset: navigationAnchorPage * GALLERY_PAGE_SIZE } as const)
+ : ({ kind: 'infinite' } as const);
+
const {
data: boardItemsData,
fetchNextPage: fetchNextBoardItemsPage,
@@ -204,12 +253,11 @@ export const usePreviewNavigation = ({
galleryView: navigationGalleryView,
orderDir: navigationOrderDir,
searchTerm: shouldFollowLive ? '' : selectedImageSearch.text,
+ ...(navigationSemanticQuery ? { semanticQuery: navigationSemanticQuery } : {}),
// Starred-first is pinned in gallery/core/settings.ts.
starredFirst: true,
},
- !shouldFollowLive && selectedImageQuery.paginationMode === 'paginated'
- ? { kind: 'anchor', offset: navigationAnchorPage * GALLERY_PAGE_SIZE }
- : { kind: 'infinite' }
+ navigationWindow
),
enabled: hasNavigationContext,
});
@@ -221,12 +269,21 @@ export const usePreviewNavigation = ({
page.items.some((candidate) => toGalleryItemKey(candidate) === itemKey)
);
const pageParam = pageIndex === undefined || pageIndex < 0 ? undefined : boardItemsData?.pageParams[pageIndex];
+ // `page` is stamped as a BOARD page and read as one everywhere else, so
+ // a ranked window's page params — offsets into the ranking — must not be
+ // written into it. Stepping through a ranked list therefore leaves the
+ // board context it was opened from untouched, ready for the moment the
+ // search is cleared.
const selectionPage =
- typeof pageParam === 'number' ? Math.floor(pageParam / GALLERY_PAGE_SIZE) : selectedImageQuery.page;
+ navigationSemanticQuery !== null
+ ? selectedImageQuery.page
+ : typeof pageParam === 'number'
+ ? Math.floor(pageParam / GALLERY_PAGE_SIZE)
+ : selectedImageQuery.page;
selectGalleryItem(item, selectionPage);
},
- [boardItemsData, selectGalleryItem, selectedImageQuery.page]
+ [boardItemsData, navigationSemanticQuery, selectGalleryItem, selectedImageQuery.page]
);
const optimisticQueueItemIds = useMemo(
@@ -294,24 +351,43 @@ export const usePreviewNavigation = ({
return [selectedItem, ...localBoardItems];
}, [localBoardItems, selectedItem, selectedItemKey, shouldFollowLive]);
const backendBoardItems = useMemo(() => flattenPreviewItems(boardItemsData), [boardItemsData]);
+ // Recents belong to a board listing; a ranked list gets only the selection,
+ // and only as the cursor anchor described in mergePreviewBoardItems.
+ const previewMergeItems = useMemo(
+ () =>
+ navigationSemanticQuery === null ? previewLocalBoardItems : selectedItem ? [selectedItem] : EMPTY_PREVIEW_ITEMS,
+ [navigationSemanticQuery, previewLocalBoardItems, selectedItem]
+ );
const boardItems = useMemo(
() =>
!hasNavigationContext
? EMPTY_PREVIEW_ITEMS
- : mergePreviewBoardItems(backendBoardItems, previewLocalBoardItems, navigationOrderDir),
- [backendBoardItems, hasNavigationContext, navigationOrderDir, previewLocalBoardItems]
+ : mergePreviewBoardItems(backendBoardItems, previewMergeItems, navigationOrderDir, {
+ isRanked: navigationSemanticQuery !== null,
+ }),
+ [backendBoardItems, hasNavigationContext, navigationOrderDir, navigationSemanticQuery, previewMergeItems]
);
const isLoadingBoard = hasNavigationContext && isFetchingBoardItems;
const navigationSequence = useMemo(
() =>
getPreviewNavigationSequence({
- activePlaceholder,
+ // A ranked result set has no chronological insertion point, so the
+ // generating placeholder is left out — matching the gallery grid,
+ // which hides pending items while a similarity search is active.
+ activePlaceholder: navigationSemanticQuery ? null : activePlaceholder,
boardId: navigationBoardId,
boardImages: boardItems,
galleryView: navigationGalleryView,
imageOrderDir: navigationOrderDir,
}),
- [activePlaceholder, boardItems, navigationBoardId, navigationGalleryView, navigationOrderDir]
+ [
+ activePlaceholder,
+ boardItems,
+ navigationBoardId,
+ navigationGalleryView,
+ navigationOrderDir,
+ navigationSemanticQuery,
+ ]
);
const navigationCursor = getPreviewNavigationCursor(navigationSequence, {
isFollowingLive: shouldFollowLive,
@@ -362,13 +438,14 @@ export const usePreviewNavigation = ({
}
const nextBackendBoardItems = flattenPreviewItems(result.data);
- const nextBoardItems = mergePreviewBoardItems(
- nextBackendBoardItems,
- previewLocalBoardItems,
- navigationOrderDir
- );
+ const nextBoardItems = mergePreviewBoardItems(nextBackendBoardItems, previewMergeItems, navigationOrderDir, {
+ isRanked: navigationSemanticQuery !== null,
+ });
const nextNavigationSequence = getPreviewNavigationSequence({
- activePlaceholder,
+ // Same exclusion as the render path: a ranked list has no
+ // chronological slot for the generating placeholder, and inserting
+ // one here would step onto a tile the sequence never showed.
+ activePlaceholder: navigationSemanticQuery ? null : activePlaceholder,
boardId: navigationBoardId,
boardImages: nextBoardItems,
galleryView: navigationGalleryView,
@@ -403,8 +480,9 @@ export const usePreviewNavigation = ({
navigationCursor,
navigationGalleryView,
navigationOrderDir,
+ navigationSemanticQuery,
navigationSequence,
- previewLocalBoardItems,
+ previewMergeItems,
selectedItemKey,
selectPreviewItem,
shouldFollowLive,