diff --git a/src/app/(frontend)/styles.css b/src/app/(frontend)/styles.css index 352e934d..efd42b99 100644 --- a/src/app/(frontend)/styles.css +++ b/src/app/(frontend)/styles.css @@ -4159,6 +4159,19 @@ .mission-chain-canvas .react-flow__controls button svg { fill: currentColor; } +/* Endpoint grabbers: xyflow always renders the circles (transparent by + default) so the grab affordance exists, but they should only paint while + their own edge is hovered or being grabbed. Drag one off into empty space + to unlink, or onto another card to rewire (see the chart component). */ +.mission-chain-canvas .react-flow__edge:hover .react-flow__edgeupdater, +.mission-chain-canvas .react-flow__edge.updating .react-flow__edgeupdater { + fill: #0e7490; + stroke: rgba(255, 255, 255, 0.75); + stroke-width: 1px; +} +.mission-chain-canvas .react-flow__edgeupdater:hover { + fill: #22d3ee; +} .mission-chain-canvas .react-flow__attribution { background: transparent; } diff --git a/src/components/frontend/intelligence/MissionCommandChainChart.tsx b/src/components/frontend/intelligence/MissionCommandChainChart.tsx index 435eff85..f6b2ace1 100644 --- a/src/components/frontend/intelligence/MissionCommandChainChart.tsx +++ b/src/components/frontend/intelligence/MissionCommandChainChart.tsx @@ -14,6 +14,7 @@ import { ReactFlowProvider, getSmoothStepPath, useReactFlow, + ConnectionMode, type Edge, type EdgeProps, type EdgeTypes, @@ -187,6 +188,21 @@ export interface MissionCommandChainChartProps { editable: boolean; onAddLink: (supervisorId: number, subordinateId: number) => void; onRemoveLink: (supervisorId: number, subordinateId: number) => void; + /** + * An existing edge's endpoint was dragged onto another card: move the link. + * Validation runs against the draft minus the old pair. + */ + onReconnectLink?: ( + prevSupervisorId: number, + prevSubordinateId: number, + nextSupervisorId: number, + nextSubordinateId: number, + ) => void; +} + +function parseEdgeId(edgeId: string): [number, number] | null { + const m = edgeId.match(/^(\d+)->(\d+)$/); + return m ? [Number(m[1]), Number(m[2])] : null; } function ChartInner({ @@ -196,6 +212,7 @@ function ChartInner({ editable, onAddLink, onRemoveLink, + onReconnectLink, }: MissionCommandChainChartProps) { const rf = useReactFlow(); const participantKey = participants.map((p) => p.id).join(","); @@ -266,6 +283,7 @@ function ChartInner({ targetHandle: "t", type: "command", selectable: editable, + reconnectable: editable, markerEnd: { type: MarkerType.ArrowClosed, width: 14, height: 14, color: invalid ? "#f87171" : "rgba(255,255,255,0.5)" }, data: { invalid, @@ -286,6 +304,24 @@ function ChartInner({ [editable, onAddLink], ); + // Edge reconnect gesture (grab an existing link's endpoint and drag): + // releasing over empty canvas removes that link; releasing over another card + // rewires it through the same rank validation as new connections. A short + // grab (click or drop-back within a few px) does nothing, and abandoning the + // drag near a card never silently deletes — only deliberate releases do. + const reconnectInfo = React.useRef<{ edgeId: string; x: number; y: number } | null>(null); + const didReconnectCommit = React.useRef(false); + + const pointerPoint = (event: MouseEvent | TouchEvent) => { + if ("clientX" in event) return { x: event.clientX, y: event.clientY }; + const touch = event.changedTouches[0]; + return touch ? { x: touch.clientX, y: touch.clientY } : null; + }; + + /** Release position over a person card? Then the drag was abandoned, not an unlink. */ + const underCard = (x: number, y: number) => + document.elementFromPoint(x, y)?.closest(".react-flow__node-chainPerson") != null; + // Re-fit when the participant set changes (RSVP churn after router.refresh). React.useEffect(() => { rf.fitView({ padding: 0.15, maxZoom: 1 }); @@ -311,7 +347,41 @@ function ChartInner({ nodesDraggable={false} nodesConnectable={editable} nodesFocusable={false} + // Loose mode: each card carries one bottom-source and one top-target + // handle, but rank decides direction after the drop, so any handle + // pair must be able to complete a (re)connection. + connectionMode={ConnectionMode.Loose} + reconnectRadius={9} onConnect={onConnect} + edgesReconnectable={editable} + onReconnectStart={(event, edge) => { + didReconnectCommit.current = false; + reconnectInfo.current = { edgeId: edge.id, x: event.clientX, y: event.clientY }; + }} + onReconnect={(oldEdge, newConnection) => { + didReconnectCommit.current = true; + const prev = parseEdgeId(oldEdge.id); + if (!prev || !onReconnectLink) return; + if (newConnection.source == null || newConnection.target == null) return; + const nextSup = Number(newConnection.source); + const nextSub = Number(newConnection.target); + if (Number.isNaN(nextSup) || Number.isNaN(nextSub)) return; + if (nextSup === prev[0] && nextSub === prev[1]) return; // dropped back, no change + onReconnectLink(prev[0], prev[1], nextSup, nextSub); + }} + onReconnectEnd={(event, edge) => { + const info = reconnectInfo.current; + reconnectInfo.current = null; + if (didReconnectCommit.current || !edge.selectable) return; // committed, or read-only viewer + const pt = pointerPoint(event); + if (!pt) return; + const moved = info && info.edgeId === edge.id ? Math.hypot(pt.x - info.x, pt.y - info.y) : Infinity; + if (moved < 8) return; // a click or drop-back, not an unlink gesture + if (underCard(pt.x, pt.y)) return; // abandoned near a card: keep the link + const pair = parseEdgeId(edge.id); + if (!pair) return; + onRemoveLink(pair[0], pair[1]); + }} > { + const { supervisorId, subordinateId } = autoOrientPair(participants, dragFrom, dragTo); + if (supervisorId === prevSupervisorId && subordinateId === prevSubordinateId) return; + const withoutOld: ChainDraft = { + ...draft, + [prevSubordinateId]: (draft[prevSubordinateId] ?? []).filter((s) => s !== prevSupervisorId), + }; + const error = checkConnect(withoutOld, participants, supervisorId, subordinateId); + if (error) { + toast.error(CONNECT_REJECTION_MESSAGES[error]); + return; + } + setDraft((prev) => { + const next: ChainDraft = { ...prev }; + next[prevSubordinateId] = (next[prevSubordinateId] ?? []).filter((s) => s !== prevSupervisorId); + const row = next[subordinateId] ?? []; + next[subordinateId] = row.includes(supervisorId) ? row : [...row, supervisorId]; + return next; + }); + }; + const invalidKeys = React.useMemo(() => invalidLinkKeys(draft, participants), [draft, participants]); const save = async () => { @@ -142,7 +173,8 @@ export function MissionCommandChainSection({

Who commands whom for this operation. Supervisors may rate their subordinates as a command review. Drag between cards to add a link; - the higher-ranked card becomes the supervisor. + the higher-ranked card becomes the supervisor. Grab an existing link + and drag it off onto empty space to remove it.

{MODE_LABELS[mode]} @@ -194,6 +226,7 @@ export function MissionCommandChainSection({ editable={editable} onAddLink={handleChartAdd} onRemoveLink={remove} + onReconnectLink={handleChartReconnect} />