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(webv2): give the template editor one error surface each
joshistoast8ca739ab361d
diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplateEditor.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplateEditor.tsxindex c7b1220ef2..63774059e5 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplateEditor.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplateEditor.tsx@@ -6,7 +6,6 @@ import type { ChangeEvent } from 'react';import { HStack, Image, Input, Stack, Text } from '@chakra-ui/react';import { PROMPT_TEMPLATE_PLACEHOLDER } from '@features/generation/core/promptTemplates';-import { fetchPromptTemplateImage } from '@features/generation/data/promptTemplates';import { useGenerationUi } from '@features/generation/ui/GenerationUiContext';import { PromptPanelHeader } from '@features/generation/ui/promptFields/PromptPanelHeader';import { PromptTextarea } from '@features/generation/ui/promptFields/PromptTextarea';@@ -109,7 +108,7 @@ export const PromptTemplateEditor = ({return;}- void fetchPromptTemplateImage(imageUrl).then((image) => {+ void catalog.fetchImage(imageUrl).then((image) => {if (image) {setDraft((current) =>current.isExistingImageSettled ? current : { ...current, image, isExistingImageSettled: true }@@ -133,17 +132,24 @@ export const PromptTemplateEditor = ({setIsSaving(true);setError(null);- try {- const saved = template ? await catalog.update(template.id, nextDraft) : await catalog.create(nextDraft);+ let saved: PromptTemplateRecord;- onSaved(saved);+ try {+ saved = template ? await catalog.update(template.id, nextDraft) : await catalog.create(nextDraft);} catch (caught) {// `ApiError.message` is the raw response body, so the backend's own// explanation only reads properly once it is unwrapped.setError(getApiErrorMessage(caught, t('widgets.generate.promptTemplates.couldNotSave')));+ return;} finally {setIsSaving(false);}++ // Deliberately outside the `try`. Handing the saved record back is the+ // caller's business, and a failure there is not a failure to save — inside,+ // it was reported as "could not save this template" about a template that+ // had just been saved. It reaches the caller's own reporting instead.+ onSaved(saved);}, [catalog, draft, onSaved, t, template, trimmedName]);const insertPlaceholder = useCallback(diff --git a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplates.browser.test.tsx b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplates.browser.test.tsxindex 2af8dce0af..d08f7483f4 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplates.browser.test.tsx+++ b/invokeai/frontend/webv2/src/features/generation/ui/promptFields/PromptTemplates.browser.test.tsx@@ -2,7 +2,6 @@ import type { PromptTemplateRecord } from '@features/generation/data/promptTemplimport type { PromptTemplateCatalog } from '@features/generation/ui/usePromptTemplates';import { ChakraProvider } from '@chakra-ui/react';-import { fetchPromptTemplateImage } from '@features/generation/data/promptTemplates';import { PromptTemplateEditor } from '@features/generation/ui/promptFields/PromptTemplateEditor';import { PromptTemplatesPanel } from '@features/generation/ui/promptFields/PromptTemplatesPanel';import { system } from '@theme/system';@@ -23,14 +22,6 @@ vi.mock('@features/generation/ui/GenerationUiContext', async (importOriginal) =>}),}));-// The editor loads an existing preview image back so saving does not drop it.-// Stubbed so a template with an image is driven from the test rather than the-// network, and so a regression shows up as a failed assertion.-vi.mock('@features/generation/data/promptTemplates', async (importOriginal) => ({- ...(await importOriginal<object>()),- fetchPromptTemplateImage: vi.fn().mockResolvedValue(null),-}));-// The subtree of `public/locales/en.json` these panels read, copied verbatim so// the assertions below can name the strings a user actually sees. Importing the// catalogue itself is not an option — Vite does not serve `public/` to modules.@@ -131,6 +122,7 @@ const createCatalog = (overrides: Partial<PromptTemplateCatalog> = {}): PromptTecreate: vi.fn(),defaultTemplates: [defaultTemplate],exportCsv: vi.fn(),+ fetchImage: vi.fn().mockResolvedValue(null),importFile: vi.fn(),isLoading: false,remove: vi.fn(),@@ -353,10 +345,11 @@ describe('the prompt template editor', () => {const catalog = createCatalog();let resolveImage: (image: Blob) => void = () => {};- vi.mocked(fetchPromptTemplateImage).mockReturnValueOnce(- new Promise<Blob | null>((resolve) => {- resolveImage = resolve;- })+ catalog.fetchImage = vi.fn(+ () =>+ new Promise<Blob | null>((resolve) => {+ resolveImage = resolve;+ }));await render(diff --git a/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.ts b/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.tsindex aaa8b0e96d..6ed8063dbb 100644--- a/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.ts+++ b/invokeai/frontend/webv2/src/features/generation/ui/usePromptTemplates.ts@@ -4,6 +4,7 @@ import {createPromptTemplate,deletePromptTemplate,exportPromptTemplates,+ fetchPromptTemplateImage,importPromptTemplates,invalidatePromptTemplates,promptTemplatesQueryOptions,@@ -27,6 +28,15 @@ export interface PromptTemplateCatalog {remove: (id: string) => Promise<void>;importFile: (file: File) => Promise<void>;exportCsv: () => Promise<Blob>;+ /**+ * The preview image behind a record's `imageUrl`, or null.+ *+ * On the port rather than imported straight from `data/` by the one component+ * that needs it: everything else here is injectable, and a single direct+ * import made the editor's tests mock a module to stand in for a dependency+ * they were otherwise handed.+ */+ fetchImage: (imageUrl: string) => Promise<Blob | null>;}/**@@ -77,6 +87,7 @@ export const usePromptTemplates = ({ isEnabled = true }: { isEnabled?: boolean }create,defaultTemplates,exportCsv: exportPromptTemplates,+ fetchImage: fetchPromptTemplateImage,importFile,isLoading: query.isPending,remove,