use-agent-history-manager.ts


Overview

This file implements a history management system for a graph-based React application, allowing users to undo and redo changes made to graph nodes and edges. It provides a HistoryManager class that internally tracks changes to the graph's state and exposes undo/redo functionality. Additionally, the file defines a custom React hook useAgentHistoryManager which integrates the HistoryManager with React's component lifecycle and event handling, enabling seamless state history tracking and keyboard shortcut support.

The primary purpose of the file is to maintain a limited-size history stack of graph states, detect changes, and apply past states upon user commands (undo/redo). It also manages keyboard shortcuts (Ctrl/Cmd + Z and Ctrl/Cmd + Shift + Z) for undo and redo actions while respecting text input focus to avoid interfering with user typing.


Classes and Functions

Class: HistoryManager

Manages the history stack of graph states, allowing pushing new states, undoing, redoing, and resetting history.

Properties

Property

Type

Description

history

{ nodes: any[]; edges: any[] }[]

Array storing snapshots of graph states (nodes and edges).

currentIndex

number

The index pointing to the current state in the history stack.

maxSize

number

Maximum number of states kept in history (default 50).

setNodes

(nodes: any[]) => void

Function to update the graph nodes in the application state.

setEdges

(edges: any[]) => void

Function to update the graph edges in the application state.

lastSavedState

string

JSON string of the last saved state, used for change detection.

Constructor

constructor(setNodes: (nodes: any[]) => void, setEdges: (edges: any[]) => void)

Methods

private statesEqual(state1, state2): boolean
push(nodes: any[], edges: any[]): void
historyManager.push(currentNodes, currentEdges);
undo(): boolean
if (historyManager.canUndo()) {
  historyManager.undo();
}
redo(): boolean
if (historyManager.canRedo()) {
  historyManager.redo();
}
canUndo(): boolean
canRedo(): boolean
reset(): void

Function: useAgentHistoryManager

A React custom hook that integrates the HistoryManager with the application's graph state and keyboard event handling.

Description

Usage Example

import React from 'react';
import { useAgentHistoryManager } from './use-agent-history-manager';

const GraphComponent = () => {
  useAgentHistoryManager();

  // ...rest of component logic

  return <GraphVisualization />;
};

Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class HistoryManager {
        -history: Array<{nodes: any[], edges: any[]}>
        -currentIndex: number
        -maxSize: number
        -setNodes(nodes: any[]): void
        -setEdges(edges: any[]): void
        -lastSavedState: string
        -statesEqual(state1, state2): boolean
        +constructor(setNodes, setEdges)
        +push(nodes, edges): void
        +undo(): boolean
        +redo(): boolean
        +canUndo(): boolean
        +canRedo(): boolean
        +reset(): void
    }

    class useAgentHistoryManager {
        +(): void
    }

    useAgentHistoryManager --> HistoryManager : uses
    HistoryManager --> "setNodes, setEdges" : callbacks

Summary

use-agent-history-manager.ts provides a robust history management utility for graph editing applications, encapsulating undo/redo logic in the HistoryManager class and integrating it with React through the useAgentHistoryManager hook. It ensures efficient state tracking, respects user input contexts, and offers a user-friendly way to revert or reapply graph changes with keyboard shortcuts. This file plays a crucial role in enhancing the user experience by enabling state history management in the graph state ecosystem.