Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

3.9 KB
Raw Blame
import { useEffect, type Dispatch, type SetStateAction } from "react";
import type { ColorMode } from "./graph-toolbar";
import type { SigmaCanvasHandle } from "./sigma/sigma-canvas";
import type { GraphCtxMenu } from "./use-graph-context-menu";
import { claimCommandPaletteShortcut } from "../lib/command-palette-scope";

interface GraphKeyboardShortcutOptions {
  sigmaRef: { current: SigmaCanvasHandle | null };
  setSelectedNodeId: Dispatch<SetStateAction<string | null>>;
  setEgoDepth: Dispatch<SetStateAction<number>>;
  setSearchQuery: Dispatch<SetStateAction<string>>;
  setCtxMenu: Dispatch<SetStateAction<GraphCtxMenu | null>>;
  setCommunityPanelId: Dispatch<SetStateAction<number | null>>;
  /** Sets the active color mode. Accepts a concrete mode only (the shortcuts
   *  always pass a literal), so it composes with both the local state setter
   *  and a host-controlled setter. */
  setColorMode: (mode: ColorMode) => void;
  /** Optional Escape pre-handler. Return true to consume the keystroke and skip
   *  the default clear. Used to dismiss the top UI layer first: clear an open
   *  selection/panel, else collapse the most recent constellation hub. */
  onEscape?: (() => boolean) | undefined;
  /** `?` toggles the shortcut help overlay. */
  onToggleHelp?: (() => void) | undefined;
}

/**
 * Global keyboard shortcuts for the graph view: `f` fit, `Escape` clears,
 * `1/2/3` switch color mode, `/` and `cmd/ctrl+k` focus search. Typing in an
 * input / textarea / contenteditable is ignored.
 */
export function useGraphKeyboardShortcuts(opts: GraphKeyboardShortcutOptions): void {
  const {
    sigmaRef,
    setSelectedNodeId,
    setEgoDepth,
    setSearchQuery,
    setCtxMenu,
    setCommunityPanelId,
    setColorMode,
    onEscape,
    onToggleHelp,
  } = opts;

  useEffect(() => {
    // The graph binds ⌘K to its own node search, so it owns the shortcut while
    // it is on screen and the app-wide palette stands down.
    const release = claimCommandPaletteShortcut("graph");
    const handleKeyDown = (e: KeyboardEvent) => {
      const target = e.target as HTMLElement;
      if (target.tagName === "INPUT" || target.tagName === "TEXTAREA" || target.isContentEditable)
        return;

      switch (e.key) {
        case "f":
          e.preventDefault();
          sigmaRef.current?.fitView();
          break;
        case "Escape":
          // Let the host peel the top UI layer first (selection/panel, then
          // hub); only fall through to the blanket clear if nothing was open.
          if (onEscape?.()) {
            e.preventDefault();
            break;
          }
          setSelectedNodeId(null);
          setEgoDepth(0);
          setSearchQuery("");
          setCtxMenu(null);
          setCommunityPanelId(null);
          break;
        case "1":
          setColorMode("language");
          break;
        case "2":
          setColorMode("community");
          break;
        // No "3". It bound the risk lens, which is gone — see the `ColorMode`
        // docstring in `graph-toolbar.tsx`.
        case "/":
          e.preventDefault();
          document
            .querySelector<HTMLInputElement>('[aria-label="Search graph nodes"]')
            ?.focus();
          break;
        case "?":
          e.preventDefault();
          onToggleHelp?.();
          break;
      }

      if ((e.metaKey || e.ctrlKey) && e.key === "k") {
        e.preventDefault();
        document
          .querySelector<HTMLInputElement>('[aria-label="Search graph nodes"]')
          ?.focus();
      }
    };

    window.addEventListener("keydown", handleKeyDown);
    return () => {
      window.removeEventListener("keydown", handleKeyDown);
      release();
    };
    // Setters and the ref object are stable identities, so this binds once.
  }, [
    sigmaRef,
    setSelectedNodeId,
    setEgoDepth,
    setSearchQuery,
    setCtxMenu,
    setCommunityPanelId,
    setColorMode,
    onEscape,
    onToggleHelp,
  ]);
}