use-agent-history-manager.ts


Overview

The use-agent-history-manager.ts file provides an undo/redo history management system tailored for managing graph data consisting of nodes and edges. It encapsulates the history logic within a HistoryManager class and exposes a custom React hook useAgentHistoryManager which integrates the history manager with a React application's state management (using a Zustand store here).

This file's primary purpose is to track changes to the graph's state, allowing users to revert (undo) or reapply (redo) changes efficiently. It also supports keyboard shortcuts (Ctrl/Cmd + Z for undo, Ctrl/Cmd + Shift + Z for redo) to provide a smooth user experience.


Detailed Explanation

Class: HistoryManager

Manages the history stack of graph states and provides methods to manipulate and navigate the history.

Properties

Name

Type

Description

history

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

Array storing snapshots of graph states.

currentIndex

number

Index of the current state in the history array.

maxSize

number

Maximum number of history states to retain (default 50).

setNodes

(nodes: any[]) => void

Callback to update the nodes state in the app.

setEdges

(edges: any[]) => void

Callback to update the edges state in the app.

lastSavedState

string

JSON string of the last saved state for quick comparison.

Constructor

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

Methods

private statesEqual(state1, state2): boolean
push(nodes, edges): 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

Hook: useAgentHistoryManager

A React hook that integrates the HistoryManager with the React component lifecycle and the Zustand store.

Internal Behavior

Usage Example

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

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

  // Graph rendering and editing logic here

  return <div>/* Graph UI */</div>;
};

Implementation Details & Algorithms


Interactions with Other Parts of the System


Mermaid Class Diagram

classDiagram
    class HistoryManager {
        -history: array
        -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
    }

    HistoryManager o-- "2" Function : setNodes,setEdges

    class useAgentHistoryManager {
        <<hook>>
        +() : void
    }

Summary

The use-agent-history-manager.ts file encapsulates history tracking for graph data structures in React applications. It provides both a class (HistoryManager) to handle undo/redo state management and a custom hook (useAgentHistoryManager) that connects it seamlessly with React state and keyboard events. This design enables intuitive and efficient graph editing experiences with robust undo/redo capabilities.