Skip to content

Commit 8a08a36

Browse files
Copilotmikebarkmin
andcommitted
Move declare local variable dialog to top-level overlay
Co-authored-by: mikebarkmin <2592379+mikebarkmin@users.noreply.github.com>
1 parent d146ecc commit 8a08a36

2 files changed

Lines changed: 70 additions & 44 deletions

File tree

src/MemoryView.tsx

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { shallow } from "zustand/shallow";
2020
import { getEdgesAndNodes, getMemory } from "./getEdgesAndNodes";
2121
import ObjectNode, { ObjectNodeType } from "./ObjectNode";
2222
import VariableNode from "./VariableNode";
23-
import { useCallback, useState, DragEvent, useRef } from "react";
23+
import { useCallback, useState, DragEvent, useRef, useMemo } from "react";
2424
import { Sidebar } from "./Sidebar";
2525
import {
2626
Attribute,
@@ -48,12 +48,6 @@ const selector = (state: RFState) => ({
4848
setRoute: state.setRoute,
4949
});
5050

51-
const nodeTypes = {
52-
object: ObjectNode,
53-
variable: VariableNode,
54-
"method-call": MethodCallNode,
55-
};
56-
5751
const edgeTypes = {
5852
reference: ReferenceEdge,
5953
};
@@ -132,6 +126,10 @@ export const MemoryView = () => {
132126
onConfirm: (value: string) => void;
133127
} | null>(null);
134128

129+
// Local variable declaration dialog state
130+
const [showLocalVarDialog, setShowLocalVarDialog] = useState(false);
131+
const [localVarDialogNodeId, setLocalVarDialogNodeId] = useState<string | null>(null);
132+
135133
const methodCalls = nodes.filter(isMethodCallNode);
136134
let previousMethodCall = methodCalls[0];
137135
let lastMethodCall = methodCalls[0];
@@ -202,6 +200,50 @@ export const MemoryView = () => {
202200
return [...primitveDataTypes, ...Object.keys(memory.klasses)];
203201
};
204202

203+
// Handler for declaring local variables
204+
const handleDeclareLocalVariable = (nodeId: string) => {
205+
setLocalVarDialogNodeId(nodeId);
206+
setShowLocalVarDialog(true);
207+
};
208+
209+
const handleLocalVarDialogConfirm = (name: string) => {
210+
if (localVarDialogNodeId) {
211+
setNodes((nds) =>
212+
nds.map((n) => {
213+
if (n.id === localVarDialogNodeId && n.type === "method-call") {
214+
return {
215+
...n,
216+
data: {
217+
...n.data,
218+
localVariables: {
219+
...n.data.localVariables,
220+
[name]: {
221+
dataType: "object",
222+
},
223+
},
224+
},
225+
};
226+
}
227+
return n;
228+
})
229+
);
230+
}
231+
setShowLocalVarDialog(false);
232+
setLocalVarDialogNodeId(null);
233+
};
234+
235+
// Create nodeTypes with access to handleDeclareLocalVariable
236+
const nodeTypes = useMemo(
237+
() => ({
238+
object: ObjectNode,
239+
variable: VariableNode,
240+
"method-call": (props: any) => (
241+
<MethodCallNode {...props} onDeclareVariable={handleDeclareLocalVariable} />
242+
),
243+
}),
244+
[handleDeclareLocalVariable]
245+
);
246+
205247
const onConnectEnd = useCallback<OnConnectEnd>(
206248
(event, connectionState) => {
207249
if (!connectingNode.current || !memory.options.createNewOnEdgeDrop)
@@ -777,6 +819,18 @@ export const MemoryView = () => {
777819
}}
778820
/>
779821
)}
822+
{showLocalVarDialog && (
823+
<SimpleInputDialog
824+
title="Declare Local Variable"
825+
label="Variable Name"
826+
placeholder="Enter variable name"
827+
onConfirm={handleLocalVarDialogConfirm}
828+
onCancel={() => {
829+
setShowLocalVarDialog(false);
830+
setLocalVarDialogNodeId(null);
831+
}}
832+
/>
833+
)}
780834
</div>
781835
);
782836
};

src/MethodCallNode.tsx

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeEventHandler, useState } from "react";
1+
import { ChangeEventHandler } from "react";
22
import {
33
Node,
44
Handle,
@@ -14,7 +14,6 @@ import {
1414
primitveDataTypes,
1515
} from "./memory";
1616
import { CustomEdgeType, CustomNodeType } from "./types";
17-
import { SimpleInputDialog } from "./SimpleInputDialog";
1817

1918
function LocalVariableHandle({
2019
name,
@@ -138,10 +137,13 @@ export function isMethodCallNode(node: Node): node is MethodCallNodeType {
138137
return node.type === "method-call";
139138
}
140139

141-
function MethodCallNode({ id, data }: NodeProps<MethodCallNodeType>) {
140+
function MethodCallNode({
141+
id,
142+
data,
143+
onDeclareVariable,
144+
}: NodeProps<MethodCallNodeType> & { onDeclareVariable?: (nodeId: string) => void }) {
142145
const { setNodes } = useReactFlow<CustomNodeType, CustomEdgeType>();
143146
const edges = useEdges<CustomEdgeType>();
144-
const [showVariableDialog, setShowVariableDialog] = useState(false);
145147

146148
const localVariablesEdges = edges.filter((e) => e.source == id);
147149

@@ -150,30 +152,9 @@ function MethodCallNode({ id, data }: NodeProps<MethodCallNodeType>) {
150152
};
151153

152154
const handleDeclareLocaleVariable = () => {
153-
setShowVariableDialog(true);
154-
};
155-
156-
const handleVariableDialogConfirm = (name: string) => {
157-
setNodes((nds) =>
158-
nds.map((n) => {
159-
if (n.id == id && n.type === "method-call") {
160-
return {
161-
...n,
162-
data: {
163-
...n.data,
164-
localVariables: {
165-
...n.data.localVariables,
166-
[name]: {
167-
dataType: "object",
168-
},
169-
},
170-
},
171-
};
172-
}
173-
return n;
174-
})
175-
);
176-
setShowVariableDialog(false);
155+
if (onDeclareVariable) {
156+
onDeclareVariable(id);
157+
}
177158
};
178159

179160
return (
@@ -212,15 +193,6 @@ function MethodCallNode({ id, data }: NodeProps<MethodCallNodeType>) {
212193
</button>
213194
</div>
214195
</div>
215-
{showVariableDialog && (
216-
<SimpleInputDialog
217-
title="Declare Local Variable"
218-
label="Variable Name"
219-
placeholder="Enter variable name"
220-
onConfirm={handleVariableDialogConfirm}
221-
onCancel={() => setShowVariableDialog(false)}
222-
/>
223-
)}
224196
</>
225197
);
226198
}

0 commit comments

Comments
 (0)