From ba615cefed63ef675eb0f96b18c5e4a5feccca37 Mon Sep 17 00:00:00 2001 From: Jerald-Golden-B Date: Fri, 17 Oct 2025 10:27:43 +0530 Subject: [PATCH] Refactor simulation styles and update API services - Refactored SCSS styles for simulation player and dashboard components to improve readability and maintainability. - Changed property names in Block and UIElement types for clarity. - Implemented getDashBoardBlocksApi and upsertDashBoardBlocksApi services for fetching and updating dashboard blocks. - Removed unnecessary transition property in simulation dashboard styles. --- .../SimulationDashboard/DashboardEditor.tsx | 224 ++- .../components/block/BlockComponent.tsx | 40 +- .../components/block/BlockGrid.tsx | 4 +- .../components/element/ElementComponent.tsx | 29 +- .../components/element/ElementEditor.tsx | 29 +- .../components/layout/scenes/MainScene.tsx | 4 +- app/src/components/ui/Tools.tsx | 126 +- .../ui/list/OutlineList/AssetOutline.tsx | 8 +- app/src/modules/builder/hooks/useZoomMesh.tsx | 64 +- .../collaboration/functions/getAvatarColor.ts | 10 + .../dashBoard/getDashBoardBlocks.ts | 27 + .../dashBoard/upsertDashBoardBlocks.ts | 30 + .../simulation/useSimulationDashBoardStore.ts | 138 +- .../components/simulation/_simulation.scss | 1233 ++++++++--------- .../_simulationDashBoard.scss | 2 +- app/src/types/exportedTypes.ts | 4 +- 16 files changed, 1016 insertions(+), 956 deletions(-) create mode 100644 app/src/services/visulization/dashBoard/getDashBoardBlocks.ts create mode 100644 app/src/services/visulization/dashBoard/upsertDashBoardBlocks.ts diff --git a/app/src/components/SimulationDashboard/DashboardEditor.tsx b/app/src/components/SimulationDashboard/DashboardEditor.tsx index e3e4314..f7d780c 100644 --- a/app/src/components/SimulationDashboard/DashboardEditor.tsx +++ b/app/src/components/SimulationDashboard/DashboardEditor.tsx @@ -1,3 +1,4 @@ +import { useParams } from "react-router-dom"; import React, { useState, useRef, useEffect } from "react"; import { dataModelManager } from "./data/dataModel"; import ControlPanel from "./ControlPanel"; @@ -5,16 +6,24 @@ import SwapModal from "./SwapModal"; import DataModelPanel from "./components/models/DataModelPanel"; import { useSceneContext } from "../../modules/scene/sceneContext"; +import useModuleStore from "../../store/ui/useModuleStore"; import { calculateMinBlockSize } from "./functions/block/calculateMinBlockSize"; import { handleElementDragStart, handleElementResizeStart, handleBlockResizeStart, handleSwapStart, handleSwapTarget, handleBlockClick, handleElementClick } from "./functions/eventHandlers"; import BlockGrid from "./components/block/BlockGrid"; import ElementDropdown from "./components/element/ElementDropdown"; import BlockEditor from "./components/block/BlockEditor"; import ElementEditor from "./components/element/ElementEditor"; +import useCallBackOnKey from "../../utils/hooks/useCallBackOnKey"; import { handleBlockDragStart } from "./functions/block/handleBlockDragStart"; +import { getDashBoardBlocksApi } from "../../services/visulization/dashBoard/getDashBoardBlocks"; +import { upsetDashBoardBlocksApi } from "../../services/visulization/dashBoard/upsertDashBoardBlocks"; + const DashboardEditor: React.FC = () => { - const { simulationDashBoardStore } = useSceneContext(); + const { simulationDashBoardStore, versionStore } = useSceneContext(); + const { selectedVersion } = versionStore(); + const { projectId } = useParams(); + const { activeModule } = useModuleStore(); const { blocks, setBlocks, @@ -33,7 +42,9 @@ const DashboardEditor: React.FC = () => { updateElementData, updateGraphData, updateGraphTitle, + updateGraphType, swapElements, + subscribe, } = simulationDashBoardStore(); const [selectedBlock, setSelectedBlock] = useState(null); const [selectedElement, setSelectedElement] = useState(null); @@ -63,8 +74,40 @@ const DashboardEditor: React.FC = () => { const dropdownRef = useRef(null); const blockRef = useRef(null); - const currentBlock = blocks.find((b) => b.id === selectedBlock); - const currentElement = currentBlock?.elements.find((el) => el.id === selectedElement); + const currentBlock = blocks.find((b) => b.blockUuid === selectedBlock); + const currentElement = currentBlock?.elements.find((el) => el.elementUuid === selectedElement); + + useEffect(() => { + if (!projectId || !selectedVersion) return; + // getDashBoardBlocksApi(projectId, selectedVersion.versionId).then((data) => { + // if (data.data && data.data.blocks) { + // console.log("data: ", data); + // setBlocks(data.data.blocks); + // } + // }); + }, [projectId, selectedVersion]); + + useEffect(() => { + const unsubscribe = subscribe(() => { + if (!projectId || !selectedVersion) return; + + // upsetDashBoardBlocksApi({ projectId, versionId: selectedVersion.versionId, blocks }).then((data) => { + // console.log("data: ", data); + // }); + }); + + return () => { + unsubscribe(); + }; + }, [blocks, projectId, selectedVersion]); + + useCallBackOnKey( + () => { + console.log(blocks); + }, + "Ctrl+S", + { dependencies: [blocks], noRepeat: true, allowOnInput: true } + ); // Subscribe to data model changes useEffect(() => { @@ -140,86 +183,144 @@ const DashboardEditor: React.FC = () => { }, [selectedBlock, selectedElement]); // Drag and drop handler - useEffect(() => { - const handleMouseMove = (e: MouseEvent): void => { - // Element dragging - use elementDragOffset - if (draggingElement && selectedBlock && currentElement?.positionType === "absolute") { - const blockElement = document.querySelector(`[data-block-id="${selectedBlock}"]`) as HTMLElement; - if (blockElement) { - const blockRect = blockElement.getBoundingClientRect(); - const newX = e.clientX - blockRect.left - elementDragOffset.x; // Use elementDragOffset - const newY = e.clientY - blockRect.top - elementDragOffset.y; // Use elementDragOffset +useEffect(() => { + const handleMouseMove = (e: MouseEvent): void => { + // Element dragging - direct DOM manipulation + if (draggingElement && selectedBlock && currentElement?.positionType === "absolute") { + const blockElement = document.querySelector(`[data-block-id="${selectedBlock}"]`) as HTMLElement; + const elementToDrag = document.querySelector(`[data-element-id="${draggingElement}"]`) as HTMLElement; + + if (blockElement && elementToDrag) { + const blockRect = blockElement.getBoundingClientRect(); + const newX = e.clientX - blockRect.left - elementDragOffset.x; + const newY = e.clientY - blockRect.top - elementDragOffset.y; - updateElementPosition(selectedBlock, draggingElement, { - x: Math.max(0, Math.min(blockRect.width - 50, newX)), - y: Math.max(0, Math.min(blockRect.height - 30, newY)), - }); - } + // Direct DOM manipulation + elementToDrag.style.left = `${Math.max(0, Math.min(blockRect.width - 50, newX))}px`; + elementToDrag.style.top = `${Math.max(0, Math.min(blockRect.height - 30, newY))}px`; } + } - // Block dragging - use blockDragOffset - if (draggingBlock && currentBlock?.positionType && (currentBlock.positionType === "absolute" || currentBlock.positionType === "fixed")) { - const editorElement = editorRef.current; - if (editorElement) { - const editorRect = editorElement.getBoundingClientRect(); - const newX = e.clientX - editorRect.left - blockDragOffset.x; // Use blockDragOffset - const newY = e.clientY - editorRect.top - blockDragOffset.y; // Use blockDragOffset + // Block dragging - direct DOM manipulation + if (draggingBlock && currentBlock?.positionType && (currentBlock.positionType === "absolute" || currentBlock.positionType === "fixed")) { + const editorElement = editorRef.current; + const blockToDrag = document.querySelector(`[data-block-id="${draggingBlock}"]`) as HTMLElement; + + if (editorElement && blockToDrag) { + const editorRect = editorElement.getBoundingClientRect(); + const newX = e.clientX - editorRect.left - blockDragOffset.x; + const newY = e.clientY - editorRect.top - blockDragOffset.y; - updateBlockPosition(draggingBlock, { - x: Math.max(0, Math.min(editorRect.width - (currentBlock.size?.width || 400), newX)), - y: Math.max(0, Math.min(editorRect.height - (currentBlock.size?.height || 300), newY)), - }); - } + // Direct DOM manipulation + blockToDrag.style.left = `${Math.max(0, Math.min(editorRect.width - (currentBlock.size?.width || 400), newX))}px`; + blockToDrag.style.top = `${Math.max(0, Math.min(editorRect.height - (currentBlock.size?.height || 300), newY))}px`; } + } - if ((resizingElement || resizingBlock) && resizeStart) { - if (resizingElement && selectedBlock) { + // Resizing - direct DOM manipulation + if ((resizingElement || resizingBlock) && resizeStart) { + if (resizingElement && selectedBlock) { + const elementToResize = document.querySelector(`[data-element-id="${resizingElement}"]`) as HTMLElement; + if (elementToResize) { const deltaX = e.clientX - resizeStart.x; const deltaY = e.clientY - resizeStart.y; const newWidth = Math.max(100, resizeStart.width + deltaX); const newHeight = Math.max(50, resizeStart.height + deltaY); - updateElementSize(selectedBlock, resizingElement, { - width: newWidth, - height: newHeight, - }); - } else if (resizingBlock) { + // Direct DOM manipulation + elementToResize.style.width = `${newWidth}px`; + elementToResize.style.height = `${newHeight}px`; + } + } else if (resizingBlock) { + const blockToResize = document.querySelector(`[data-block-id="${resizingBlock}"]`) as HTMLElement; + if (blockToResize) { const deltaX = e.clientX - resizeStart.x; const deltaY = e.clientY - resizeStart.y; - const currentBlock = blocks.find((b) => b.id === resizingBlock); + const currentBlock = blocks.find((b) => b.blockUuid === resizingBlock); const minSize = currentBlock ? calculateMinBlockSize(currentBlock) : { width: 300, height: 200 }; const newWidth = Math.max(minSize.width, resizeStart.width + deltaX); const newHeight = Math.max(minSize.height, resizeStart.height + deltaY); - updateBlockSize(resizingBlock, { - width: newWidth, - height: newHeight, - }); + // Direct DOM manipulation + blockToResize.style.width = `${newWidth}px`; + blockToResize.style.height = `${newHeight}px`; } } - }; + } + }; - const handleMouseUp = (): void => { - setDraggingElement(null); - setResizingElement(null); - setDraggingBlock(null); - setResizingBlock(null); - setResizeStart(null); - }; - - if (draggingElement || draggingBlock || resizingElement || resizingBlock) { - document.addEventListener("mousemove", handleMouseMove); - document.addEventListener("mouseup", handleMouseUp); + const handleMouseUp = (): void => { + // Update state only on mouse up for elements + if (draggingElement && selectedBlock && currentElement?.positionType === "absolute") { + const blockElement = document.querySelector(`[data-block-id="${selectedBlock}"]`) as HTMLElement; + const elementToDrag = document.querySelector(`[data-element-id="${draggingElement}"]`) as HTMLElement; + + if (blockElement && elementToDrag) { + const computedStyle = window.getComputedStyle(elementToDrag); + const x = parseFloat(computedStyle.left); + const y = parseFloat(computedStyle.top); + + updateElementPosition(selectedBlock, draggingElement, { x, y }); + } } - return () => { - document.removeEventListener("mousemove", handleMouseMove); - document.removeEventListener("mouseup", handleMouseUp); - }; - }, [draggingElement, resizingElement, draggingBlock, resizingBlock, elementDragOffset, blockDragOffset, selectedBlock, currentElement, resizeStart, currentBlock, blocks]); + // Update state only on mouse up for blocks + if (draggingBlock && currentBlock?.positionType && (currentBlock.positionType === "absolute" || currentBlock.positionType === "fixed")) { + const blockToDrag = document.querySelector(`[data-block-id="${draggingBlock}"]`) as HTMLElement; + + if (blockToDrag) { + const computedStyle = window.getComputedStyle(blockToDrag); + const x = parseFloat(computedStyle.left); + const y = parseFloat(computedStyle.top); + + updateBlockPosition(draggingBlock, { x, y }); + } + } + + // Update state only on mouse up for resizing + if (resizingElement && selectedBlock) { + const elementToResize = document.querySelector(`[data-element-id="${resizingElement}"]`) as HTMLElement; + if (elementToResize) { + const computedStyle = window.getComputedStyle(elementToResize); + const width = parseFloat(computedStyle.width); + const height = parseFloat(computedStyle.height); + + updateElementSize(selectedBlock, resizingElement, { width, height }); + } + } + + if (resizingBlock) { + const blockToResize = document.querySelector(`[data-block-id="${resizingBlock}"]`) as HTMLElement; + if (blockToResize) { + const computedStyle = window.getComputedStyle(blockToResize); + const width = parseFloat(computedStyle.width); + const height = parseFloat(computedStyle.height); + + updateBlockSize(resizingBlock, { width, height }); + } + } + + // Reset all dragging states + setDraggingElement(null); + setResizingElement(null); + setDraggingBlock(null); + setResizingBlock(null); + setResizeStart(null); + }; + + if (draggingElement || draggingBlock || resizingElement || resizingBlock) { + document.addEventListener("mousemove", handleMouseMove); + document.addEventListener("mouseup", handleMouseUp); + } + + return () => { + document.removeEventListener("mousemove", handleMouseMove); + document.removeEventListener("mouseup", handleMouseUp); + }; +}, [draggingElement, resizingElement, draggingBlock, resizingBlock, elementDragOffset, blockDragOffset, selectedBlock, currentElement, resizeStart, currentBlock, blocks]); // Update dropdown position when showElementDropdown changes useEffect(() => { @@ -234,7 +335,9 @@ const DashboardEditor: React.FC = () => { return (
- addBlock()} showDataModelPanel={showDataModelPanel} setShowDataModelPanel={setShowDataModelPanel} /> + {activeModule === "visualization" && ( + addBlock()} showDataModelPanel={showDataModelPanel} setShowDataModelPanel={setShowDataModelPanel} /> + )} { currentElement={currentElement} selectedBlock={selectedBlock} selectedElement={selectedElement} - blocks={blocks} - setBlocks={setBlocks} updateElementStyle={(blockId, elementId, style) => updateElementStyle(blockId, elementId, style)} updateElementSize={(blockId, elementId, size) => updateElementSize(blockId, elementId, size)} updateElementPosition={(blockId, elementId, position) => updateElementPosition(blockId, elementId, position)} @@ -295,6 +396,7 @@ const DashboardEditor: React.FC = () => { updateElementData={(blockId, elementId, updates) => updateElementData(blockId, elementId, updates)} updateGraphData={(blockId, elementId, newData) => updateGraphData(blockId, elementId, newData)} updateGraphTitle={(blockId, elementId, title) => updateGraphTitle(blockId, elementId, title)} + updateGraphType={(blockId, elementId, type) => updateGraphType(blockId, elementId, type)} setSwapSource={setSwapSource} setShowSwapUI={setShowSwapUI} dataModelManager={dataModelManager} diff --git a/app/src/components/SimulationDashboard/components/block/BlockComponent.tsx b/app/src/components/SimulationDashboard/components/block/BlockComponent.tsx index f159412..8970db2 100644 --- a/app/src/components/SimulationDashboard/components/block/BlockComponent.tsx +++ b/app/src/components/SimulationDashboard/components/block/BlockComponent.tsx @@ -44,36 +44,27 @@ const BlockComponent: React.FC = ({ handleBlockDragStart, }) => { const minSize = calculateMinBlockSize(block); - const isSelected = selectedBlock === block.id; - const isDraggable = - editMode && (block.positionType === "absolute" || block.positionType === "fixed"); + const isSelected = selectedBlock === block.blockUuid; + const isDraggable = editMode && (block.positionType === "absolute" || block.positionType === "fixed"); const handleMouseDown = (event: React.MouseEvent) => { if (isDraggable) { - handleBlockDragStart(block.id, event); + handleBlockDragStart(block.blockUuid, event); } - handleBlockClick(block.id, event); + handleBlockClick(block.blockUuid, event); }; return (
= ({ )} -
handleElementResizeStart(element.id, e)} - /> +
handleElementResizeStart(element.elementUuid, e)} /> )}
diff --git a/app/src/components/SimulationDashboard/components/element/ElementEditor.tsx b/app/src/components/SimulationDashboard/components/element/ElementEditor.tsx index 2d17fc3..acc239f 100644 --- a/app/src/components/SimulationDashboard/components/element/ElementEditor.tsx +++ b/app/src/components/SimulationDashboard/components/element/ElementEditor.tsx @@ -1,5 +1,5 @@ import type { RefObject } from "react"; -import { ExtendedCSSProperties, UIElement, Block } from "../../../../types/exportedTypes"; +import { ExtendedCSSProperties, UIElement } from "../../../../types/exportedTypes"; import { getCurrentElementStyleValue } from "../../functions/helpers/getCurrentElementStyleValue"; import type { DataModelManager } from "../../data/dataModel"; @@ -8,8 +8,6 @@ interface ElementEditorProps { currentElement: UIElement; selectedBlock: string; selectedElement: string; - blocks: Block[]; - setBlocks: (blocks: Block[]) => void; updateElementStyle: (blockId: string, elementId: string, style: ExtendedCSSProperties) => void; updateElementSize: (blockId: string, elementId: string, size: { width: number; height: number }) => void; updateElementPosition: (blockId: string, elementId: string, position: { x: number; y: number }) => void; @@ -18,6 +16,7 @@ interface ElementEditorProps { updateElementData: (blockId: string, elementId: string, updates: Partial) => void; updateGraphData: (blockId: string, elementId: string, newData: GraphDataPoint[]) => void; updateGraphTitle: (blockId: string, elementId: string, title: string) => void; + updateGraphType: (blockId: string, elementId: string, type: GraphTypes) => void; setSwapSource: (source: string | null) => void; setShowSwapUI: (show: boolean) => void; dataModelManager: DataModelManager; @@ -28,8 +27,6 @@ const ElementEditor: React.FC = ({ currentElement, selectedBlock, selectedElement, - blocks, - setBlocks, updateElementStyle, updateElementSize, updateElementPosition, @@ -38,6 +35,7 @@ const ElementEditor: React.FC = ({ updateElementData, updateGraphData, updateGraphTitle, + updateGraphType, setSwapSource, setShowSwapUI, dataModelManager, @@ -352,26 +350,7 @@ const ElementEditor: React.FC = ({