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

2.8 KB
Raw Blame
import { isNil } from '@activepieces/core-utils';
import { useReactFlow, useStore } from '@xyflow/react';
import { useRef, useState } from 'react';

import { useBuilderStateContext } from '../../../builder-hooks';
import { NoteDragOverlayMode } from '../../../state/notes-state';
import {
  useCursorPosition,
  useCursorPositionEffect,
} from '../../cursor-position-context';
import { flowCanvasConsts } from '../../utils/consts';

import { NoteContent } from '.';

const NoteDragOverlay = () => {
  const { cursorPosition } = useCursorPosition();
  const [overlayPosition, setOverlayPosition] =
    useState<typeof cursorPosition>(cursorPosition);
  const canvasElement = useStore((state) => state.domNode);
  const canvasRect = canvasElement?.getBoundingClientRect();
  const [draggedNote, noteDragOverlayMode, addNote, draggedNoteOffset] =
    useBuilderStateContext((state) => [
      state.draggedNote,
      state.noteDragOverlayMode,
      state.addNote,
      state.draggedNoteOffset,
    ]);
  const reactFlow = useReactFlow();
  const containerRef = useRef<HTMLDivElement>(null);

  const nodeSizeWithZoom = {
    width: (draggedNote?.size.width ?? 0) * reactFlow.getZoom(),
    height: (draggedNote?.size.height ?? 0) * reactFlow.getZoom(),
  };

  const offsetX = draggedNoteOffset
    ? draggedNoteOffset.x
    : nodeSizeWithZoom.width / 2;
  const offsetY = draggedNoteOffset
    ? draggedNoteOffset.y
    : nodeSizeWithZoom.height / 2;

  const left = `${overlayPosition.x - offsetX - (canvasRect?.left ?? 0)}px`;
  const top = `${overlayPosition.y - offsetY - (canvasRect?.top ?? 0)}px`;
  useCursorPositionEffect((position) => {
    setOverlayPosition(position);
  });
  const hideOverlay = isNil(draggedNote) || isNil(noteDragOverlayMode);

  if (hideOverlay) {
    return null;
  }
  return (
    <div
      className={'absolute !cursor-grabbing note-drag-overlay'}
      ref={containerRef}
      onClick={() => {
        if (noteDragOverlayMode === NoteDragOverlayMode.CREATE) {
          const rect = containerRef.current?.getBoundingClientRect();
          if (rect) {
            const positionOnCanvas = reactFlow.screenToFlowPosition({
              x: rect.left,
              y: rect.top,
            });
            addNote({
              content: flowCanvasConsts.DEFAULT_NOTE_CONTENT,
              position: positionOnCanvas,
              size: draggedNote.size,
              color: flowCanvasConsts.DEFAULT_NOTE_COLOR,
            });
          }
        }
      }}
      style={{
        left,
        top,
        height: `${draggedNote.size.height}px`,
        width: `${draggedNote.size.width}px`,
        transform: `scale(${reactFlow.getZoom()})`,
        transformOrigin: '0 0',
      }}
    >
      <NoteContent
        isDragging={true}
        note={{
          ...draggedNote,
        }}
      ></NoteContent>
    </div>
  );
};

export default NoteDragOverlay;