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.

2.3 KB
Raw Blame
import { MakeLogicType, actions, kea, listeners, path, reducers } from 'kea'

export type GhostPhase = 'drag' | 'drop' | 'return'

/** The card that follows the pointer while a block is dragged. */
export interface SourceDragGhost {
    label: string
    blockType: string | null
    x: number
    y: number
    tilt: number
    hint: string | null
    phase: GhostPhase
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface canvasSourceDragLogicValues {
    ghost: SourceDragGhost | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface canvasSourceDragLogicActions {
    leaveGhost: (
        ghost: SourceDragGhost,
        duration: number
    ) => {
        duration: number
        ghost: SourceDragGhost
    }
    setGhost: (ghost: SourceDragGhost | null) => {
        ghost: SourceDragGhost | null
    }
}

export type canvasSourceDragLogicType = MakeLogicType<canvasSourceDragLogicValues, canvasSourceDragLogicActions>

/** The drag ghost of the block editor. A drag spans the host and the frame, so its state lives outside both. */
export const canvasSourceDragLogic = kea<canvasSourceDragLogicType>([
    path(['products', 'canvas', 'frontend', 'editing', 'canvasSourceDragLogic']),
    actions({
        setGhost: (ghost: SourceDragGhost | null) => ({ ghost }),
        /** Plays the drop or return exit, then clears the ghost unless a new drag took over. */
        leaveGhost: (ghost: SourceDragGhost, duration: number) => ({ ghost, duration }),
    }),
    reducers({
        ghost: [
            null as SourceDragGhost | null,
            {
                setGhost: (_, { ghost }) => ghost,
                leaveGhost: (_, { ghost }) => ghost,
            },
        ],
    }),
    listeners(({ actions, values, cache }) => ({
        setGhost: ({ ghost }) => {
            if (ghost?.phase === 'drag') {
                cache.disposables.dispose('exit')
            }
        },
        leaveGhost: ({ duration }) => {
            cache.disposables.add(() => {
                const timer = setTimeout(() => {
                    if (values.ghost?.phase !== 'drag') {
                        actions.setGhost(null)
                    }
                }, duration)
                return () => clearTimeout(timer)
            }, 'exit')
        },
    })),
])