Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(frontend): Add minimum movement threshold for node position history tracking (#11481)

Abhimanyu Yadavea4b55f967af
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/stores/nodeStore.ts b/autogpt_platform/frontend/src/app/(platform)/build/stores/nodeStore.ts
index 892c0e1cac..a8101434f4 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/stores/nodeStore.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/build/stores/nodeStore.ts
@@ -13,6 +13,13 @@ import { useHistoryStore } from "./historyStore";
import { useEdgeStore } from "./edgeStore";
import { BlockUIType } from "../components/types";
+// Minimum movement (in pixels) required before logging position change to history
+// Prevents spamming history with small movements when clicking on inputs inside blocks
+const MINIMUM_MOVE_BEFORE_LOG = 50;
+
+// Track initial positions when drag starts (outside store to avoid re-renders)
+const dragStartPositions: Record<string, XYPosition> = {};
+
type NodeStore = {
nodes: CustomNode[];
nodeCounter: number;
@@ -61,12 +68,44 @@ export const useNodeStore = create<NodeStore>((set, get) => ({
nodes: get().nodes,
edges: useEdgeStore.getState().edges,
};
- const shouldTrack = changes.some(
- (change) =>
- change.type === "remove" ||
- change.type === "add" ||
- (change.type === "position" && change.dragging === false),
+
+ // Track initial positions when drag starts
+ changes.forEach((change) => {
+ if (change.type === "position" && change.dragging === true) {
+ if (!dragStartPositions[change.id]) {
+ const node = get().nodes.find((n) => n.id === change.id);
+ if (node) {
+ dragStartPositions[change.id] = { ...node.position };
+ }
+ }
+ }
+ });
+
+ // Check if we should track this change in history
+ let shouldTrack = changes.some(
+ (change) => change.type === "remove" || change.type === "add",
);
+
+ // For position changes, only track if movement exceeds threshold
+ if (!shouldTrack) {
+ changes.forEach((change) => {
+ if (change.type === "position" && change.dragging === false) {
+ const startPos = dragStartPositions[change.id];
+ if (startPos && change.position) {
+ const distanceMoved = Math.sqrt(
+ Math.pow(change.position.x - startPos.x, 2) +
+ Math.pow(change.position.y - startPos.y, 2),
+ );
+ if (distanceMoved > MINIMUM_MOVE_BEFORE_LOG) {
+ shouldTrack = true;
+ }
+ }
+ // Clean up tracked position after drag ends
+ delete dragStartPositions[change.id];
+ }
+ });
+ }
+
set((state) => ({
nodes: applyNodeChanges(changes, state.nodes),
}));