Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:777genius Agent teams ai useGraphInteraction

From Leeroopedia
Knowledge Sources
Domains Visualization, Interaction
Last Updated 2026-10-22 00:00 GMT

Overview

Concrete tool for handling mouse interactions (hover, drag, click, double-click) on a canvas graph with hit detection and drag thresholding, provided by the Agent Teams agent-graph package.

Description

The useGraphInteraction() React hook manages mouse interaction state for a canvas graph. It uses `findNodeAt()` for hit detection, maintains refs for hovered/dragged node IDs, and exposes event handlers for mouse down, move, up, and double-click.

Key behaviors:

  • Drag threshold: A minimum squared distance (`ANIM.dragThresholdPx`) must be exceeded before a mouse-down is treated as a drag rather than a click.
  • Member-only dragging: Only 'member' nodes can be dragged (the lead is fixed at center in stable-slot layout). The `canDragNode` callback can further restrict dragging.
  • Hit detection: `findNodeAt()` from `canvas/hit-detection.ts` performs point-in-shape testing against all graph nodes, accounting for zoom.
  • Hover tracking: `hoveredNodeId` ref is updated on mouse move when not dragging.
  • Click detection: A mouse-up without exceeding the drag threshold fires a click on the `clickedNodeId`.
  • Double-click: `handleDoubleClick` is exposed for double-click interactions.

Usage

  • Canvas event binding: Bind the returned handlers to canvas mousedown, mousemove, mouseup, and dblclick events.
  • Drag callback: Pass an `onDragNode` callback to receive drag position updates.

Code Reference

Source Location

  • Repository: agent-teams
  • File: packages/agent-graph/src/hooks/useGraphInteraction.ts

Signature

export interface UseGraphInteractionOptions {
  canDragNode?: (node: GraphNode) => boolean;
}

export interface UseGraphInteractionResult {
  hoveredNodeId: React.Ref<string | null>;
  dragNodeId: React.Ref<string | null>;
  isDragging: React.Ref<boolean>;
  handleMouseDown: (wx: number, wy: number, nodes: GraphNode[], zoom?: number) => void;
  handleMouseMove: (wx: number, wy: number, nodes: GraphNode[], zoom?: number) => void;
  handleMouseUp: () => void;
  handleDoubleClick: (wx: number, wy: number, nodes: GraphNode[], zoom?: number) => void;
}

export function useGraphInteraction(
  onDragNode?: (nodeId: string, x: number, y: number) => void,
  options?: UseGraphInteractionOptions
): UseGraphInteractionResult;

Import

import { useGraphInteraction } from '@agent-graph/hooks/useGraphInteraction';

I/O Contract

Inputs

Name Type Required Description
onDragNode (nodeId, x, y) => void No Callback fired during drag with node ID and world coordinates
options.canDragNode (node) => boolean No Gate function to restrict which nodes can be dragged

Outputs

Name Type Description
hoveredNodeId null> Currently hovered node ID
dragNodeId null> Currently dragged node ID
isDragging Ref<boolean> Whether drag threshold has been exceeded
handleMouseDown (wx, wy, nodes, zoom?) => void Mouse down handler
handleMouseMove (wx, wy, nodes, zoom?) => void Mouse move handler
handleMouseUp () => void Mouse up handler
handleDoubleClick (wx, wy, nodes, zoom?) => void Double-click handler

Usage Examples

Binding to Canvas Events

import { useGraphInteraction } from '@agent-graph/hooks/useGraphInteraction';

function GraphCanvas({ nodes, camera, onDragNode }) {
  const interaction = useGraphInteraction(onDragNode, {
    canDragNode: (node) => node.kind === 'member' && node.state !== 'spawning',
  });

  const toWorld = (e: MouseEvent) => {
    const rect = canvas.getBoundingClientRect();
    const sx = e.clientX - rect.left;
    const sy = e.clientY - rect.top;
    return {
      x: (sx - camera.x) / camera.zoom,
      y: (sy - camera.y) / camera.zoom,
    };
  };

  return (
    <canvas
      onMouseDown={(e) => {
        const { x, y } = toWorld(e);
        interaction.handleMouseDown(x, y, nodes, camera.zoom);
      }}
      onMouseMove={(e) => {
        const { x, y } = toWorld(e);
        interaction.handleMouseMove(x, y, nodes, camera.zoom);
      }}
      onMouseUp={() => interaction.handleMouseUp()}
      onDoubleClick={(e) => {
        const { x, y } = toWorld(e);
        interaction.handleDoubleClick(x, y, nodes, camera.zoom);
      }}
    />
  );
}

Reading Hover State for Rendering

// Pass hovered ID to drawAgents for hover highlighting
drawAgents(
  ctx,
  nodes,
  time,
  selectedId,
  interaction.hoveredNodeId.current,  // From interaction hook
  null,
  zoom
);

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment