Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(builder): notes can be added again and drag previews follow the cursor (#15760)

Ahmad Tash5e98123cdfaf
diff --git a/brain/knowledge/flows-execution/flows.md b/brain/knowledge/flows-execution/flows.md
index 15668fb6638..22020f07470 100644
--- a/brain/knowledge/flows-execution/flows.md
+++ b/brain/knowledge/flows-execution/flows.md
@@ -32,6 +32,7 @@ Flows are the core automation primitive: a versioned directed graph of trigger +
- List filtering: `folderId` uses the string sentinel `"NULL"` for uncategorized; `folderIds` (array) loads all foldered flows in one request.
- Builder is Zustand-sliced (flow/run/canvas/step-form/piece-selector state). Canvas supports vertical (default) and horizontal orientations, and PNG export via a hand-rolled clone-and-rasterize pipeline.
- **`Popover.Trigger asChild` always composes its own open-toggle onto the child's click, even when `open`/`onOpenChange` is externally controlled — a `preventDefault()`-shaped guard is the only thing that stops it.** Radix wires `onClick={composeEventHandlers(props.onClick, context.onOpenToggle)}` on the trigger, and `composeEventHandlers` skips the second handler only if `event.defaultPrevented`. `ApStepCanvasNode` wraps *every* step (trigger and action) in `<PieceSelector openSelectorOnClick={false}>` (`flow-canvas/nodes/step-node/index.tsx`), but that prop only guards the app's own `onClick` — it was `handleStepClick`'s incidental `e.preventDefault()` that actually suppressed Radix's toggle for ordinary steps. PR #14405 ("close trigger piece selector on outside/repeat click") removed that `preventDefault()` and made `pieces-selector/index.tsx`'s `onOpenChange` unconditionally honor `open` for any step id — fixing a real empty-trigger close/repeat-click race, but with nothing scoping the new open-branch to the empty trigger specifically. Regression window: 2026-07-28 (#14405) to 2026-08-11 — any plain click on a *configured* step's body reopened its own piece selector, not just the empty trigger (case 1) or an explicit Replace (case 2, which sets the state directly from the context menu and never touches this trigger). Fix: gate the `onOpenChange` open-branch on `isForEmptyTrigger || openSelectorOnClick` instead of honoring every toggle — Radix's own attempt to open is simply ignored (state doesn't change, so the controlled `open` prop stays false) when neither condition holds. Confirmed both the break and the fix by reproducing the exact `Popover.Root`/`Trigger asChild` wiring against the real installed `radix-ui` package in a throwaway vitest — not just static reading. The general lesson (Radix always attempts the toggle; controlled mode only means *you* decide whether to honor it) applies to every controlled Radix `Trigger` in this codebase, not just this one.
+- **Anything drawn in canvas coordinates must measure the canvas element, never infer its offset from app chrome.** The note and step drag previews are `position: absolute` inside the canvas and used to subtract `#project-sidebar`'s width plus a hardcoded 60px header; when the chat-first rail (#15005) replaced that sidebar the id vanished, the width read 0, and Add note silently stopped working — the preview drew a sidebar-width right of the cursor, so the placing click always missed it. Take the origin from xyflow's `useStore((s) => s.domNode)?.getBoundingClientRect()`. `position: fixed` is no way out: an ancestor carries `will-change: transform`, which makes it the containing block.
- **A stray DRAFT row makes a published flow look blank.** "Latest version" is resolved by `getFlowVersionOrThrow({ versionId: undefined })`, which is just `ORDER BY created DESC` with no state filter — so any DRAFT created after the LOCKED version *is* what the builder opens. This is why a half-created draft is a user-visible outage, not DB litter: recovering needs the row deleted or a `USE_AS_DRAFT`. Editing a published flow used to commit the empty DRAFT and import the locked content into it as two separate writes, so an import failure (field case: `RangeError: Maximum call stack size exceeded` on deeply nested flows — the recursive `sanitizeObjectForPostgresql` before the save is the prime suspect) left the flow rendering empty forever. GIT-1590 / Pylon #5225; the `RangeError` itself is still open as GIT-1593.
- **Draft creation is now atomic, in two different ways — know which one you're in.** `createNewDraftIfVersionIsPublished` runs `createEmptyVersion` + the IMPORT_FLOW loop in one `transaction()`, threading the `entityManager` down through `applyOperation` to the `updateLastModified` side effect. The *user's* operation deliberately stays outside that transaction (it would hold Postgres open across `prepareRequest` piece-metadata fetches and non-rollbackable file/webhook side effects) and is instead undone by a compensating `delete` of the freshly created draft. `flowService.create` wraps the flow row + first empty version the same way, so a failure can't leave a zero-version, unopenable flow.
- **`flowVersionSideEffects.preApplyOperation` writes on the default connection, so it escapes any caller transaction.** `handleSampleDataDeletion` and `handleUpdateTriggerWebhookSimulation` take no `entityManager`; a write they make from inside a `transaction()` survives its rollback. They early-return for `IMPORT_FLOW`/`UPDATE_SAMPLE_DATA_INFO`, so the transactional path above is safe today — but adding an operation type to it silently reintroduces partial commits. `updateLastModified` sits outside that swallow-all catch on purpose: a swallowed statement failure inside a transaction poisons it and resurfaces as a confusing "transaction is aborted" error on the next statement.
diff --git a/packages/web/src/app/builder/flow-canvas/nodes/note-node/note-drag-overlay.tsx b/packages/web/src/app/builder/flow-canvas/nodes/note-node/note-drag-overlay.tsx
index 2a20f5a20ea..63672273b9c 100644
--- a/packages/web/src/app/builder/flow-canvas/nodes/note-node/note-drag-overlay.tsx
+++ b/packages/web/src/app/builder/flow-canvas/nodes/note-node/note-drag-overlay.tsx
@@ -1,9 +1,7 @@
import { isNil } from '@activepieces/core-utils';
-import { useReactFlow } from '@xyflow/react';
+import { useReactFlow, useStore } from '@xyflow/react';
import { useRef, useState } from 'react';
-import { SIDEBAR_ID } from '@/app/components/sidebar/dashboard';
-
import { useBuilderStateContext } from '../../../builder-hooks';
import { NoteDragOverlayMode } from '../../../state/notes-state';
import {
@@ -18,7 +16,8 @@ const NoteDragOverlay = () => {
const { cursorPosition } = useCursorPosition();
const [overlayPosition, setOverlayPosition] =
useState<typeof cursorPosition>(cursorPosition);
- const sidebar = document.getElementById(SIDEBAR_ID);
+ const canvasElement = useStore((state) => state.domNode);
+ const canvasRect = canvasElement?.getBoundingClientRect();
const [draggedNote, noteDragOverlayMode, addNote, draggedNoteOffset] =
useBuilderStateContext((state) => [
state.draggedNote,
@@ -28,7 +27,6 @@ const NoteDragOverlay = () => {
]);
const reactFlow = useReactFlow();
const containerRef = useRef<HTMLDivElement>(null);
- const sidebarWidth = sidebar?.clientWidth ?? 0;
const nodeSizeWithZoom = {
width: (draggedNote?.size.width ?? 0) * reactFlow.getZoom(),
@@ -42,10 +40,8 @@ const NoteDragOverlay = () => {
? draggedNoteOffset.y
: nodeSizeWithZoom.height / 2;
- const left = `${overlayPosition.x - offsetX - sidebarWidth}px`;
- const top = `${
- overlayPosition.y - offsetY - flowCanvasConsts.BUILDER_HEADER_HEIGHT
- }px`;
+ const left = `${overlayPosition.x - offsetX - (canvasRect?.left ?? 0)}px`;
+ const top = `${overlayPosition.y - offsetY - (canvasRect?.top ?? 0)}px`;
useCursorPositionEffect((position) => {
setOverlayPosition(position);
});
diff --git a/packages/web/src/app/builder/flow-canvas/nodes/step-node/step-drag-overlay.tsx b/packages/web/src/app/builder/flow-canvas/nodes/step-node/step-drag-overlay.tsx
index 24aee8678af..bd09d14790f 100644
--- a/packages/web/src/app/builder/flow-canvas/nodes/step-node/step-drag-overlay.tsx
+++ b/packages/web/src/app/builder/flow-canvas/nodes/step-node/step-drag-overlay.tsx
@@ -1,8 +1,8 @@
import { FlowAction, FlowTrigger } from '@activepieces/shared';
+import { useStore } from '@xyflow/react';
import { t } from 'i18next';
import { useState } from 'react';
-import { SIDEBAR_ID } from '@/app/components/sidebar/dashboard';
import { stepsHooks } from '@/features/pieces';
import {
@@ -15,15 +15,17 @@ const StepDragOverlay = ({ step }: { step: FlowAction | FlowTrigger }) => {
const { cursorPosition } = useCursorPosition();
const [overlayPosition, setOverlayPosition] =
useState<typeof cursorPosition>(cursorPosition);
- const sidebar = document.getElementById(SIDEBAR_ID);
- const sidebarWidth = sidebar?.clientWidth ?? 0;
+ const canvasElement = useStore((state) => state.domNode);
+ const canvasRect = canvasElement?.getBoundingClientRect();
const left = `${
overlayPosition.x -
flowCanvasConsts.STEP_DRAG_OVERLAY_WIDTH / 2 -
- sidebarWidth
+ (canvasRect?.left ?? 0)
}px`;
const top = `${
- overlayPosition.y - flowCanvasConsts.STEP_DRAG_OVERLAY_HEIGHT - 20
+ overlayPosition.y -
+ flowCanvasConsts.STEP_DRAG_OVERLAY_HEIGHT / 2 -
+ (canvasRect?.top ?? 0)
}px`;
const { stepMetadata } = stepsHooks.useStepMetadata({
step,
diff --git a/packages/web/src/app/components/sidebar/dashboard/index.tsx b/packages/web/src/app/components/sidebar/dashboard/index.tsx
deleted file mode 100644
index 93c84e1549c..00000000000
--- a/packages/web/src/app/components/sidebar/dashboard/index.tsx
+++ /dev/null
@@ -1 +0,0 @@
-export const SIDEBAR_ID = 'project-sidebar';
diff --git a/packages/web/test/app/builder/flow-canvas/nodes/note-node/note-drag-overlay.test.tsx b/packages/web/test/app/builder/flow-canvas/nodes/note-node/note-drag-overlay.test.tsx
new file mode 100644
index 00000000000..518f87b58ec
--- /dev/null
+++ b/packages/web/test/app/builder/flow-canvas/nodes/note-node/note-drag-overlay.test.tsx
@@ -0,0 +1,135 @@
+/**
+ * @vitest-environment jsdom
+ */
+/* eslint-disable testing-library/no-unnecessary-act */
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+import NoteDragOverlay from '@/app/builder/flow-canvas/nodes/note-node/note-drag-overlay';
+import { NoteDragOverlayMode } from '@/app/builder/state/notes-state';
+
+declare global {
+ // eslint-disable-next-line no-var
+ var IS_REACT_ACT_ENVIRONMENT: boolean;
+}
+
+globalThis.IS_REACT_ACT_ENVIRONMENT = true;
+
+const canvasOrigin = vi.hoisted(() => ({ left: 255, top: 68 }));
+
+const builderState = vi.hoisted((): { current: DraggedNoteSlice } => ({
+ current: {
+ draggedNote: null,
+ noteDragOverlayMode: null,
+ addNote: () => undefined,
+ draggedNoteOffset: null,
+ },
+}));
+
+vi.mock('@xyflow/react', () => ({
+ useReactFlow: () => ({
+ getZoom: () => 1,
+ screenToFlowPosition: (position: Point) => position,
+ }),
+ useStore: <T,>(selector: (state: CanvasState) => T) =>
+ selector({ domNode: { getBoundingClientRect: () => canvasOrigin } }),
+}));
+
+vi.mock('@/app/builder/builder-hooks', () => ({
+ useBuilderStateContext: <T,>(selector: (state: DraggedNoteSlice) => T) =>
+ selector(builderState.current),
+}));
+
+vi.mock('@/app/builder/state/notes-state', () => ({
+ NoteDragOverlayMode: { CREATE: 'create', MOVE: 'move' },
+}));
+
+vi.mock('@/app/builder/flow-canvas/nodes/note-node', () => ({
+ NoteContent: () => null,
+}));
+
+vi.mock('@/app/builder/flow-canvas/utils/consts', () => ({
+ flowCanvasConsts: { DEFAULT_NOTE_CONTENT: '', DEFAULT_NOTE_COLOR: 'blue' },
+}));
+
+let container: HTMLDivElement | null = null;
+let root: Root | null = null;
+
+describe('NoteDragOverlay', () => {
+ afterEach(() => {
+ act(() => root?.unmount());
+ container?.remove();
+ container = null;
+ root = null;
+ builderState.current = {
+ draggedNote: null,
+ noteDragOverlayMode: null,
+ addNote: () => undefined,
+ draggedNoteOffset: null,
+ };
+ });
+
+ it('centres the new-note preview on the cursor, measured from the canvas element', () => {
+ mountOverlay();
+ movePointer({ x: 600, y: 700 });
+ builderState.current = {
+ ...builderState.current,
+ draggedNote: { size: { width: 150, height: 150 } },
+ noteDragOverlayMode: NoteDragOverlayMode.CREATE,
+ };
+ renderOverlay();
+
+ const overlay = container?.querySelector<HTMLElement>('.note-drag-overlay');
+ expect(overlay?.style.left).toBe(`${600 - 150 / 2 - canvasOrigin.left}px`);
+ expect(overlay?.style.top).toBe(`${700 - 150 / 2 - canvasOrigin.top}px`);
+ });
+
+ it('keeps the grabbed point of a moved note under the cursor', () => {
+ mountOverlay();
+ movePointer({ x: 600, y: 700 });
+ builderState.current = {
+ ...builderState.current,
+ draggedNote: { size: { width: 275, height: 175 } },
+ noteDragOverlayMode: NoteDragOverlayMode.MOVE,
+ draggedNoteOffset: { x: 40, y: 125 },
+ };
+ renderOverlay();
+
+ const overlay = container?.querySelector<HTMLElement>('.note-drag-overlay');
+ expect(overlay?.style.left).toBe(`${600 - 40 - canvasOrigin.left}px`);
+ expect(overlay?.style.top).toBe(`${700 - 125 - canvasOrigin.top}px`);
+ });
+});
+
+function mountOverlay(): void {
+ container = document.createElement('div');
+ document.body.appendChild(container);
+ root = createRoot(container);
+ renderOverlay();
+}
+
+function renderOverlay(): void {
+ act(() => root?.render(<NoteDragOverlay />));
+}
+
+function movePointer({ x, y }: Point): void {
+ act(() => {
+ window.dispatchEvent(
+ new MouseEvent('pointermove', { clientX: x, clientY: y }),
+ );
+ });
+}
+
+type Point = { x: number; y: number };
+
+type CanvasState = {
+ domNode: { getBoundingClientRect: () => { left: number; top: number } };
+};
+
+type DraggedNoteSlice = {
+ draggedNote: { size: { width: number; height: number } } | null;
+ noteDragOverlayMode: NoteDragOverlayMode | null;
+ addNote: () => void;
+ draggedNoteOffset: Point | null;
+};
diff --git a/packages/web/test/app/builder/flow-canvas/nodes/step-node/step-drag-overlay.test.tsx b/packages/web/test/app/builder/flow-canvas/nodes/step-node/step-drag-overlay.test.tsx
new file mode 100644
index 00000000000..4a8959643b7
--- /dev/null
+++ b/packages/web/test/app/builder/flow-canvas/nodes/step-node/step-drag-overlay.test.tsx
@@ -0,0 +1,91 @@
+/**
+ * @vitest-environment jsdom
+ */
+/* eslint-disable testing-library/no-unnecessary-act */
+import { FlowAction, FlowActionType } from '@activepieces/shared';
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+import StepDragOverlay from '@/app/builder/flow-canvas/nodes/step-node/step-drag-overlay';
+
+declare global {
+ // eslint-disable-next-line no-var
+ var IS_REACT_ACT_ENVIRONMENT: boolean;
+}
+
+globalThis.IS_REACT_ACT_ENVIRONMENT = true;
+
+const canvasOrigin = vi.hoisted(() => ({ left: 255, top: 68 }));
+
+vi.mock('@xyflow/react', () => ({
+ useStore: <T,>(selector: (state: CanvasState) => T) =>
+ selector({ domNode: { getBoundingClientRect: () => canvasOrigin } }),
+}));
+
+vi.mock('@/features/pieces', () => ({
+ stepsHooks: { useStepMetadata: () => ({ stepMetadata: undefined }) },
+}));
+
+vi.mock('@/app/builder/flow-canvas/utils/consts', () => ({
+ flowCanvasConsts: {
+ STEP_DRAG_OVERLAY_WIDTH: 75,
+ STEP_DRAG_OVERLAY_HEIGHT: 75,
+ },
+}));
+
+vi.mock('i18next', () => ({ t: (key: string) => key }));
+
+let container: HTMLDivElement | null = null;
+let root: Root | null = null;
+
+describe('StepDragOverlay', () => {
+ afterEach(() => {
+ act(() => root?.unmount());
+ container?.remove();
+ container = null;
+ root = null;
+ });
+
+ it('centres the step preview on the cursor, measured from the canvas element', () => {
+ container = document.createElement('div');
+ document.body.appendChild(container);
+ root = createRoot(container);
+ act(() => root?.render(<StepDragOverlay step={buildCodeStep()} />));
+
+ act(() => {
+ window.dispatchEvent(
+ new MouseEvent('pointermove', { clientX: 509, clientY: 591 }),
+ );
+ });
+
+ const overlay = container.querySelector<HTMLElement>(
+ '#dragged-step-overlay',
+ );
+ expect(overlay?.style.left).toBe(`${509 - 75 / 2 - canvasOrigin.left}px`);
+ expect(overlay?.style.top).toBe(`${591 - 75 / 2 - canvasOrigin.top}px`);
+ });
+});
+
+function buildCodeStep(): FlowAction {
+ return {
+ name: 'step_1',
+ valid: true,
+ displayName: 'Step 1',
+ skip: false,
+ lastUpdatedDate: '2026-09-24T00:00:00.000Z',
+ type: FlowActionType.CODE,
+ settings: {
+ sourceCode: { code: '', packageJson: '{}' },
+ input: {},
+ errorHandlingOptions: {
+ continueOnFailure: { value: false },
+ retryOnFailure: { value: false },
+ },
+ },
+ };
+}
+
+type CanvasState = {
+ domNode: { getBoundingClientRect: () => { left: number; top: number } };
+};