Skip to content

Commit 406302d

Browse files
committed
Fix buildings disappearing when removing and re-adding nodes
1 parent ca8a00b commit 406302d

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

mindy-website/www/src/components/AddBuildingMenu.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import {
1111
ProcessorKind,
1212
} from "mindy-website";
1313

14-
import { createNode } from "../utils";
14+
import {
15+
createNode,
16+
getMonotonicNodeCount,
17+
incrementMonotonicNodeCount,
18+
} from "../utils";
1519
import type { LogicVMNode } from "./LogicVMFlow";
1620

1721
export default function AddBuildingMenu() {
@@ -39,7 +43,7 @@ export default function AddBuildingMenu() {
3943
// calculate a position such that all nodes of a given size are at the same y position without overlapping
4044
const buildingPosition = {
4145
// leave enough room to the left assuming all nodes are in the same row as this one
42-
x: reactFlow.getNodes().length * node.size,
46+
x: getMonotonicNodeCount() * node.size,
4347
// the sum of the first n natural numbers is n*(n+1)/2
4448
// for a given size, we need to leave room below for all sizes < size
4549
// so y should be the sum of all sizes < size
@@ -48,6 +52,8 @@ export default function AddBuildingMenu() {
4852
y: ((node.size - 1) * node.size) / 2,
4953
};
5054

55+
incrementMonotonicNodeCount();
56+
5157
return [
5258
createNode({
5359
...node,

mindy-website/www/src/components/LogicVMFlow.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { useCallback, useEffect } from "react";
1616
import { DisplayKind, ProcessorKind } from "mindy-website";
1717

1818
import { useLogicVM } from "../hooks";
19-
import { createNode } from "../utils";
19+
import { createNode, setMonotonicNodeCount } from "../utils";
2020
import AddBuildingMenu from "./AddBuildingMenu";
2121
import BuildingLinkConnectionLine from "./edges/BuildingLinkConnectionLine";
2222
import BuildingLinkEdge from "./edges/BuildingLinkEdge";
@@ -92,6 +92,8 @@ const defaultEdges: Edge[] = [
9292
},
9393
];
9494

95+
setMonotonicNodeCount(defaultNodes.length);
96+
9597
export default function LogicVMFlow() {
9698
const vm = useLogicVM();
9799

mindy-website/www/src/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,17 @@ export function createNode<N, D>(
9898
},
9999
};
100100
}
101+
102+
let monotonicNodeCount = 0;
103+
104+
export function getMonotonicNodeCount() {
105+
return monotonicNodeCount;
106+
}
107+
108+
export function setMonotonicNodeCount(n: number) {
109+
monotonicNodeCount = Math.max(n, monotonicNodeCount);
110+
}
111+
112+
export function incrementMonotonicNodeCount() {
113+
setMonotonicNodeCount(monotonicNodeCount + 1);
114+
}

0 commit comments

Comments
 (0)