Skip to content

The open alternative to Salesforce, designed for AI.

2.4 KB
Raw Blame
import { type Draggable } from '@dnd-kit/abstract';
import { pointerIntersection } from '@dnd-kit/collision';
import { useDroppable } from '@dnd-kit/react';
import { styled } from '@linaria/react';
import { type ReactNode, useCallback } from 'react';
import { themeCssVariables } from 'twenty-ui/theme';

import { PAGE_LAYOUT_TAB_DROP_TARGET_DATA_ATTRIBUTE } from '@/page-layout/constants/PageLayoutTabDropTargetDataAttribute';
import { pageLayoutGridDragHoveredTabIdComponentState } from '@/page-layout/states/pageLayoutGridDragHoveredTabIdComponentState';
import { type PageLayoutWidget } from '@/page-layout/types/PageLayoutWidget';
import { type PageLayoutTabWidgetDropData } from '@/page-layout/types/PageLayoutTabWidgetDropData';
import { canVerticalListAcceptWidgetDrag } from '@/page-layout/utils/canVerticalListAcceptWidgetDrag';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';

const StyledDropTarget = styled.div<{ isActive: boolean }>`
  border-radius: ${themeCssVariables.border.radius.sm};
  display: flex;
  outline: ${({ isActive }) =>
    isActive ? `1px solid ${themeCssVariables.color.blue}` : 'none'};
  outline-offset: -1px;
  z-index: ${({ isActive }) => (isActive ? 1 : 'auto')};
`;

type PageLayoutTabWidgetDropTargetProps = {
  tabId: string;
  destinationWidgets: PageLayoutWidget[];
  children: ReactNode;
};

export const PageLayoutTabWidgetDropTarget = ({
  tabId,
  destinationWidgets,
  children,
}: PageLayoutTabWidgetDropTargetProps) => {
  const data: PageLayoutTabWidgetDropData = {
    type: 'tab-widget-drop',
    tabId,
  };

  const canAcceptWidgetDrag = useCallback(
    (source: Draggable) =>
      canVerticalListAcceptWidgetDrag({ destinationWidgets, source }),
    [destinationWidgets],
  );

  const { ref, isDropTarget } = useDroppable({
    id: `page-layout-tab-widget-drop-${tabId}`,
    accept: canAcceptWidgetDrag,
    collisionDetector: pointerIntersection,
    data,
  });

  // Grid drags bypass dnd-kit, so their hover highlight comes from pointer hit-testing.
  const pageLayoutGridDragHoveredTabId = useAtomComponentStateValue(
    pageLayoutGridDragHoveredTabIdComponentState,
  );

  const isActive = isDropTarget || pageLayoutGridDragHoveredTabId === tabId;

  return (
    <StyledDropTarget
      ref={ref}
      isActive={isActive}
      {...{ [PAGE_LAYOUT_TAB_DROP_TARGET_DATA_ATTRIBUTE]: tabId }}
    >
      {children}
    </StyledDropTarget>
  );
};