2025-03-25 06:17:41 +00:00
|
|
|
|
import React, { useEffect, useState, useRef } from "react";
|
|
|
|
|
import { usePlayButtonStore } from "../../../store/usePlayButtonStore";
|
|
|
|
|
import Panel from "./Panel";
|
|
|
|
|
import AddButtons from "./AddButtons";
|
|
|
|
|
import { useSelectedZoneStore } from "../../../store/useZoneStore";
|
|
|
|
|
import DisplayZone from "./DisplayZone";
|
2025-03-25 11:00:33 +00:00
|
|
|
|
import Scene from "../../../modules/scene/scene";
|
2025-03-26 07:02:04 +00:00
|
|
|
|
import useModuleStore from "../../../store/useModuleStore";
|
2025-03-27 12:34:16 +00:00
|
|
|
|
|
2025-03-27 06:58:17 +00:00
|
|
|
|
import DroppedObjects from "./DroppedFloatingWidgets";
|
2025-03-27 12:34:16 +00:00
|
|
|
|
import { useDroppedObjectsStore } from "../../../store/useDroppedObjectsStore";
|
2025-03-29 14:06:56 +00:00
|
|
|
|
import {
|
|
|
|
|
useAsset3dWidget,
|
|
|
|
|
useWidgetSubOption,
|
|
|
|
|
useZones,
|
|
|
|
|
} from "../../../store/store";
|
2025-03-28 13:43:20 +00:00
|
|
|
|
import { getZone2dData } from "../../../services/realTimeVisulization/zoneData/getZoneData";
|
2025-03-29 13:51:20 +00:00
|
|
|
|
import { generateUniqueId } from "../../../functions/generateUniqueId";
|
|
|
|
|
import { determinePosition } from "./functions/determinePosition";
|
|
|
|
|
import { addingFloatingWidgets } from "../../../services/realTimeVisulization/zoneData/addFloatingWidgets";
|
2025-03-26 13:00:33 +00:00
|
|
|
|
|
2025-03-25 06:17:41 +00:00
|
|
|
|
type Side = "top" | "bottom" | "left" | "right";
|
|
|
|
|
|
2025-03-26 06:58:22 +00:00
|
|
|
|
type FormattedZoneData = Record<
|
|
|
|
|
string,
|
|
|
|
|
{
|
|
|
|
|
activeSides: Side[];
|
|
|
|
|
panelOrder: Side[];
|
2025-03-28 13:43:20 +00:00
|
|
|
|
|
2025-03-26 06:58:22 +00:00
|
|
|
|
lockedPanels: Side[];
|
2025-03-26 13:00:33 +00:00
|
|
|
|
zoneId: string;
|
|
|
|
|
zoneViewPortTarget: number[];
|
2025-03-29 13:32:02 +00:00
|
|
|
|
zoneViewPortPosition: number[];
|
2025-03-26 06:58:22 +00:00
|
|
|
|
widgets: Widget[];
|
|
|
|
|
}
|
|
|
|
|
>;
|
|
|
|
|
type Widget = {
|
2025-03-25 06:17:41 +00:00
|
|
|
|
id: string;
|
|
|
|
|
type: string;
|
|
|
|
|
title: string;
|
|
|
|
|
panel: Side;
|
|
|
|
|
data: any;
|
2025-03-26 06:58:22 +00:00
|
|
|
|
};
|
2025-03-25 06:17:41 +00:00
|
|
|
|
|
|
|
|
|
const RealTimeVisulization: React.FC = () => {
|
|
|
|
|
const [hiddenPanels, setHiddenPanels] = React.useState<Side[]>([]);
|
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const { isPlaying } = usePlayButtonStore();
|
2025-03-26 07:02:04 +00:00
|
|
|
|
const { activeModule } = useModuleStore();
|
2025-03-27 06:58:17 +00:00
|
|
|
|
const [droppedObjects, setDroppedObjects] = useState<any[]>([]);
|
2025-03-26 06:58:22 +00:00
|
|
|
|
const [zonesData, setZonesData] = useState<FormattedZoneData>({});
|
2025-03-25 06:17:41 +00:00
|
|
|
|
const { selectedZone, setSelectedZone } = useSelectedZoneStore();
|
2025-03-29 13:32:02 +00:00
|
|
|
|
const { zones } = useZones();
|
|
|
|
|
const [floatingWidgets, setFloatingWidgets] = useState<
|
|
|
|
|
Record<string, { zoneName: string; zoneId: string; objects: any[] }>
|
|
|
|
|
>({});
|
2025-03-28 13:43:20 +00:00
|
|
|
|
const { widgetSelect, setWidgetSelect } = useAsset3dWidget();
|
2025-03-29 14:06:56 +00:00
|
|
|
|
const { widgetSubOption, setWidgetSubOption } = useWidgetSubOption();
|
2025-03-29 13:51:20 +00:00
|
|
|
|
|
2025-03-25 06:17:41 +00:00
|
|
|
|
useEffect(() => {
|
2025-03-28 13:43:20 +00:00
|
|
|
|
async function GetZoneData() {
|
|
|
|
|
const email = localStorage.getItem("email") || "";
|
|
|
|
|
const organization = email?.split("@")[1]?.split(".")[0];
|
|
|
|
|
try {
|
|
|
|
|
const response = await getZone2dData(organization);
|
2025-03-31 14:09:09 +00:00
|
|
|
|
console.log('response: ', response);
|
2025-03-31 13:50:03 +00:00
|
|
|
|
|
2025-03-28 13:43:20 +00:00
|
|
|
|
if (!Array.isArray(response)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-03-29 13:32:02 +00:00
|
|
|
|
const formattedData = response.reduce<FormattedZoneData>(
|
|
|
|
|
(acc, zone) => {
|
|
|
|
|
acc[zone.zoneName] = {
|
|
|
|
|
activeSides: [],
|
|
|
|
|
panelOrder: [],
|
|
|
|
|
lockedPanels: [],
|
|
|
|
|
zoneId: zone.zoneId,
|
|
|
|
|
zoneViewPortTarget: zone.viewPortCenter,
|
|
|
|
|
zoneViewPortPosition: zone.viewPortposition,
|
|
|
|
|
widgets: [],
|
|
|
|
|
};
|
|
|
|
|
return acc;
|
|
|
|
|
},
|
|
|
|
|
{}
|
|
|
|
|
);
|
2025-03-28 13:43:20 +00:00
|
|
|
|
setZonesData(formattedData);
|
|
|
|
|
} catch (error) {
|
2025-03-31 13:50:03 +00:00
|
|
|
|
|
2025-03-28 13:43:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GetZoneData();
|
2025-03-29 12:44:29 +00:00
|
|
|
|
}, [activeModule]); // Removed `zones` from dependencies
|
2025-03-26 06:58:22 +00:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setZonesData((prev) => {
|
|
|
|
|
if (!selectedZone) return prev;
|
|
|
|
|
return {
|
|
|
|
|
...prev,
|
|
|
|
|
[selectedZone.zoneName]: {
|
|
|
|
|
...prev[selectedZone.zoneName], // Keep existing properties
|
|
|
|
|
activeSides: selectedZone.activeSides || [],
|
|
|
|
|
panelOrder: selectedZone.panelOrder || [],
|
|
|
|
|
lockedPanels: selectedZone.lockedPanels || [],
|
2025-03-26 13:00:33 +00:00
|
|
|
|
zoneId: selectedZone.zoneId || "",
|
|
|
|
|
zoneViewPortTarget: selectedZone.zoneViewPortTarget || [],
|
|
|
|
|
zoneViewPortPosition: selectedZone.zoneViewPortPosition || [],
|
2025-03-26 06:58:22 +00:00
|
|
|
|
widgets: selectedZone.widgets || [],
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
});
|
2025-03-25 06:17:41 +00:00
|
|
|
|
}, [selectedZone]);
|
2025-03-28 13:43:20 +00:00
|
|
|
|
|
2025-03-31 13:50:03 +00:00
|
|
|
|
// useEffect(() => {}, [floatingWidgets]);
|
2025-03-25 06:17:41 +00:00
|
|
|
|
|
2025-03-29 13:51:20 +00:00
|
|
|
|
const handleDrop = async (event: React.DragEvent<HTMLDivElement>) => {
|
|
|
|
|
try {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
const email = localStorage.getItem("email") || "";
|
|
|
|
|
const organization = email?.split("@")[1]?.split(".")[0];
|
|
|
|
|
|
|
|
|
|
const data = event.dataTransfer.getData("text/plain");
|
|
|
|
|
// if (widgetSelect !== "") return;
|
2025-03-29 14:06:56 +00:00
|
|
|
|
if (widgetSubOption === "3D") return;
|
2025-03-29 13:51:20 +00:00
|
|
|
|
if (!data || selectedZone.zoneName === "") return;
|
|
|
|
|
|
|
|
|
|
const droppedData = JSON.parse(data);
|
|
|
|
|
const canvasElement = document.getElementById("real-time-vis-canvas");
|
|
|
|
|
if (!canvasElement) throw new Error("Canvas element not found");
|
|
|
|
|
|
|
|
|
|
const canvasRect = canvasElement.getBoundingClientRect();
|
|
|
|
|
const relativeX = event.clientX - canvasRect.left;
|
|
|
|
|
const relativeY = event.clientY - canvasRect.top;
|
|
|
|
|
|
|
|
|
|
const newObject = {
|
|
|
|
|
...droppedData,
|
|
|
|
|
id: generateUniqueId(),
|
|
|
|
|
position: determinePosition(canvasRect, relativeX, relativeY),
|
|
|
|
|
};
|
2025-03-31 13:50:03 +00:00
|
|
|
|
console.log('newObject: ', newObject);
|
2025-03-29 13:51:20 +00:00
|
|
|
|
|
2025-03-29 14:06:56 +00:00
|
|
|
|
let response = await addingFloatingWidgets(
|
|
|
|
|
selectedZone.zoneId,
|
|
|
|
|
organization,
|
|
|
|
|
newObject
|
|
|
|
|
);
|
2025-03-29 13:51:20 +00:00
|
|
|
|
|
|
|
|
|
// Only set zone if it’s not already in the store (prevents overwriting objects)
|
2025-03-29 14:06:56 +00:00
|
|
|
|
const existingZone =
|
|
|
|
|
useDroppedObjectsStore.getState().zones[selectedZone.zoneName];
|
2025-03-29 13:51:20 +00:00
|
|
|
|
if (!existingZone) {
|
2025-03-29 14:06:56 +00:00
|
|
|
|
useDroppedObjectsStore
|
|
|
|
|
.getState()
|
|
|
|
|
.setZone(selectedZone.zoneName, selectedZone.zoneId);
|
2025-03-29 13:51:20 +00:00
|
|
|
|
}
|
2025-03-31 14:09:09 +00:00
|
|
|
|
|
2025-03-29 13:51:20 +00:00
|
|
|
|
// Add the dropped object to the zone if the API call is successful
|
|
|
|
|
if (response.message === "FloatWidget created successfully") {
|
2025-03-29 14:06:56 +00:00
|
|
|
|
useDroppedObjectsStore
|
|
|
|
|
.getState()
|
|
|
|
|
.addObject(selectedZone.zoneName, newObject);
|
2025-03-29 13:51:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update floating widgets state
|
|
|
|
|
setFloatingWidgets((prevWidgets) => ({
|
|
|
|
|
...prevWidgets,
|
|
|
|
|
[selectedZone.zoneName]: {
|
|
|
|
|
...prevWidgets[selectedZone.zoneName],
|
|
|
|
|
zoneName: selectedZone.zoneName,
|
|
|
|
|
zoneId: selectedZone.zoneId,
|
2025-03-29 14:06:56 +00:00
|
|
|
|
objects: [
|
|
|
|
|
...(prevWidgets[selectedZone.zoneName]?.objects || []),
|
|
|
|
|
newObject,
|
|
|
|
|
],
|
2025-03-29 13:51:20 +00:00
|
|
|
|
},
|
|
|
|
|
}));
|
2025-03-31 13:50:03 +00:00
|
|
|
|
} catch (error) { }
|
2025-03-26 13:00:33 +00:00
|
|
|
|
};
|
2025-03-28 13:43:20 +00:00
|
|
|
|
|
2025-03-25 06:17:41 +00:00
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
ref={containerRef}
|
|
|
|
|
id="real-time-vis-canvas"
|
2025-03-26 08:22:10 +00:00
|
|
|
|
className={`realTime-viz canvas ${isPlaying ? "playingFlase" : ""}`}
|
2025-03-25 06:17:41 +00:00
|
|
|
|
style={{
|
2025-03-26 07:02:04 +00:00
|
|
|
|
height: isPlaying || activeModule !== "visualization" ? "100vh" : "",
|
|
|
|
|
width: isPlaying || activeModule !== "visualization" ? "100vw" : "",
|
|
|
|
|
left: isPlaying || activeModule !== "visualization" ? "0%" : "",
|
2025-03-25 06:17:41 +00:00
|
|
|
|
}}
|
|
|
|
|
>
|
2025-03-25 11:00:33 +00:00
|
|
|
|
<div
|
|
|
|
|
className="scene-container"
|
2025-03-26 07:02:04 +00:00
|
|
|
|
style={{
|
|
|
|
|
height: "100%",
|
|
|
|
|
width: "100%",
|
2025-03-27 05:24:40 +00:00
|
|
|
|
borderRadius:
|
|
|
|
|
isPlaying || activeModule !== "visualization" ? "" : "6px",
|
2025-03-26 07:02:04 +00:00
|
|
|
|
}}
|
2025-03-27 06:58:17 +00:00
|
|
|
|
onDrop={(event) => handleDrop(event)}
|
|
|
|
|
onDragOver={(event) => event.preventDefault()}
|
2025-03-25 11:00:33 +00:00
|
|
|
|
>
|
2025-03-29 13:32:02 +00:00
|
|
|
|
<Scene />
|
2025-03-25 11:00:33 +00:00
|
|
|
|
</div>
|
2025-03-31 13:50:03 +00:00
|
|
|
|
{activeModule === "visualization" && selectedZone.zoneName !== "" && <DroppedObjects />}
|
2025-03-31 14:09:09 +00:00
|
|
|
|
{/* <DroppedObjects /> */}
|
2025-03-26 07:02:04 +00:00
|
|
|
|
{activeModule === "visualization" && (
|
|
|
|
|
<>
|
|
|
|
|
<DisplayZone
|
|
|
|
|
zonesData={zonesData}
|
|
|
|
|
selectedZone={selectedZone}
|
|
|
|
|
setSelectedZone={setSelectedZone}
|
|
|
|
|
/>
|
|
|
|
|
|
2025-03-26 13:00:33 +00:00
|
|
|
|
{!isPlaying && selectedZone?.zoneName !== "" && (
|
2025-03-26 07:02:04 +00:00
|
|
|
|
<AddButtons
|
|
|
|
|
hiddenPanels={hiddenPanels}
|
|
|
|
|
setHiddenPanels={setHiddenPanels}
|
|
|
|
|
selectedZone={selectedZone}
|
|
|
|
|
setSelectedZone={setSelectedZone}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<Panel
|
|
|
|
|
selectedZone={selectedZone}
|
|
|
|
|
setSelectedZone={setSelectedZone}
|
2025-03-26 07:02:09 +00:00
|
|
|
|
hiddenPanels={hiddenPanels}
|
2025-03-27 05:24:40 +00:00
|
|
|
|
setZonesData={setZonesData}
|
2025-03-26 07:02:04 +00:00
|
|
|
|
/>
|
|
|
|
|
</>
|
2025-03-25 11:00:33 +00:00
|
|
|
|
)}
|
2025-03-25 06:17:41 +00:00
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default RealTimeVisulization;
|