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.

2.3 KB
Raw Blame
import { useDndMonitor, type DragEndEvent, type DragStartEvent } from '@dnd-kit/core';
import { useCallback, useRef } from 'react';

import {
  forwardGalleryBoardDrop,
  isGalleryItemDragData,
  resolveGalleryBoardMove,
  resolveGallerySemanticSearchDrop,
} from './galleryDnd';
import { useGalleryWidget } from './GalleryWidgetContext';

/** Forward item and board drops, and temporarily expose collapsed board targets during item drags. */
export const GalleryBoardDragMonitor = () => {
  const { actions, gallery, itemActions, region } = useGalleryWidget();
  const dragOpenedPanelRef = useRef(false);

  const restoreDisclosure = useCallback(() => {
    if (!dragOpenedPanelRef.current) {
      return;
    }

    dragOpenedPanelRef.current = false;
    actions.updateSettings({ boardPanelCollapsed: true });
  }, [actions]);

  const handleDragStart = useCallback(
    (event: DragStartEvent) => {
      if (!gallery.settings.boardPanelCollapsed || !isGalleryItemDragData(event.active.data.current)) {
        return;
      }

      dragOpenedPanelRef.current = true;
      actions.updateSettings({ boardPanelCollapsed: false });
    },
    [actions, gallery.settings.boardPanelCollapsed]
  );

  const handleDragEnd = useCallback(
    (event: DragEndEvent) => {
      const boardMove = resolveGalleryBoardMove(event.active.data.current, event.over?.data.current, region);

      if (boardMove) {
        void actions.moveBoard(boardMove.boardId, boardMove.projectId, boardMove.label);
        return;
      }

      const semanticDrop = resolveGallerySemanticSearchDrop(event.active.data.current, event.over?.id);

      if (semanticDrop) {
        actions.setSemanticImageQuery({ imageName: semanticDrop.imageName, kind: 'image' });
        restoreDisclosure();
        return;
      }

      forwardGalleryBoardDrop({
        activeData: event.active.data.current,
        loadedItems: gallery.items,
        moveItemsToBoard: (items, boardId) => void itemActions.moveItemsToBoard(items, boardId),
        overData: event.over?.data.current,
      });
      restoreDisclosure();
    },
    [actions, gallery.items, itemActions, region, restoreDisclosure]
  );

  useDndMonitor({
    onDragCancel: restoreDisclosure,
    onDragEnd: handleDragEnd,
    onDragStart: handleDragStart,
  });

  return null;
};