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.

refactor(ui): consolidate stage row construction

joshistoastb7bca595a85d
diff --git a/invokeai/frontend/webv2/src/workbench/palette/paletteRowModel.test.ts b/invokeai/frontend/webv2/src/workbench/palette/paletteRowModel.test.ts
index 7fded64182..d4ced11640 100644
--- a/invokeai/frontend/webv2/src/workbench/palette/paletteRowModel.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/palette/paletteRowModel.test.ts
@@ -5,7 +5,7 @@ import { describe, expect, it, vi } from 'vitest';
import type { PaletteEntry, PaletteSearchProvider, ProviderResultSection } from './entries';
import { derivePaletteQueryModel } from './paletteQueryModel';
-import { buildCommandPaletteRows } from './paletteRowModel';
+import { buildCommandPaletteRows, buildStageRows } from './paletteRowModel';
import { createInitialPaletteState, enterPaletteStage, type PaletteState } from './paletteState';
const t = ((key: string, options?: Record<string, unknown>) =>
@@ -113,3 +113,32 @@ describe('buildCommandPaletteRows', () => {
expect(withDates.at(-1)).toMatchObject({ label: 'commandPalette.syntax.commandsAndDates' });
});
});
+
+describe('buildStageRows', () => {
+ const stage = {
+ options: [
+ { apply: vi.fn(), id: 'dark', isCurrent: true, label: 'Dark' },
+ { apply: vi.fn(), id: 'light', isCurrent: false, label: 'Light' },
+ ],
+ title: 'Theme',
+ };
+
+ it('shows every option for an empty query', () => {
+ const rows = buildStageRows(stage, vi.fn(), '');
+
+ expect(rows.filter((row) => row.kind === 'entry').map((row) => row.id)).toEqual(['stage:dark', 'stage:light']);
+ });
+
+ it('filters options for a non-empty query', () => {
+ const rows = buildStageRows(stage, vi.fn(), 'li');
+
+ expect(rows.filter((row) => row.kind === 'entry').map((row) => row.id)).toEqual(['stage:light']);
+ });
+
+ it('marks the current option with the localized subtitle when a translator is supplied', () => {
+ const rows = buildStageRows(stage, vi.fn(), '', t);
+ const current = rows.find((row) => row.id === 'stage:dark');
+
+ expect(current?.kind === 'entry' && current.entry.subtitle).toBe('commandPalette.settings.current');
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/palette/paletteRowModel.ts b/invokeai/frontend/webv2/src/workbench/palette/paletteRowModel.ts
index 6eeddc9890..9e7da23722 100644
--- a/invokeai/frontend/webv2/src/workbench/palette/paletteRowModel.ts
+++ b/invokeai/frontend/webv2/src/workbench/palette/paletteRowModel.ts
@@ -3,7 +3,7 @@ import type { TFunction } from 'i18next';
import { completeTrailingDateToken, matchTrailingDateToken } from '@platform/search/dateTokens';
-import type { PaletteEntry, PaletteRow, PaletteSearchProvider, ProviderResultSection } from './entries';
+import type { PaletteEntry, PaletteRow, PaletteSearchProvider, PaletteStage, ProviderResultSection } from './entries';
import type { PaletteQueryModel } from './paletteQueryModel';
import { getPaletteContributionKey } from './contributionKey';
@@ -16,6 +16,18 @@ import {
searchPaletteRows,
} from './entries';
+/**
+ * Single source for stage row construction. `t` is optional so the synchronous
+ * stage-preview path can build rows without translation-dependent labels;
+ * rendered rows always pass it.
+ */
+export const buildStageRows = (
+ stage: PaletteStage,
+ onStageApplied: () => void,
+ query: string,
+ t?: TFunction
+): PaletteRow[] => searchPaletteRows(buildStageEntries(stage, onStageApplied, t), query, [], { showAllOnEmpty: true });
+
const DATE_TOKEN_SUGGESTIONS: Record<DateTokenKey, ReadonlyArray<{ labelKey: string; value: string }>> = {
date: [
{ labelKey: 'commandPalette.date.today', value: 'today' },
@@ -126,7 +138,7 @@ export const buildCommandPaletteRows = ({
queryModel;
if (stage) {
- return searchPaletteRows(buildStageEntries(stage, onStageApplied, t), query, [], { showAllOnEmpty: true });
+ return buildStageRows(stage, onStageApplied, query, t);
}
if (scopeProvider) {
diff --git a/invokeai/frontend/webv2/src/workbench/palette/useCommandPaletteController.ts b/invokeai/frontend/webv2/src/workbench/palette/useCommandPaletteController.ts
index b8c23bcc7b..7ee05d5132 100644
--- a/invokeai/frontend/webv2/src/workbench/palette/useCommandPaletteController.ts
+++ b/invokeai/frontend/webv2/src/workbench/palette/useCommandPaletteController.ts
@@ -8,10 +8,10 @@ import { useTranslation } from 'react-i18next';
import type { PaletteEntry, PaletteRow, PaletteSearchProvider, PaletteStage } from './entries';
import type { ScrollToIndex } from './usePaletteNavigation';
-import { buildStageEntries, searchPaletteRows, STAGE_ENTRY_ID_PREFIX } from './entries';
+import { STAGE_ENTRY_ID_PREFIX } from './entries';
import { PaletteDebouncer } from './paletteDebouncer';
import { derivePaletteQueryModel } from './paletteQueryModel';
-import { buildCommandPaletteRows } from './paletteRowModel';
+import { buildCommandPaletteRows, buildStageRows } from './paletteRowModel';
import {
commitPaletteQuery,
createInitialPaletteState,
@@ -143,7 +143,9 @@ export const useCommandPaletteController = ({
clearDebounce();
if (stage) {
- const nextRows = searchStageRows(stage, onStageApplied, nextQuery);
+ // Stage preview needs the next synchronous row set while typing; labels
+ // are not rendered here, so the optional translator is omitted.
+ const nextRows = buildStageRows(stage, onStageApplied, nextQuery);
previewRow(nextRows.find((row) => row.kind !== 'label'));
}
@@ -271,8 +273,3 @@ export const useCommandPaletteController = ({
trimmedQuery,
};
};
-
-const searchStageRows = (stage: PaletteStage, onStageApplied: () => void, query: string): PaletteRow[] =>
- // Kept local because stage preview needs the next synchronous row set while typing.
- // The main rendered row path lives in paletteRowModel.
- searchPaletteRows(buildStageEntries(stage, onStageApplied), query, [], { showAllOnEmpty: true });