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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
refactor(ui): type the starter sort as its own field subset
Josh Corbetta5c91e8b0ad1
diff --git a/invokeai/frontend/webv2/src/features/models/core/starters.ts b/invokeai/frontend/webv2/src/features/models/core/starters.tsindex 70ae6b2df4..8f3540b82b 100644--- a/invokeai/frontend/webv2/src/features/models/core/starters.ts+++ b/invokeai/frontend/webv2/src/features/models/core/starters.ts@@ -5,12 +5,15 @@ import { getModelBaseLabel } from './baseIdentity';/** Pure filter/sort for the starter-models catalog; the installed-library twin is `library.ts`. */+/** Starter models have no local files, so `size` is not a meaningful sort. */+export type StarterSortField = Exclude<ModelSortField, 'size'>;+export interface StarterModelFilters {/** null = all types. */typeFilter: ModelTaxonomyType | null;/** null = all bases. */baseFilter: string | null;- sortField: ModelSortField;+ sortField: StarterSortField;sortDirection: 'asc' | 'desc';}@@ -26,7 +29,7 @@ interface IndexedStarterModel {model: StarterModel;}-const compareStarterModels = (a: IndexedStarterModel, b: IndexedStarterModel, field: ModelSortField): number => {+const compareStarterModels = (a: IndexedStarterModel, b: IndexedStarterModel, field: StarterSortField): number => {switch (field) {case 'default':return a.index - b.index;@@ -36,9 +39,6 @@ const compareStarterModels = (a: IndexedStarterModel, b: IndexedStarterModel, fireturn getModelBaseLabel(a.model.base).localeCompare(getModelBaseLabel(b.model.base));case 'format':return String(a.model.format ?? '').localeCompare(String(b.model.format ?? ''));- case 'size':- // Starter models have no local files, so size is not a meaningful sort.- return 0;}};diff --git a/invokeai/frontend/webv2/src/features/models/ui/add-models/StarterFilterMenu.tsx b/invokeai/frontend/webv2/src/features/models/ui/add-models/StarterFilterMenu.tsxindex 517b9717df..59b4bb7115 100644--- a/invokeai/frontend/webv2/src/features/models/ui/add-models/StarterFilterMenu.tsx+++ b/invokeai/frontend/webv2/src/features/models/ui/add-models/StarterFilterMenu.tsx@@ -1,12 +1,14 @@-import type { StarterModelFilters } from '@features/models/core/starters';+import type { StarterModelFilters, StarterSortField } from '@features/models/core/starters';import type { ModelTaxonomyType } from '@features/models/core/types';+import type { ModelFilterSortOption } from '@features/models/ui/shared/ModelFilterMenu';/* eslint-disable react-perf/jsx-no-jsx-as-prop, react-perf/jsx-no-new-array-as-prop, react-perf/jsx-no-new-function-as-prop, react-perf/jsx-no-new-object-as-prop */import { ModelFilterMenu, SORT_FIELD_OPTIONS } from '@features/models/ui/shared/ModelFilterMenu';import { useTranslation } from 'react-i18next';-// Starter models have no local files, so size is not a meaningful sort.-const STARTER_SORT_FIELDS = SORT_FIELD_OPTIONS.filter((option) => option.field !== 'size');+const STARTER_SORT_FIELDS = SORT_FIELD_OPTIONS.filter(+ (option): option is ModelFilterSortOption<StarterSortField> => option.field !== 'size'+);const isFiltering = (filters: StarterModelFilters): boolean =>filters.typeFilter !== null || filters.baseFilter !== null;diff --git a/invokeai/frontend/webv2/src/features/models/ui/library/ModelFilterBar.tsx b/invokeai/frontend/webv2/src/features/models/ui/library/ModelFilterBar.tsxindex 74d72cd7a7..8856e1d23c 100644--- a/invokeai/frontend/webv2/src/features/models/ui/library/ModelFilterBar.tsx+++ b/invokeai/frontend/webv2/src/features/models/ui/library/ModelFilterBar.tsx@@ -3,7 +3,7 @@ import type { ModelLibraryFilters, ModelSortField } from '@features/models/core/import type { ModelTaxonomyType } from '@features/models/core/types';import { HStack, Icon, Input, InputGroup } from '@chakra-ui/react';-import { FilterMenuItem, ModelFilterMenu } from '@features/models/ui/shared/ModelFilterMenu';+import { FilterMenuItem, ModelFilterMenu, SORT_FIELD_OPTIONS } from '@features/models/ui/shared/ModelFilterMenu';import { SearchIcon } from 'lucide-react';import { useCallback, useMemo } from 'react';import { useTranslation } from 'react-i18next';@@ -81,6 +81,7 @@ export const ModelFilterBar = ({isActive={isFiltering(filters)}sortDirection={filters.sortDirection}sortField={filters.sortField}+ sortFields={SORT_FIELD_OPTIONS}typeAllChecked={filters.typeFilter === null && !filters.missingOnly}typeFilter={filters.typeFilter}onBaseFilterChange={handleBaseFilterChange}diff --git a/invokeai/frontend/webv2/src/features/models/ui/shared/ModelFilterMenu.tsx b/invokeai/frontend/webv2/src/features/models/ui/shared/ModelFilterMenu.tsxindex 06fb0e0d8e..73e294f18c 100644--- a/invokeai/frontend/webv2/src/features/models/ui/shared/ModelFilterMenu.tsx+++ b/invokeai/frontend/webv2/src/features/models/ui/shared/ModelFilterMenu.tsx@@ -11,8 +11,8 @@ import { CheckIcon, SlidersHorizontalIcon } from 'lucide-react';import { memo } from 'react';import { useTranslation } from 'react-i18next';-export interface ModelFilterSortOption {- field: ModelSortField;+export interface ModelFilterSortOption<Field extends ModelSortField = ModelSortField> {+ field: Field;labelKey: string;}@@ -25,8 +25,12 @@ export const SORT_FIELD_OPTIONS: readonly ModelFilterSortOption[] = [{ field: 'format', labelKey: 'models.sort.format' },];-/** Shared taxonomy filter + sort menu for installed and starter model lists. */-export const ModelFilterMenu = ({+/**+ * Shared taxonomy filter + sort menu for installed and starter model lists.+ * Generic over the sort-field subset so a menu fed a narrowed `sortFields`+ * list reports only those fields to `onSortChange`.+ */+export const ModelFilterMenu = <Field extends ModelSortField>({ariaLabel,availableBases,availableTypes,@@ -38,7 +42,7 @@ export const ModelFilterMenu = ({onTypeFilterChange,sortDirection,sortField,- sortFields = SORT_FIELD_OPTIONS,+ sortFields,typeAllChecked,typeAllLabel,typeFilter,@@ -50,17 +54,19 @@ export const ModelFilterMenu = ({extraTypeItems?: ReactNode;isActive: boolean;onBaseFilterChange: (base: string | null) => void;- onSortChange: (field: ModelSortField, direction: 'asc' | 'desc') => void;+ onSortChange: (field: Field, direction: 'asc' | 'desc') => void;onTypeFilterChange: (type: ModelTaxonomyType | null) => void;sortDirection: 'asc' | 'desc';- sortField: ModelSortField;- sortFields?: readonly ModelFilterSortOption[];+ sortField: Field;+ sortFields: readonly ModelFilterSortOption<Field>[];typeAllChecked?: boolean;typeAllLabel?: string;typeFilter: ModelTaxonomyType | null;}) => {const { t } = useTranslation();const resolvedTypeAllLabel = typeAllLabel ?? t('models.allModels');+ // Safe to widen: the menu only reports fields drawn from `sortFields`.+ const reportSort = onSortChange as (field: ModelSortField, direction: 'asc' | 'desc') => void;return (<Menu.Root closeOnSelect={false} positioning={{ placement: 'bottom-end' }}>@@ -118,7 +124,7 @@ export const ModelFilterMenu = ({labelKey={labelKey}sortDirection={sortDirection}sortField={sortField}- onSortChange={onSortChange}+ onSortChange={reportSort}/>))}</Menu.ItemGroup>