Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

2.0 KB
Raw Blame
import { type EntityData, isEntityDragEvent } from '@entity';
import { createDroppable, useDragDropContext } from '@thisbeyond/solid-dnd';

export type EntityDropCoordinates = {
  clientX: number;
  clientY: number;
};

type CreateEntityDropZoneOptions = {
  droppableId: string;
  /** Called when a soup entity is dropped onto this zone. */
  onDropEntity: (
    entity: EntityData,
    coordinates?: EntityDropCoordinates
  ) => void;
  /** Called while a soup entity is dragged over this zone. */
  onDragEntityMove?: (coordinates: EntityDropCoordinates) => void;
  /** Called when the active entity drag leaves this zone or ends. */
  onDragEntityEnd?: () => void;
};

type EntityDropZone = {
  droppable: ReturnType<typeof createDroppable>;
};

export function createEntityDropZone(
  options: CreateEntityDropZoneOptions
): EntityDropZone {
  const droppable = createDroppable(options.droppableId);

  const [state, { onDragEnd, onDragMove }] = useDragDropContext() ?? [
    undefined,
    { onDragEnd: () => {}, onDragMove: () => {} },
  ];

  const currentCoordinates = (): EntityDropCoordinates | undefined => {
    const coordinates = state?.active.sensor?.coordinates.current;
    if (!coordinates) return undefined;
    return {
      clientX: coordinates.x,
      clientY: coordinates.y,
    };
  };

  const isOverThisDropZone = () =>
    droppable.isActiveDroppable ||
    state?.active.droppable?.id === options.droppableId;

  onDragEnd((event) => {
    if (!isEntityDragEvent(event)) return;
    const data = event.draggable.data;

    options.onDragEntityEnd?.();
    if (!event.droppable) return;
    if (event.droppable.id !== options.droppableId) return;

    const coordinates = currentCoordinates();
    options.onDropEntity(data, coordinates);
  });

  onDragMove((event) => {
    if (!isEntityDragEvent(event) || !isOverThisDropZone()) {
      options.onDragEntityEnd?.();
      return;
    }
    const coordinates = currentCoordinates();
    if (!coordinates) return;
    options.onDragEntityMove?.(coordinates);
  });

  return { droppable };
}