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(workflow): mount add node dialog on demand

joshistoastfb07163a9f83
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/editor/AddNodeDialog.browser.test.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/editor/AddNodeDialog.browser.test.tsx
new file mode 100644
index 0000000000..c8642c826e
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/editor/AddNodeDialog.browser.test.tsx
@@ -0,0 +1,54 @@
+import { ChakraProvider } from '@chakra-ui/react';
+import { system } from '@theme/system';
+import { act, StrictMode } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { AddNodeDialog } from './AddNodeDialog';
+
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
+describe('AddNodeDialog lifecycle', () => {
+ let host: HTMLDivElement;
+ let root: Root;
+
+ beforeEach(() => {
+ host = document.createElement('div');
+ document.body.append(host);
+ root = createRoot(host);
+ });
+
+ afterEach(async () => {
+ await act(() => root.unmount());
+ host.remove();
+ });
+
+ it('does not mount the virtualized result content while closed', async () => {
+ const errorSpy = vi.spyOn(console, 'error');
+
+ try {
+ await act(() => {
+ root.render(
+ <StrictMode>
+ <ChakraProvider value={system}>
+ <AddNodeDialog
+ connectionFilter={null}
+ isOpen={false}
+ onAddConnector={vi.fn()}
+ onAddCurrentImage={vi.fn()}
+ onAddNode={vi.fn()}
+ onAddNote={vi.fn()}
+ onOpenChange={vi.fn()}
+ />
+ </ChakraProvider>
+ </StrictMode>
+ );
+ });
+
+ expect(host.querySelector('[aria-label="Node search results"]')).toBeNull();
+ expect(errorSpy.mock.calls.flat().join(' ')).not.toContain('getSnapshot');
+ } finally {
+ errorSpy.mockRestore();
+ }
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/workflow/ui/editor/AddNodeDialog.tsx b/invokeai/frontend/webv2/src/features/workflow/ui/editor/AddNodeDialog.tsx
index 927ea9a511..d023157de2 100644
--- a/invokeai/frontend/webv2/src/features/workflow/ui/editor/AddNodeDialog.tsx
+++ b/invokeai/frontend/webv2/src/features/workflow/ui/editor/AddNodeDialog.tsx
@@ -5,13 +5,12 @@ import { Badge, Box, Dialog, HStack, Icon, Input, Portal, ScrollArea, Stack, Tex
import { useInvocationTemplatesSelector } from '@features/workflow/react';
import { useWorkflowUi } from '@features/workflow/ui/WorkflowUiContext';
import { getCompatibleInputTemplate, getCompatibleOutputTemplate } from '@features/workflow/utility';
+import { useMountEffect } from '@platform/react/useMountEffect';
import { IconButton, Tooltip } from '@platform/ui';
import { ChevronDownIcon, ChevronsDownUpIcon, ChevronsUpDownIcon, HammerIcon } from 'lucide-react';
import {
startTransition,
useCallback,
- useEffect,
- useEffectEvent,
useMemo,
useState,
type ChangeEvent,
@@ -220,7 +219,6 @@ export const AddNodeDialog = ({
onAddNote: () => void;
onOpenChange: (isOpen: boolean) => void;
}) => {
- const { registerModalHotkeyLayer } = useWorkflowUi();
const onDialogOpenChange = useCallback(
(event: { open: boolean }) => {
if (!event.open) {
@@ -230,14 +228,6 @@ export const AddNodeDialog = ({
[onOpenChange]
);
- useEffect(() => {
- if (!isOpen) {
- return;
- }
-
- return registerModalHotkeyLayer('workflow-add-node');
- }, [isOpen, registerModalHotkeyLayer]);
-
return (
<Dialog.Root
lazyMount
@@ -248,14 +238,16 @@ export const AddNodeDialog = ({
unmountOnExit
onOpenChange={onDialogOpenChange}
>
- <AddNodeDialogContent
- connectionFilter={connectionFilter}
- onAddCurrentImage={onAddCurrentImage}
- onAddConnector={onAddConnector}
- onAddNode={onAddNode}
- onAddNote={onAddNote}
- onOpenChange={onOpenChange}
- />
+ {isOpen ? (
+ <AddNodeDialogContent
+ connectionFilter={connectionFilter}
+ onAddCurrentImage={onAddCurrentImage}
+ onAddConnector={onAddConnector}
+ onAddNode={onAddNode}
+ onAddNote={onAddNote}
+ onOpenChange={onOpenChange}
+ />
+ ) : null}
</Dialog.Root>
);
};
@@ -275,6 +267,7 @@ const AddNodeDialogContent = ({
onAddNote: () => void;
onOpenChange: (isOpen: boolean) => void;
}) => {
+ const { registerModalHotkeyLayer } = useWorkflowUi();
const error = useInvocationTemplatesSelector((snapshot) => snapshot.error);
const status = useInvocationTemplatesSelector((snapshot) => snapshot.status);
const templates = useInvocationTemplatesSelector((snapshot) => snapshot.templates);
@@ -282,6 +275,8 @@ const AddNodeDialogContent = ({
const [expandedCategories, setExpandedCategories] = useState<Set<string>>(() => new Set());
const [activeIndex, setActiveIndex] = useState<number | null>(null);
const [scrollElement, setScrollElement] = useState<HTMLDivElement | null>(null);
+
+ useMountEffect(() => registerModalHotkeyLayer('workflow-add-node'));
// While searching, every matching group is force-expanded regardless of the
// manual expand/collapse state, so results are never hidden behind a header.
const isSearching = searchTerm.trim().length > 0;
@@ -414,10 +409,6 @@ const AddNodeDialogContent = ({
initialRect: VIRTUALIZER_INITIAL_RECT,
overscan: 8,
});
- const measureVirtualizer = useEffectEvent(() => {
- virtualizer.measure();
- });
-
const toggleCategory = useCallback((label: string) => {
setExpandedCategories((prev) => {
const next = new Set(prev);
@@ -442,16 +433,6 @@ const AddNodeDialogContent = ({
});
}, [groups]);
- useEffect(() => {
- measureVirtualizer();
- }, [resultRows.length]);
-
- useEffect(() => {
- if (effectiveActiveIndex !== null) {
- virtualizer.scrollToIndex(effectiveActiveIndex, { align: 'auto' });
- }
- }, [effectiveActiveIndex, virtualizer]);
-
const moveActiveIndex = useCallback(
(direction: 1 | -1) => {
setActiveIndex((prev) => {
@@ -459,14 +440,18 @@ const AddNodeDialogContent = ({
return null;
}
- if (prev === null) {
- return direction > 0 ? 0 : resultRows.length - 1;
- }
+ const nextIndex =
+ prev === null
+ ? direction > 0
+ ? 0
+ : resultRows.length - 1
+ : (prev + direction + resultRows.length) % resultRows.length;
- return (prev + direction + resultRows.length) % resultRows.length;
+ virtualizer.scrollToIndex(nextIndex, { align: 'auto' });
+ return nextIndex;
});
},
- [resultRows.length]
+ [resultRows.length, virtualizer]
);
const activateResultRow = useCallback(
diff --git a/invokeai/frontend/webv2/vitest.browser.config.mts b/invokeai/frontend/webv2/vitest.browser.config.mts
index aaef995817..3316f3edf5 100644
--- a/invokeai/frontend/webv2/vitest.browser.config.mts
+++ b/invokeai/frontend/webv2/vitest.browser.config.mts
@@ -10,7 +10,7 @@ export default mergeConfig(
// Hook-bearing dependencies must be prebundled into the initial browser-test
// graph. An optimization reload can otherwise leave the running suite with
// two React instances and produce invalid-hook failures.
- optimizeDeps: { include: ['@dnd-kit/core', '@tanstack/react-query'] },
+ optimizeDeps: { include: ['@dnd-kit/core', '@tanstack/react-query', 'react-hook-tanstack-virtual'] },
test: {
browser: {
enabled: true,