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(webv2): keep the first graph preview from the library open

Josh Corbett653e4ec6d0b6
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowLibraryDialog.browser.test.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowLibraryDialog.browser.test.tsx
index 0b005adfe9..12225958be 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowLibraryDialog.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowLibraryDialog.browser.test.tsx
@@ -918,6 +918,17 @@ describe('WorkflowLibraryDialog', () => {
expect(preview?.textContent).toContain('integer');
});
+ it('commits the first preview in the click that opens it once the library has loaded the dialog', async () => {
+ await openWith(withSnapshot({ entries: [PORTRAIT, PREVIEW_FIXTURE] }));
+ await act(() => card('wf-preview-fixture')?.click());
+ await vi.dynamicImportSettled();
+
+ // A Suspense reveal here lets StrictMode replay the open dialog's effects after its focus trap started.
+ act(() => (buttonWithText('Preview graph') as HTMLButtonElement).click());
+
+ expect(document.querySelector('[data-preview-dialog]')?.getAttribute('data-preview-open')).toBe('true');
+ });
+
it('disables the Preview action for an entry whose enrichment is not ready', async () => {
await openWith(LOADED_SNAPSHOT);
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowLibraryDialog.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowLibraryDialog.tsx
index 291b50eac1..5930e4800a 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowLibraryDialog.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowLibraryDialog.tsx
@@ -20,12 +20,16 @@ import {
} from '@features/workflow/ui/workflowUiStore';
import { useMountEffect } from '@platform/react/useMountEffect';
import { CloseButton, SegmentTabs, segmentTabsPanelId, segmentTabsTabId } from '@platform/ui';
-import { lazy, Suspense, useCallback, useId, useMemo, useRef, useState } from 'react';
+import { Suspense, useCallback, useId, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import type { WorkflowCardMenuAnchor } from './WorkflowLibraryCard';
-import { buildLibraryGraphPreviewSource } from './libraryPreviewSource';
+import {
+ buildLibraryGraphPreviewSource,
+ DeferredGraphPreviewDialog,
+ preloadGraphPreview,
+} from './libraryPreviewSource';
import { ProjectWorkflowsView } from './ProjectWorkflowsView';
import {
type OpenLibraryWorkflowMode,
@@ -37,13 +41,6 @@ import { WorkflowLibraryGrid } from './WorkflowLibraryGrid';
import { WorkflowLibraryTagChips } from './WorkflowLibraryTagChips';
import { useWorkflowLibraryMissingCounts } from './WorkflowRequirementsList';
-/** Load graph preview only on request to keep xyflow outside the library dialog's initial chunk. */
-const LazyGraphPreviewDialog = lazy(() =>
- import('@features/workflow/ui/graph-preview/GraphPreviewDialog').then((module) => ({
- default: module.GraphPreviewDialog,
- }))
-);
-
const SEARCH_DEBOUNCE_MS = 300;
const TAB_ITEMS: ReadonlyArray<{ labelKey: string; value: WorkflowLibraryTab }> = [
@@ -315,6 +312,7 @@ export const WorkflowLibraryDialog = ({
<Dialog.Backdrop />
<Dialog.Positioner>
<Dialog.Content
+ ref={preloadGraphPreview}
aria-busy={isLoadPending}
h="80vh"
maxH="80vh"
@@ -441,7 +439,7 @@ export const WorkflowLibraryDialog = ({
</Dialog.Root>
{previewRequest && previewSource ? (
<Suspense fallback={null}>
- <LazyGraphPreviewDialog
+ <DeferredGraphPreviewDialog
graphId={previewGraphId}
hideInvoke
isOpen={isPreviewOpen}
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowPublicationHost.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowPublicationHost.tsx
index 947d2e4992..1d6028712c 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowPublicationHost.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/library/WorkflowPublicationHost.tsx
@@ -18,10 +18,14 @@ import { getApiErrorMessage } from '@platform/transport/http';
import { Button, CloseButton } from '@platform/ui/Button';
import { ConfirmDialog } from '@platform/ui/ConfirmDialog';
import { Field } from '@platform/ui/Field';
-import { lazy, Suspense, useCallback, useMemo, useRef, useState } from 'react';
+import { Suspense, useCallback, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
-import { buildLibraryGraphPreviewSource } from './libraryPreviewSource';
+import {
+ buildLibraryGraphPreviewSource,
+ DeferredGraphPreviewDialog,
+ preloadGraphPreview,
+} from './libraryPreviewSource';
import { isUpdatableSource } from './projectWorkflowEntries';
import { formatRelativeTime } from './relativeTime';
import {
@@ -32,13 +36,6 @@ import {
type WorkflowPublicationFailure,
} from './useWorkflowPublication';
-/** Load graph preview only on request to keep xyflow outside the publication dialogs' chunk. */
-const LazyGraphPreviewDialog = lazy(() =>
- import('@features/workflow/ui/graph-preview/GraphPreviewDialog').then((module) => ({
- default: module.GraphPreviewDialog,
- }))
-);
-
type FailedResult = WorkflowPublicationFailure;
/**
@@ -489,7 +486,7 @@ export const WorkflowPublicationHost = () => {
<Portal>
<Dialog.Backdrop />
<Dialog.Positioner>
- <Dialog.Content data-workflow-review-dialog>
+ <Dialog.Content ref={preloadGraphPreview} data-workflow-review-dialog>
<Dialog.Header>
<Dialog.Title>{t('workflowLibrary.reviewTitle')}</Dialog.Title>
</Dialog.Header>
@@ -529,7 +526,7 @@ export const WorkflowPublicationHost = () => {
{activeStage.kind === 'review' && activeStage.record && reviewPreviewSource ? (
<Suspense fallback={null}>
- <LazyGraphPreviewDialog
+ <DeferredGraphPreviewDialog
graphId={activeStage.record.workflow_id}
hideInvoke
isOpen={activeStage.isPreviewOpen}
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/library/libraryPreviewSource.ts b/invokeai/frontend/webv2/src/features/workflow/ui/library/libraryPreviewSource.ts
index 2e5f513650..2eff06f2b3 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/library/libraryPreviewSource.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/library/libraryPreviewSource.ts
@@ -1,7 +1,50 @@
import type { GraphPreviewSourceState, InvocationTemplates, ProjectGraphState } from '@features/workflow/contracts';
+import type * as GraphPreviewDialogModule from '@features/workflow/ui/graph-preview/GraphPreviewDialog';
import { ForLoopGraphValidationError } from '@features/workflow/core/forLoops';
import { compileProjectGraph } from '@features/workflow/graph';
+import { type ComponentType, createElement, lazy, useState } from 'react';
+
+type GraphPreviewModule = typeof GraphPreviewDialogModule;
+
+let loadedGraphPreview: GraphPreviewModule | null = null;
+let graphPreviewModule: Promise<GraphPreviewModule> | null = null;
+
+/** The preview chunk (xyflow and its stylesheet), kept out of the library and publication chunks until needed. */
+export const loadGraphPreview = (): Promise<GraphPreviewModule> =>
+ (graphPreviewModule ??= import('@features/workflow/ui/graph-preview/GraphPreviewDialog').then(
+ (module) => {
+ loadedGraphPreview = module;
+ return module;
+ },
+ (error: unknown) => {
+ graphPreviewModule = null;
+ throw error;
+ }
+ ));
+
+/** Starts the preview chunk when a dialog that can open the preview mounts. */
+export const preloadGraphPreview = (): void => {
+ void loadGraphPreview().catch(() => {});
+};
+
+/**
+ * Renders the loaded component directly and falls back to `lazy` only before the chunk arrives. A first `lazy` render
+ * always suspends; an open dialog revealed by Suspense gets its effects replayed in StrictMode after its focus trap
+ * started, and the library's trap then took focus back and dismissed the preview.
+ */
+const deferGraphPreview = <Props extends object>(pick: (module: GraphPreviewModule) => ComponentType<Props>) => {
+ const Lazy = lazy(() => loadGraphPreview().then((module) => ({ default: pick(module) })));
+
+ return function DeferredGraphPreview(props: Props) {
+ // Chosen once per mount: switching from `Lazy` to the loaded component would remount it.
+ const [component] = useState(() => (loadedGraphPreview ? pick(loadedGraphPreview) : Lazy));
+
+ return createElement(component, props);
+ };
+};
+
+export const DeferredGraphPreviewDialog = deferGraphPreview((module) => module.GraphPreviewDialog);
/**
* Preview the entry's saved document without active-project destination or live updates; catch malformed cached