@@ -18,18 +45,47 @@ const Building = ({ data, selected }: NodeProps) => {
const { icon, name, description, color, inputs = [], outputs = [] } = data;
+ // Calculate position counts
+ const inputCounts = useMemo(() => {
+ const counts: Record
= {};
+ inputs.forEach((input) => {
+ counts[input.position] = (counts[input.position] || 0) + 1;
+ });
+ return counts;
+ }, [inputs]);
+
+ const outputCounts = useMemo(() => {
+ const counts: Record = {};
+ outputs.forEach((output) => {
+ counts[output.position] = (counts[output.position] || 0) + 1;
+ });
+ return counts;
+ }, [outputs]);
+
+ // Track index at each position
+ const inputIndexAtPosition: Record = {};
+ const outputIndexAtPosition: Record = {};
+
return (
- {inputs.map((input, index) => (
-
- ))}
+ {inputs.map((input, globalIndex) => {
+ const posIndex = inputIndexAtPosition[input.position] || 0;
+ inputIndexAtPosition[input.position] = posIndex + 1;
+
+ return (
+
+ );
+ })}
{
- {outputs.map((output, index) => (
-
- ))}
+ {outputs.map((output, globalIndex) => {
+ const posIndex = outputIndexAtPosition[output.position] || 0;
+ outputIndexAtPosition[output.position] = posIndex + 1;
+
+ return (
+
+ );
+ })}
);
};
diff --git a/src/components/Factory/Canvas/callbacks/onConnect.ts b/src/components/Factory/Canvas/callbacks/onConnect.ts
new file mode 100644
index 000000000..2cd2cfb59
--- /dev/null
+++ b/src/components/Factory/Canvas/callbacks/onConnect.ts
@@ -0,0 +1,53 @@
+import type { Connection, Edge } from "@xyflow/react";
+
+import { RESOURCES } from "../../data/resources";
+import { extractResource } from "../../utils/string";
+
+export const createOnConnect = (
+ setEdges: (update: Edge[] | ((edges: Edge[]) => Edge[])) => void,
+) => {
+ return (connection: Connection) => {
+ if (connection.source === connection.target) return;
+
+ const sourceResource = extractResource(connection.sourceHandle);
+ const targetResource = extractResource(connection.targetHandle);
+
+ if (
+ sourceResource !== "any" &&
+ targetResource !== "any" &&
+ sourceResource !== targetResource
+ ) {
+ return;
+ }
+
+ const edgeResource = sourceResource ?? targetResource;
+
+ if (!edgeResource) {
+ console.error("Invalid resource type:", edgeResource);
+ return;
+ }
+
+ const newEdge: Edge = {
+ ...connection,
+ id: `${connection.source}-${connection.sourceHandle}-${connection.target}-${connection.targetHandle}`,
+ type: "resourceEdge",
+ data: { ...RESOURCES[edgeResource] },
+ animated: true,
+ };
+
+ setEdges((eds) => {
+ // Remove any existing edges from the same source handle OR to the same target handle
+ const filteredEdges = eds.filter(
+ (edge) =>
+ !(
+ (edge.source === connection.source &&
+ edge.sourceHandle === connection.sourceHandle) ||
+ (edge.target === connection.target &&
+ edge.targetHandle === connection.targetHandle)
+ ),
+ );
+
+ return [...filteredEdges, newEdge];
+ });
+ };
+};
diff --git a/src/components/Factory/Canvas/callbacks/onDrop.ts b/src/components/Factory/Canvas/callbacks/onDrop.ts
new file mode 100644
index 000000000..9683ddf30
--- /dev/null
+++ b/src/components/Factory/Canvas/callbacks/onDrop.ts
@@ -0,0 +1,53 @@
+import type { Node, ReactFlowInstance } from "@xyflow/react";
+import type { DragEvent } from "react";
+
+import type { Building } from "../../types/buildings";
+
+let nodeIdCounter = 0;
+
+export const createOnDrop = (
+ reactFlowInstance: ReactFlowInstance | undefined,
+ setNodes: (update: Node[] | ((nodes: Node[]) => Node[])) => void,
+) => {
+ return (event: DragEvent