Skip to content

The open alternative to Salesforce, designed for AI.

3.3 KB
Raw Blame
import { PAGE_LAYOUT_WIDGET_DND_TYPE } from '@/page-layout/constants/PageLayoutWidgetDndType';
import { PageLayoutWidgetViewportContext } from '@/page-layout/contexts/PageLayoutWidgetViewportContext';
import { type PageLayoutWidget } from '@/page-layout/types/PageLayoutWidget';
import { type PageLayoutWidgetDragData } from '@/page-layout/types/PageLayoutWidgetDragData';
import { WidgetRenderer } from '@/page-layout/widgets/components/WidgetRenderer';
import { DragDropItemDropTarget } from '@/ui/utilities/drag-and-drop/components/DragDropItemDropTarget';
import { DragDropItemSortableCell } from '@/ui/utilities/drag-and-drop/components/DragDropItemSortableCell';
import { type Draggable } from '@dnd-kit/abstract';
import { styled } from '@linaria/react';
import { themeCssVariables } from 'twenty-ui/theme';

const StyledWidgetSlot = styled.div<{
  isInEditMode: boolean;
  shouldShowDivider: boolean;
}>`
  display: flex;
  flex: 0 0 auto;
  flex-direction: column;
  min-height: 0;
  min-width: 0;

  @container tab-viewport (min-height: 0px) {
    &.page-layout-viewport-filling-widget-slot {
      --widget-height: 100%;
      --widget-scroll-overflow: auto;

      height: calc(100cqh - var(--viewport-filling-widget-editor-block-inset));
      overflow: clip;

      .widget {
        overflow: clip;
      }

      .widget-card-header {
        background: var(--record-card-background-color);
        position: sticky;
        top: 0;
        z-index: 3;
      }
    }
  }

  &:not(:last-child) {
    border-bottom: ${({ isInEditMode, shouldShowDivider }) =>
      !isInEditMode && shouldShowDivider
        ? `1px solid ${themeCssVariables.border.color.light}`
        : 'none'};
  }
`;

type PageLayoutVerticalListWidgetSlotProps = {
  canAcceptWidgetDrag: (source: Draggable) => boolean;
  index: number;
  isInEditMode: boolean;
  fillsViewport: boolean;
  shouldShowDivider: boolean;
  tabId: string;
  widget: PageLayoutWidget;
};

export const PageLayoutVerticalListWidgetSlot = ({
  canAcceptWidgetDrag,
  index,
  isInEditMode,
  fillsViewport,
  shouldShowDivider,
  tabId,
  widget,
}: PageLayoutVerticalListWidgetSlotProps) => {
  const widgetDragData: PageLayoutWidgetDragData = {
    type: 'widget',
    widgetId: widget.id,
    widgetType: widget.type,
    widgetPosition: widget.position,
    tabId,
    index,
  };

  return (
    <StyledWidgetSlot
      className={
        fillsViewport ? 'page-layout-viewport-filling-widget-slot' : undefined
      }
      isInEditMode={isInEditMode}
      shouldShowDivider={shouldShowDivider}
    >
      <DragDropItemDropTarget
        index={index}
        droppableId={tabId}
        orientation="horizontal"
        compact
      />
      <DragDropItemSortableCell
        id={widget.id}
        index={index}
        group={tabId}
        data={widgetDragData}
        type={PAGE_LAYOUT_WIDGET_DND_TYPE}
        accept={canAcceptWidgetDrag}
        allowNativeDragWhenDisabled
        disabled={!isInEditMode}
        hasTransition={false}
        highlightWhileDragging={isInEditMode}
        orientation="horizontal"
        fill={fillsViewport}
      >
        <PageLayoutWidgetViewportContext.Provider value={fillsViewport}>
          <WidgetRenderer widget={widget} />
        </PageLayoutWidgetViewportContext.Provider>
      </DragDropItemSortableCell>
    </StyledWidgetSlot>
  );
};