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(models): disable a model picker with nothing to pick

Josh Corbettf871fa72a9ce
diff --git a/invokeai/frontend/webv2/src/features/models/core/library.ts b/invokeai/frontend/webv2/src/features/models/core/library.ts
index 11c1dc068c..048cb35750 100644
--- a/invokeai/frontend/webv2/src/features/models/core/library.ts
+++ b/invokeai/frontend/webv2/src/features/models/core/library.ts
@@ -140,14 +140,28 @@ export const groupModelsByType = (models: ModelConfig[]): ModelGroup[] => {
};
/** Candidate, sort, and grouping rules shared by every model picker instance. */
+const isModelPickerCandidate = (
+ model: ModelConfig,
+ allowedTypes: ReadonlySet<string>,
+ options: Pick<ModelPickerOptions, 'excludeKeys' | 'filter'>
+): boolean =>
+ allowedTypes.has(model.type) &&
+ !options.excludeKeys?.has(model.key) &&
+ (options.filter ? options.filter(model) : true);
+
+/** Whether the picker would list anything at all — the base filter aside, which only narrows a non-empty list. */
+export const hasModelPickerCandidates = (
+ models: readonly ModelConfig[],
+ options: Pick<ModelPickerOptions, 'excludeKeys' | 'filter' | 'modelTypes'>
+): boolean => {
+ const allowedTypes = new Set(options.modelTypes);
+
+ return models.some((model) => isModelPickerCandidate(model, allowedTypes, options));
+};
+
export const getModelPickerGroups = (models: ModelConfig[], options: ModelPickerOptions): ModelPickerResult => {
const allowedTypes = new Set(options.modelTypes);
- const candidates = models.filter(
- (model) =>
- allowedTypes.has(model.type) &&
- !options.excludeKeys?.has(model.key) &&
- (options.filter ? options.filter(model) : true)
- );
+ const candidates = models.filter((model) => isModelPickerCandidate(model, allowedTypes, options));
// Chips are derived from candidates — before the base filter — so the chip
// row stays stable while the user toggles chips.
const availableBases = collectBasesForDisplay(candidates);
diff --git a/invokeai/frontend/webv2/src/features/models/ui/components/ModelSelect.browser.test.tsx b/invokeai/frontend/webv2/src/features/models/ui/components/ModelSelect.browser.test.tsx
index 66d684f474..52868ff13f 100644
--- a/invokeai/frontend/webv2/src/features/models/ui/components/ModelSelect.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/models/ui/components/ModelSelect.browser.test.tsx
@@ -39,6 +39,8 @@ const MODELS_UI_ADAPTER = { enableModelDescriptions: true, managerProjectId: nul
const MAIN_MODEL_TYPES: ['main'] = ['main'];
const CROSS_TYPE_MODEL_TYPES: ['main', 'lora'] = ['main', 'lora'];
+const CONTROLNET_TYPES = ['controlnet'] as const;
+
describe('ModelSelect loading states', () => {
let host: HTMLDivElement;
let root: Root;
@@ -74,6 +76,41 @@ describe('ModelSelect loading states', () => {
await expect.poll(() => document.querySelector('[role="option"]')?.textContent).toContain('SDXL Main');
});
+ it('disables the trigger instead of opening an empty list when nothing compatible is installed', async () => {
+ setModelsSnapshotForTests({ error: null, models: [model], status: 'loaded' });
+
+ await act(() => {
+ root.render(
+ <ChakraProvider value={system}>
+ <ModelsUiProvider adapter={MODELS_UI_ADAPTER}>
+ <ModelSelect modelTypes={CONTROLNET_TYPES} showManagerButton={false} value={null} onChange={vi.fn()} />
+ </ModelsUiProvider>
+ </ChakraProvider>
+ );
+ });
+
+ const trigger = host.querySelector<HTMLButtonElement>('[aria-haspopup="listbox"]')!;
+ expect(trigger.disabled).toBe(true);
+ expect(trigger.textContent).toContain('scopeNoCompatibleInstalled');
+ await act(() => trigger.click());
+ // Closed: the picker computes no options, and the popover never opens.
+ expect(document.querySelectorAll('[role="option"]')).toHaveLength(0);
+ expect(document.querySelector('[data-scope="popover"][data-part="content"][data-state="open"]')).toBeNull();
+
+ // While the library is still loading there is nothing to conclude yet.
+ setModelsSnapshotForTests({ error: null, models: [], status: 'loading' });
+ await act(() => {
+ root.render(
+ <ChakraProvider value={system}>
+ <ModelsUiProvider adapter={MODELS_UI_ADAPTER}>
+ <ModelSelect modelTypes={CONTROLNET_TYPES} showManagerButton={false} value={null} onChange={vi.fn()} />
+ </ModelsUiProvider>
+ </ChakraProvider>
+ );
+ });
+ expect(host.querySelector<HTMLButtonElement>('[aria-haspopup="listbox"]')!.disabled).toBe(false);
+ });
+
const renderPicker = async (props: Partial<Parameters<typeof ModelSelect>[0]> = {}) => {
await act(() => {
root.render(
diff --git a/invokeai/frontend/webv2/src/features/models/ui/components/ModelSelect.tsx b/invokeai/frontend/webv2/src/features/models/ui/components/ModelSelect.tsx
index f75a6bdd60..99e8a59b8f 100644
--- a/invokeai/frontend/webv2/src/features/models/ui/components/ModelSelect.tsx
+++ b/invokeai/frontend/webv2/src/features/models/ui/components/ModelSelect.tsx
@@ -4,7 +4,7 @@ import type { PickerGroup, PickerOptionState } from '@platform/ui/Picker';
import { Badge, Box, HStack, Icon, Image, Popover, Portal, Spacer, Stack, Text } from '@chakra-ui/react';
import { getModelBaseColorPalette, getModelBaseLabel, getModelBaseLongLabel } from '@features/models/core/baseIdentity';
-import { getModelPickerGroups } from '@features/models/core/library';
+import { getModelPickerGroups, hasModelPickerCandidates } from '@features/models/core/library';
import { formatBytes, getModelTypeLabel, getModelTypePluralLabel } from '@features/models/core/taxonomy';
import { getModelImageUrl } from '@features/models/data/api';
import { ensureModelsLoaded, useModelsSelector } from '@features/models/data/modelsStore';
@@ -119,7 +119,7 @@ export const ModelSelect = ({
const loadError = useModelsSelector((snapshot) => snapshot.error);
const loadStatus = useModelsSelector((snapshot) => snapshot.status);
const [isOpen, setIsOpen] = useState(false);
- const [lastDisabled, setLastDisabled] = useState(disabled);
+ const [lastDisabled, setLastDisabled] = useState(Boolean(disabled));
const pickerId = id ?? `models:${modelTypes.join('+')}`;
const isCompact = useModelsUiSelector((snapshot) => snapshot.pickerCompactViews[pickerId] ?? false);
@@ -139,10 +139,19 @@ export const ModelSelect = ({
void ensureModelsLoaded();
});
- if (disabled !== lastDisabled) {
- setLastDisabled(disabled);
+ // Nothing to offer once the library is known: the trigger disables and says
+ // so, rather than opening an empty list. A stale selection stays clearable.
+ const hasCandidates = useMemo(
+ () => loadStatus !== 'loaded' || hasModelPickerCandidates(models, { excludeKeys, filter, modelTypes }),
+ [excludeKeys, filter, loadStatus, modelTypes, models]
+ );
+ const isEmpty = !hasCandidates && !value;
+ const isInert = disabled || isEmpty;
+
+ if (isInert !== lastDisabled) {
+ setLastDisabled(isInert);
- if (disabled) {
+ if (isInert) {
setIsOpen(false);
}
}
@@ -224,7 +233,7 @@ export const ModelSelect = ({
strategy: 'fixed',
}}
onOpenChange={(event) => {
- if (disabled) {
+ if (isInert) {
setIsOpen(false);
return;
}
@@ -240,7 +249,7 @@ export const ModelSelect = ({
className={className}
borderColor={invalid ? undefined : isOpen ? 'accent.solid' : 'border'}
colorPalette={invalid ? 'red' : 'gray'}
- disabled={disabled}
+ disabled={isInert}
justifyContent="space-between"
minW="0"
pe={canClear ? '7' : '2'}
@@ -259,10 +268,12 @@ export const ModelSelect = ({
<ModelButtonContent model={selectedModel} />
) : (
<Text as="span" color="fg.muted" fontSize="xs" minW="0" truncate>
- {placeholder ?? t('models.scopeSelect', { scope: scopeLabel })}
+ {isEmpty
+ ? t('models.scopeNoCompatibleInstalled', { scope: scopeLabel })
+ : (placeholder ?? t('models.scopeSelect', { scope: scopeLabel }))}
</Text>
)}
- {canClear ? null : <Icon as={ChevronDownIcon} boxSize="3" flexShrink={0} />}
+ {canClear || isEmpty ? null : <Icon as={ChevronDownIcon} boxSize="3" flexShrink={0} />}
</Button>
</Popover.Trigger>
{canClear ? (