2025-03-28 13:43:20 +00:00
|
|
|
import { useThree } from "@react-three/fiber";
|
|
|
|
import React, { useState, useEffect } from "react";
|
2025-04-02 13:19:18 +00:00
|
|
|
import { useAsset3dWidget, useSocketStore, useWidgetSubOption } from "../../../store/store";
|
2025-03-28 13:43:20 +00:00
|
|
|
import useModuleStore from "../../../store/useModuleStore";
|
|
|
|
import { ThreeState } from "../../../types/world/worldTypes";
|
|
|
|
import * as THREE from "three";
|
|
|
|
import Throughput from "../../layout/3D-cards/cards/Throughput";
|
|
|
|
import ProductionCapacity from "../../layout/3D-cards/cards/ProductionCapacity";
|
|
|
|
import ReturnOfInvestment from "../../layout/3D-cards/cards/ReturnOfInvestment";
|
|
|
|
import StateWorking from "../../layout/3D-cards/cards/StateWorking";
|
2025-03-29 13:51:20 +00:00
|
|
|
import { useSelectedZoneStore } from "../../../store/useZoneStore";
|
2025-03-31 13:50:03 +00:00
|
|
|
import { generateUniqueId } from "../../../functions/generateUniqueId";
|
|
|
|
import { adding3dWidgets } from "../../../services/realTimeVisulization/zoneData/add3dWidget";
|
|
|
|
import { get3dWidgetZoneData } from "../../../services/realTimeVisulization/zoneData/get3dWidgetData";
|
|
|
|
import { use3DWidget } from "../../../store/useDroppedObjectsStore";
|
2025-04-02 13:19:18 +00:00
|
|
|
import { useZoneWidgetStore } from "../../../store/useZone3DWidgetStore";
|
2025-03-28 13:43:20 +00:00
|
|
|
|
|
|
|
export default function Dropped3dWidgets() {
|
2025-03-31 13:50:03 +00:00
|
|
|
const { widgetSelect } = useAsset3dWidget();
|
2025-03-28 13:43:20 +00:00
|
|
|
const { activeModule } = useModuleStore();
|
|
|
|
const { raycaster, gl, scene }: ThreeState = useThree();
|
2025-04-02 13:19:18 +00:00
|
|
|
const { widgetSubOption } = useWidgetSubOption();
|
|
|
|
const { selectedZone } = useSelectedZoneStore();
|
|
|
|
|
|
|
|
// ✅ Use Zustand Store instead of useState
|
|
|
|
const { zoneWidgetData, setZoneWidgetData, addWidget } = useZoneWidgetStore();
|
|
|
|
const { setWidgets3D } = use3DWidget();
|
|
|
|
const { visualizationSocket } = useSocketStore();
|
|
|
|
|
2025-03-31 13:50:03 +00:00
|
|
|
useEffect(() => {
|
2025-04-02 13:19:18 +00:00
|
|
|
if (activeModule !== "visualization") return;
|
|
|
|
if (!selectedZone.zoneId) return;
|
2025-03-31 13:50:03 +00:00
|
|
|
|
2025-04-02 12:21:44 +00:00
|
|
|
const email = localStorage.getItem("email") || "";
|
|
|
|
const organization = email?.split("@")[1]?.split(".")[0];
|
2025-03-31 13:50:03 +00:00
|
|
|
|
|
|
|
async function get3dWidgetData() {
|
|
|
|
let result = await get3dWidgetZoneData(selectedZone.zoneId, organization);
|
2025-04-01 14:05:11 +00:00
|
|
|
console.log('result: ', result);
|
2025-04-02 13:19:18 +00:00
|
|
|
setWidgets3D(result);
|
|
|
|
|
2025-03-31 13:50:03 +00:00
|
|
|
const formattedWidgets = result.map((widget: any) => ({
|
|
|
|
id: widget.id,
|
|
|
|
type: widget.type,
|
|
|
|
position: widget.position,
|
|
|
|
}));
|
|
|
|
|
2025-04-02 13:19:18 +00:00
|
|
|
setZoneWidgetData(selectedZone.zoneId, formattedWidgets);
|
2025-03-31 13:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get3dWidgetData();
|
2025-04-01 09:46:54 +00:00
|
|
|
}, [selectedZone.zoneId, activeModule]);
|
2025-03-28 13:43:20 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2025-03-31 13:50:03 +00:00
|
|
|
if (activeModule !== "visualization") return;
|
2025-04-02 13:19:18 +00:00
|
|
|
if (widgetSubOption === "Floating" || widgetSubOption === "2D") return;
|
|
|
|
if (selectedZone.zoneName === "") return;
|
|
|
|
|
2025-03-28 13:43:20 +00:00
|
|
|
const canvasElement = gl.domElement;
|
2025-04-02 13:19:18 +00:00
|
|
|
|
2025-03-31 13:50:03 +00:00
|
|
|
const onDrop = async (event: DragEvent) => {
|
2025-04-02 13:19:18 +00:00
|
|
|
event.preventDefault();
|
|
|
|
|
2025-03-31 13:50:03 +00:00
|
|
|
const email = localStorage.getItem("email") || "";
|
|
|
|
const organization = email?.split("@")[1]?.split(".")[0];
|
|
|
|
if (!widgetSelect.startsWith("ui")) return;
|
|
|
|
const group1 = scene.getObjectByName("itemsGroup");
|
|
|
|
if (!group1) return;
|
2025-04-02 13:19:18 +00:00
|
|
|
|
2025-03-31 13:50:03 +00:00
|
|
|
const intersects = raycaster.intersectObjects(scene.children, true).filter(
|
|
|
|
(intersect) =>
|
|
|
|
!intersect.object.name.includes("Roof") &&
|
|
|
|
!intersect.object.name.includes("agv-collider") &&
|
|
|
|
!intersect.object.name.includes("MeasurementReference") &&
|
|
|
|
!intersect.object.userData.isPathObject &&
|
|
|
|
!(intersect.object.type === "GridHelper")
|
|
|
|
);
|
2025-04-02 13:19:18 +00:00
|
|
|
|
2025-03-31 13:50:03 +00:00
|
|
|
if (intersects.length > 0) {
|
|
|
|
const { x, y, z } = intersects[0].point;
|
2025-04-02 13:19:18 +00:00
|
|
|
const newWidget = {
|
2025-03-31 13:50:03 +00:00
|
|
|
id: generateUniqueId(),
|
|
|
|
type: widgetSelect,
|
2025-04-02 13:19:18 +00:00
|
|
|
position: [x, y, z] as [number, number, number],
|
2025-03-31 13:50:03 +00:00
|
|
|
};
|
|
|
|
|
2025-04-02 13:19:18 +00:00
|
|
|
let add3dWidget = {
|
|
|
|
organization: organization,
|
|
|
|
widget: newWidget,
|
|
|
|
zoneId: selectedZone.zoneId
|
|
|
|
}
|
|
|
|
console.log('add3dWidget: ', add3dWidget);
|
|
|
|
if (visualizationSocket) {
|
|
|
|
visualizationSocket.emit("v2:viz-3D-widget:add", add3dWidget)
|
|
|
|
}
|
2025-03-31 13:50:03 +00:00
|
|
|
|
2025-04-02 13:19:18 +00:00
|
|
|
// let response = await adding3dWidgets(selectedZone.zoneId, organization, newWidget);
|
|
|
|
// console.log('response: ', response);
|
2025-03-31 13:50:03 +00:00
|
|
|
|
2025-04-02 13:19:18 +00:00
|
|
|
// if (response.message === "Widget created successfully") {
|
|
|
|
addWidget(selectedZone.zoneId, newWidget);
|
|
|
|
// }
|
2025-03-28 13:43:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
canvasElement.addEventListener("drop", onDrop);
|
|
|
|
return () => {
|
2025-03-31 13:50:03 +00:00
|
|
|
canvasElement.removeEventListener("drop", onDrop);
|
2025-03-28 13:43:20 +00:00
|
|
|
};
|
2025-03-31 13:50:03 +00:00
|
|
|
}, [widgetSelect, activeModule, selectedZone.zoneId, widgetSubOption]);
|
|
|
|
|
|
|
|
const activeZoneWidgets = zoneWidgetData[selectedZone.zoneId] || [];
|
2025-03-28 13:43:20 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2025-03-31 13:50:03 +00:00
|
|
|
{activeZoneWidgets.map(({ id, type, position }) => {
|
2025-04-01 13:36:29 +00:00
|
|
|
console.log('Typeeeeeeeeeeee',type);
|
2025-03-31 13:50:03 +00:00
|
|
|
switch (type) {
|
|
|
|
case "ui-Widget 1":
|
2025-04-02 12:36:51 +00:00
|
|
|
return <ProductionCapacity key={id} id={id} type={type} position={position} />;
|
2025-03-31 13:50:03 +00:00
|
|
|
case "ui-Widget 2":
|
2025-04-02 12:36:51 +00:00
|
|
|
return <ReturnOfInvestment key={id} id={id} type={type} position={position} />;
|
2025-03-31 13:50:03 +00:00
|
|
|
case "ui-Widget 3":
|
2025-04-02 12:36:51 +00:00
|
|
|
return <StateWorking key={id} id={id} type={type} position={position} />;
|
2025-03-31 13:50:03 +00:00
|
|
|
case "ui-Widget 4":
|
2025-04-02 12:36:51 +00:00
|
|
|
return <Throughput key={id} id={id} type={type} position={position} />;
|
2025-03-31 13:50:03 +00:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})}
|
2025-03-28 13:43:20 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|