Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

4.2 KB
Raw Blame
import { useActions, useValues } from 'kea'
import type { DragEvent, ReactNode } from 'react'

import { cn } from 'lib/utils/css-classes'

import { biEditorLogic } from 'products/business_intelligence/frontend/biEditorLogic'
import {
    BIShelf,
    BI_FIELD_DRAG_MIME_TYPE,
    BI_SHELF_PILL_DRAG_MIME_TYPE,
    getBIDropTarget,
    parseBIField,
    parseBIShelfPillDragData,
} from 'products/business_intelligence/frontend/biEditorTypes'

function carriesBIDrag(event: DragEvent<HTMLElement>): 'field' | 'pill' | null {
    const types = Array.from(event.dataTransfer.types)
    return types.includes(BI_SHELF_PILL_DRAG_MIME_TYPE)
        ? 'pill'
        : types.includes(BI_FIELD_DRAG_MIME_TYPE)
          ? 'field'
          : null
}

/** Accepts fields from the data pane or the database tree, and pills moved from other shelves. */
export function BIShelfDropTarget({
    shelf,
    index,
    className,
    children,
}: {
    shelf: BIShelf
    index?: number
    className?: string
    children: ReactNode
}): JSX.Element {
    const { activeDropShelf, dragSessionId } = useValues(biEditorLogic)
    const { config } = useValues(biEditorLogic)
    const {
        addFieldToShelf,
        clearActiveDropShelf,
        moveFieldToShelf,
        duplicateField,
        setActiveDropShelf,
        setMeasureShelf,
    } = useActions(biEditorLogic)

    return (
        <div
            className={cn(
                'rounded transition-colors',
                activeDropShelf === shelf && 'bg-accent-highlight-secondary ring-2 ring-inset ring-accent',
                className
            )}
            data-attr={`bi-editor-${shelf}-${index === undefined ? 'shelf' : 'drop-target'}`}
            onDragEnter={(event) => {
                if (carriesBIDrag(event)) {
                    setActiveDropShelf(shelf)
                }
            }}
            onDragOver={(event) => {
                const dragKind = carriesBIDrag(event)
                if (dragKind) {
                    event.preventDefault()
                    event.dataTransfer.dropEffect = dragKind === 'pill' && !event.altKey ? 'move' : 'copy'
                }
            }}
            onDragLeave={(event) => {
                const nextTarget = event.relatedTarget
                if (!nextTarget || !event.currentTarget.contains(nextTarget as Node)) {
                    clearActiveDropShelf(shelf)
                }
            }}
            onDrop={(event) => {
                event.preventDefault()
                event.stopPropagation()
                clearActiveDropShelf(shelf)
                const pill = parseBIShelfPillDragData(event.dataTransfer.getData(BI_SHELF_PILL_DRAG_MIME_TYPE))
                if (pill) {
                    if (pill.dragSessionId !== dragSessionId) {
                        return
                    }
                    if (event.altKey && pill.shelf !== 'filters') {
                        duplicateField(pill.shelf, pill.index, shelf, index)
                        return
                    }
                    if (pill.shelf === 'values' && shelf === 'values' && index !== undefined) {
                        setMeasureShelf(pill.index, config.values[index]?.shelf ?? 'rows')
                    }
                    moveFieldToShelf(pill.shelf, pill.index, shelf, index)
                    return
                }
                const field = parseBIField(event.dataTransfer.getData(BI_FIELD_DRAG_MIME_TYPE))
                if (field) {
                    const target = getBIDropTarget(field, shelf)
                    addFieldToShelf(target.field, target.shelf)
                    if (target.shelf === 'values' && (shelf === 'rows' || shelf === 'columns')) {
                        setMeasureShelf(config.values.length, shelf)
                    } else if (target.shelf === 'values' && index !== undefined) {
                        setMeasureShelf(config.values.length, config.values[index]?.shelf ?? 'rows')
                        moveFieldToShelf('values', config.values.length, 'values', index)
                    } else if (index !== undefined) {
                        moveFieldToShelf(target.shelf, config[target.shelf].length, target.shelf, index)
                    }
                }
            }}
        >
            {children}
        </div>
    )
}