|
| 1 | +import { useMemo } from 'react'; |
| 2 | +import type { BrickViewProps, BrickViewPropsWithModel } from '@/@types/brick.types'; |
| 3 | +import { BrickView } from '@/components/Brick/Brick'; |
| 4 | +import { useConnectionPreviewStore } from '@/stores/connection-preview'; |
| 5 | +import { findNodeAndTower } from '@/stores/workspace'; |
| 6 | +import { useBrickLayoutStore } from '@/stores/brick'; |
| 7 | +import { createBrickModel } from '@/utils/brick-model-factory'; |
| 8 | +import { SCALE_LEVEL_CONFIG } from '@/utils/constants'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Renders a "ghost" footprint (DisconnectShadow) exactly where a brick was just disconnected from. |
| 12 | + * This provides visual feedback to the user, showing them the empty slot where the brick |
| 13 | + * previously lived before they dropped it somewhere else or deleted it. |
| 14 | + * It automatically disappears once the user finishes dragging and drops the brick. |
| 15 | + */ |
| 16 | +export function DisconnectShadowView() { |
| 17 | + const disconnectShadow = useConnectionPreviewStore((state) => state.disconnectShadow); |
| 18 | + |
| 19 | + // We need the exact coordinates of the host brick to render the shadow at the correct location |
| 20 | + const hostCoords = useBrickLayoutStore((state) => |
| 21 | + disconnectShadow ? state.coords[disconnectShadow.hostBrickId] : null, |
| 22 | + ); |
| 23 | + |
| 24 | + // Generate a dummy grey "ghost" model that perfectly matches the shape of the disconnected brick |
| 25 | + const shadowModel = useMemo(() => { |
| 26 | + if (!disconnectShadow || !hostCoords) return null; |
| 27 | + |
| 28 | + const found = findNodeAndTower(disconnectShadow.hostBrickId); |
| 29 | + if (!found) return null; |
| 30 | + const hostModel = found.node.model; |
| 31 | + |
| 32 | + const colors = { background: '#d1d5db', foreground: 'transparent', border: '#9ca3af' }; |
| 33 | + |
| 34 | + if (typeof disconnectShadow.socket === 'number' || disconnectShadow.socket === 'output') { |
| 35 | + const props: BrickViewProps = { |
| 36 | + kind: 'value', |
| 37 | + widget: { type: 'label', text: '' }, |
| 38 | + colorsDefault: colors, |
| 39 | + tooltipText: '', |
| 40 | + scaleLevel: hostModel.scaleLevel, |
| 41 | + }; |
| 42 | + return createBrickModel(props); |
| 43 | + } else { |
| 44 | + const props: BrickViewProps = { |
| 45 | + kind: 'statement', |
| 46 | + widget: { type: 'label', text: '' }, |
| 47 | + colorsDefault: colors, |
| 48 | + hasConnectionPrev: true, |
| 49 | + hasConnectionNext: true, |
| 50 | + tooltipText: '', |
| 51 | + scaleLevel: hostModel.scaleLevel, |
| 52 | + }; |
| 53 | + return createBrickModel(props); |
| 54 | + } |
| 55 | + }, [disconnectShadow, hostCoords]); |
| 56 | + |
| 57 | + // Calculate the precise absolute coordinates on the workspace where the shadow should render. |
| 58 | + // We extract the exact connector bounds (e.g. the notch for 'next') and scale it. |
| 59 | + const snapPosition = useMemo(() => { |
| 60 | + if (!disconnectShadow || !shadowModel || !hostCoords) return null; |
| 61 | + |
| 62 | + const found = findNodeAndTower(disconnectShadow.hostBrickId); |
| 63 | + if (!found) return null; |
| 64 | + |
| 65 | + const hostModel = found.node.model; |
| 66 | + const connectors = hostModel.getConnectorCoords(); |
| 67 | + |
| 68 | + let hostBounds = null; |
| 69 | + if (disconnectShadow.socket === 'next') { |
| 70 | + hostBounds = connectors.next; |
| 71 | + } else if (disconnectShadow.socket === 'nestedNext') { |
| 72 | + hostBounds = connectors.nestedNext; |
| 73 | + // If the host block's cavity collapsed (because we just detached its last child), |
| 74 | + // connectors.nestedNext will be missing. We can synthesize its exact location |
| 75 | + // by shifting the bottom tab to the right by TAIL_INDENT_W (8) + strokeWidth. |
| 76 | + if (!hostBounds && connectors.next) { |
| 77 | + const strokeWidth = hostModel.scaleLevel * 2; |
| 78 | + hostBounds = { |
| 79 | + x: connectors.next.x + 8 + strokeWidth, |
| 80 | + y: connectors.next.y, |
| 81 | + w: connectors.next.w, |
| 82 | + h: connectors.next.h, |
| 83 | + }; |
| 84 | + } |
| 85 | + } else if (typeof disconnectShadow.socket === 'number') { |
| 86 | + hostBounds = connectors.inputs[disconnectShadow.socket]; |
| 87 | + } else if (disconnectShadow.socket === 'output') hostBounds = connectors.output; |
| 88 | + |
| 89 | + const hostScale = SCALE_LEVEL_CONFIG[hostModel.scaleLevel].brickScale; |
| 90 | + const hx = hostCoords.x + (hostBounds ? hostBounds.x * hostScale : 0); |
| 91 | + const hy = hostCoords.y + (hostBounds ? hostBounds.y * hostScale : 0); |
| 92 | + |
| 93 | + const shadowScale = SCALE_LEVEL_CONFIG[shadowModel.scaleLevel].brickScale; |
| 94 | + const shadowConnectors = shadowModel.getConnectorCoords(); |
| 95 | + const shadowBounds = |
| 96 | + typeof disconnectShadow.socket === 'number' || disconnectShadow.socket === 'output' |
| 97 | + ? shadowConnectors.output |
| 98 | + : shadowConnectors.prev; |
| 99 | + |
| 100 | + const dx = shadowBounds ? shadowBounds.x * shadowScale : 0; |
| 101 | + const dy = shadowBounds ? shadowBounds.y * shadowScale : 0; |
| 102 | + |
| 103 | + const strokeWidthOffset = 2 * hostScale; // STROKE_WIDTH = 2 |
| 104 | + let snapX = hx - dx; |
| 105 | + let snapY = hy - dy; |
| 106 | + |
| 107 | + // The layout engine makes strokes abut rather than overlap |
| 108 | + if (typeof disconnectShadow.socket === 'number' || disconnectShadow.socket === 'output') { |
| 109 | + snapX += strokeWidthOffset; |
| 110 | + } else { |
| 111 | + snapY += strokeWidthOffset; |
| 112 | + } |
| 113 | + |
| 114 | + return { |
| 115 | + x: snapX, |
| 116 | + y: snapY, |
| 117 | + }; |
| 118 | + }, [disconnectShadow, hostCoords, shadowModel]); |
| 119 | + |
| 120 | + if (!disconnectShadow || !snapPosition || !shadowModel) return null; |
| 121 | + |
| 122 | + const viewProps = { |
| 123 | + kind: shadowModel.kind, |
| 124 | + model: shadowModel, |
| 125 | + } as unknown as BrickViewPropsWithModel; |
| 126 | + |
| 127 | + return ( |
| 128 | + <div |
| 129 | + data-testid="disconnect-shadow-view" |
| 130 | + className="pointer-events-none absolute top-0 left-0 z-20 opacity-80" |
| 131 | + style={{ |
| 132 | + transform: `translate(${snapPosition.x}px, ${snapPosition.y}px)`, |
| 133 | + }} |
| 134 | + > |
| 135 | + <BrickView {...viewProps} /> |
| 136 | + </div> |
| 137 | + ); |
| 138 | +} |
0 commit comments