Refactor code structure for improved readability and maintainability

This commit is contained in:
Vishnu 2025-05-12 18:09:22 +05:30
parent 970a1b2bae
commit b1569e64ed
13 changed files with 170 additions and 128 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

@ -32,7 +32,7 @@ const FileMenu: React.FC = () => {
}, []); }, []);
// project // project
const [projectName, setProjectName] = useState("project 1"); const [projectName, setProjectName] = useState("Demo Project");
// Load project name from localStorage on mount // Load project name from localStorage on mount
useEffect(() => { useEffect(() => {

View File

@ -5,7 +5,7 @@ import SkeletonUI from "../../templates/SkeletonUI";
const ProductionCapacity = ({ const ProductionCapacity = ({
progressPercent = 50, progressPercent = 50,
avgProcessTime = "28.4 Secs/unit", avgProcessTime = "28.4",
machineUtilization = "78%", machineUtilization = "78%",
throughputValue = 128, throughputValue = 128,
timeRange = { startTime: "08:00 AM", endTime: "09:00 AM" }, timeRange = { startTime: "08:00 AM", endTime: "09:00 AM" },
@ -34,7 +34,7 @@ const ProductionCapacity = ({
<> <>
<div className="process-container"> <div className="process-container">
<div className="throughput-value"> <div className="throughput-value">
<span className="value">{throughputValue}</span> Units/hour <span className="value">{avgProcessTime}</span> secs/unit
</div> </div>
{/* Dynamic Progress Bar */} {/* Dynamic Progress Bar */}
@ -56,8 +56,8 @@ const ProductionCapacity = ({
<div className="metrics-section"> <div className="metrics-section">
<div className="metric"> <div className="metric">
<span className="label">Avg. Process Time</span> <span className="label">Units/hour</span>
<span className="value">{avgProcessTime}</span> <span className="value">{throughputValue} avg.</span>
</div> </div>
<div className="metric"> <div className="metric">
<span className="label">Machine Utilization</span> <span className="label">Machine Utilization</span>

View File

@ -2,8 +2,8 @@ import * as THREE from 'three';
import * as Types from "../../../../types/world/worldTypes"; import * as Types from "../../../../types/world/worldTypes";
import * as CONSTANTS from "../../../../types/world/worldConstants"; import * as CONSTANTS from "../../../../types/world/worldConstants";
import texturePath from "../../../../assets/textures/floor/concreteFloorWorn001Diff2k.jpg"; import texturePath from "../../../../assets/textures/floor/white.png";
import normalPath from "../../../../assets/textures/floor/concreteFloorWorn001NorGl2k.jpg"; import texturePathDark from "../../../../assets/textures/floor/black.png";
// Cache for materials // Cache for materials
const materialCache = new Map<string, THREE.Material>(); const materialCache = new Map<string, THREE.Material>();
@ -14,6 +14,8 @@ export default function addFloorToScene(
floorGroup: Types.RefGroup, floorGroup: Types.RefGroup,
userData: any, userData: any,
) { ) {
const savedTheme: string | null = localStorage.getItem('theme');
const textureLoader = new THREE.TextureLoader(); const textureLoader = new THREE.TextureLoader();
const textureScale = CONSTANTS.floorConfig.textureScale; const textureScale = CONSTANTS.floorConfig.textureScale;
@ -24,20 +26,17 @@ export default function addFloorToScene(
if (materialCache.has(materialKey)) { if (materialCache.has(materialKey)) {
material = materialCache.get(materialKey) as THREE.Material; material = materialCache.get(materialKey) as THREE.Material;
// } else {
} else { } else {
const floorTexture = textureLoader.load(texturePath); const floorTexture = textureLoader.load(savedTheme === "dark" ? texturePathDark : texturePath);
const normalMap = textureLoader.load(normalPath); // const floorTexture = textureLoader.load(texturePath);
floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping; floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;
floorTexture.repeat.set(textureScale, textureScale); floorTexture.repeat.set(textureScale, textureScale);
floorTexture.colorSpace = THREE.SRGBColorSpace; floorTexture.colorSpace = THREE.SRGBColorSpace;
normalMap.wrapS = normalMap.wrapT = THREE.RepeatWrapping;
normalMap.repeat.set(textureScale, textureScale);
material = new THREE.MeshStandardMaterial({ material = new THREE.MeshStandardMaterial({
map: floorTexture, map: floorTexture,
normalMap: normalMap,
side: THREE.DoubleSide, side: THREE.DoubleSide,
}); });

View File

@ -1,7 +1,6 @@
import { useFrame, useThree } from "@react-three/fiber"; import { useFrame, useThree } from "@react-three/fiber";
import { import {
useActiveTool, useActiveTool,
useAsset3dWidget,
useCamMode, useCamMode,
useDeletableFloorItem, useDeletableFloorItem,
useDeleteTool, useDeleteTool,
@ -14,7 +13,6 @@ import {
useToggleView, useToggleView,
useTransformMode, useTransformMode,
} from "../../../store/store"; } from "../../../store/store";
import assetVisibility from "../geomentries/assets/assetVisibility";
import { useEffect } from "react"; import { useEffect } from "react";
import * as THREE from "three"; import * as THREE from "three";
import * as Types from "../../../types/world/worldTypes"; import * as Types from "../../../types/world/worldTypes";
@ -29,7 +27,6 @@ import loadInitialFloorItems from "../IntialLoad/loadInitialFloorItems";
import addAssetModel from "../geomentries/assets/addAssetModel"; import addAssetModel from "../geomentries/assets/addAssetModel";
import { getFloorAssets } from "../../../services/factoryBuilder/assest/floorAsset/getFloorItemsApi"; import { getFloorAssets } from "../../../services/factoryBuilder/assest/floorAsset/getFloorItemsApi";
import useModuleStore from "../../../store/useModuleStore"; import useModuleStore from "../../../store/useModuleStore";
// import { retrieveGLTF } from "../../../utils/indexDB/idbUtils";
import { useEventsStore } from "../../../store/simulation/useEventsStore"; import { useEventsStore } from "../../../store/simulation/useEventsStore";
const assetManagerWorker = new Worker( const assetManagerWorker = new Worker(
@ -198,9 +195,7 @@ const FloorItemsGroup = ({
}; };
const startInterval = () => { const startInterval = () => {
if (!intervalId) { intervalId ??= setInterval(handleChange, 50);
intervalId = setInterval(handleChange, 50);
}
}; };
const stopInterval = () => { const stopInterval = () => {

View File

@ -1,30 +1,48 @@
import { Geometry } from "@react-three/csg"; import { Geometry } from "@react-three/csg";
import { useDeleteTool, useSelectedWallItem, useToggleView, useTransformMode, useWallItems, useWalls } from "../../../store/store"; import {
useDeleteTool,
useSelectedWallItem,
useToggleView,
useTransformMode,
useWallItems,
useWalls,
} from "../../../store/store";
import handleMeshDown from "../eventFunctions/handleMeshDown"; import handleMeshDown from "../eventFunctions/handleMeshDown";
import handleMeshMissed from "../eventFunctions/handleMeshMissed"; import handleMeshMissed from "../eventFunctions/handleMeshMissed";
import WallsMesh from "./wallsMesh"; import WallsMesh from "./wallsMesh";
import WallItemsGroup from "./wallItemsGroup"; import WallItemsGroup from "./wallItemsGroup";
import { useEffect } from "react"; import { useEffect } from "react";
const WallsAndWallItems = ({
const WallsAndWallItems = ({ CSGGroup, AssetConfigurations, setSelectedItemsIndex, selectedItemsIndex, currentWallItem, csg, lines, hoveredDeletableWallItem }: any) => { CSGGroup,
const { walls, setWalls } = useWalls(); AssetConfigurations,
const { wallItems, setWallItems } = useWallItems(); setSelectedItemsIndex,
const { toggleView, setToggleView } = useToggleView(); selectedItemsIndex,
const { deleteTool, setDeleteTool } = useDeleteTool(); currentWallItem,
const { transformMode, setTransformMode } = useTransformMode(); csg,
const { selectedWallItem, setSelectedWallItem } = useSelectedWallItem(); lines,
hoveredDeletableWallItem,
}: any) => {
const { walls } = useWalls();
const { wallItems } = useWallItems();
const { toggleView } = useToggleView();
const { deleteTool } = useDeleteTool();
const { transformMode } = useTransformMode();
const { setSelectedWallItem } = useSelectedWallItem();
useEffect(() => { useEffect(() => {
if (transformMode === null) { if (transformMode === null) {
if (!deleteTool) { if (!deleteTool) {
handleMeshMissed(currentWallItem, setSelectedWallItem, setSelectedItemsIndex); handleMeshMissed(
currentWallItem,
setSelectedWallItem,
setSelectedItemsIndex
);
setSelectedWallItem(null); setSelectedWallItem(null);
setSelectedItemsIndex(null); setSelectedItemsIndex(null);
} }
} }
}, [transformMode]) }, [transformMode]);
return ( return (
<mesh <mesh
ref={CSGGroup as any} ref={CSGGroup as any}
@ -34,12 +52,23 @@ const WallsAndWallItems = ({ CSGGroup, AssetConfigurations, setSelectedItemsInde
visible={!toggleView} visible={!toggleView}
onClick={(event) => { onClick={(event) => {
if (!deleteTool && transformMode !== null) { if (!deleteTool && transformMode !== null) {
handleMeshDown(event, currentWallItem, setSelectedWallItem, setSelectedItemsIndex, wallItems, toggleView); handleMeshDown(
event,
currentWallItem,
setSelectedWallItem,
setSelectedItemsIndex,
wallItems,
toggleView
);
} }
}} }}
onPointerMissed={() => { onPointerMissed={() => {
if (!deleteTool) { if (!deleteTool) {
handleMeshMissed(currentWallItem, setSelectedWallItem, setSelectedItemsIndex); handleMeshMissed(
currentWallItem,
setSelectedWallItem,
setSelectedItemsIndex
);
setSelectedWallItem(null); setSelectedWallItem(null);
setSelectedItemsIndex(null); setSelectedItemsIndex(null);
} }
@ -47,10 +76,17 @@ const WallsAndWallItems = ({ CSGGroup, AssetConfigurations, setSelectedItemsInde
> >
<Geometry ref={csg as any} computeVertexNormals useGroups> <Geometry ref={csg as any} computeVertexNormals useGroups>
<WallsMesh lines={lines} /> <WallsMesh lines={lines} />
<WallItemsGroup currentWallItem={currentWallItem} AssetConfigurations={AssetConfigurations} hoveredDeletableWallItem={hoveredDeletableWallItem} selectedItemsIndex={selectedItemsIndex} setSelectedItemsIndex={setSelectedItemsIndex} CSGGroup={CSGGroup} /> <WallItemsGroup
currentWallItem={currentWallItem}
AssetConfigurations={AssetConfigurations}
hoveredDeletableWallItem={hoveredDeletableWallItem}
selectedItemsIndex={selectedItemsIndex}
setSelectedItemsIndex={setSelectedItemsIndex}
CSGGroup={CSGGroup}
/>
</Geometry> </Geometry>
</mesh> </mesh>
) );
} };
export default WallsAndWallItems; export default WallsAndWallItems;

View File

@ -1,23 +1,27 @@
import * as THREE from 'three'; import * as THREE from "three";
import * as Types from '../../../types/world/worldTypes'; import * as Types from "../../../types/world/worldTypes";
import * as CONSTANTS from '../../../types/world/worldConstants'; import * as CONSTANTS from "../../../types/world/worldConstants";
import { Base } from '@react-three/csg'; import { Base } from "@react-three/csg";
import { MeshDiscardMaterial } from '@react-three/drei'; import { MeshDiscardMaterial } from "@react-three/drei";
import { useUpdateScene, useWalls } from '../../../store/store'; import { useUpdateScene, useWalls } from "../../../store/store";
import { useEffect } from 'react'; import React, { useEffect } from "react";
import { getLines } from '../../../services/factoryBuilder/lines/getLinesApi'; import { getLines } from "../../../services/factoryBuilder/lines/getLinesApi";
import objectLinesToArray from '../geomentries/lines/lineConvertions/objectLinesToArray'; import objectLinesToArray from "../geomentries/lines/lineConvertions/objectLinesToArray";
import loadWalls from '../geomentries/walls/loadWalls'; import loadWalls from "../geomentries/walls/loadWalls";
// texture
import texturePath from "../../../assets/textures/floor/wall-tex.png";
const WallsMesh = ({ lines }: any) => { // Cache for materials
const materialCache = new Map<string, THREE.Material>();
const WallsMeshComponent = ({ lines }: any) => {
const { walls, setWalls } = useWalls(); const { walls, setWalls } = useWalls();
const { updateScene, setUpdateScene } = useUpdateScene(); const { updateScene, setUpdateScene } = useUpdateScene();
useEffect(() => { useEffect(() => {
if (updateScene) { if (updateScene) {
const email = localStorage.getItem("email");
const email = localStorage.getItem('email') const organization = email!.split("@")[1].split(".")[0];
const organization = (email!.split("@")[1]).split(".")[0];
getLines(organization).then((data) => { getLines(organization).then((data) => {
const Lines: Types.Lines = objectLinesToArray(data); const Lines: Types.Lines = objectLinesToArray(data);
@ -26,15 +30,22 @@ const WallsMesh = ({ lines }: any) => {
if (Lines) { if (Lines) {
loadWalls(lines, setWalls); loadWalls(lines, setWalls);
} }
}) });
setUpdateScene(false); setUpdateScene(false);
} }
}, [updateScene]) }, [updateScene]);
const textureLoader = new THREE.TextureLoader();
const wallTexture = textureLoader.load(texturePath);
wallTexture.wrapS = wallTexture.wrapT = THREE.RepeatWrapping;
wallTexture.repeat.set(0.1, 0.1);
wallTexture.colorSpace = THREE.SRGBColorSpace;
return ( return (
<> <>
{walls.map((wall: Types.Wall, index: number) => ( {walls.map((wall: Types.Wall, index: number) => (
<mesh key={index}> <mesh key={index} renderOrder={1}>
<Base <Base
name={`Wall${index + 1}`} name={`Wall${index + 1}`}
geometry={wall[0]} geometry={wall[0]}
@ -45,6 +56,7 @@ const WallsMesh = ({ lines }: any) => {
<meshStandardMaterial <meshStandardMaterial
side={THREE.DoubleSide} side={THREE.DoubleSide}
color={CONSTANTS.wallConfig.defaultColor} color={CONSTANTS.wallConfig.defaultColor}
map={wallTexture}
/> />
</Base> </Base>
<mesh <mesh
@ -59,7 +71,8 @@ const WallsMesh = ({ lines }: any) => {
</mesh> </mesh>
))} ))}
</> </>
) );
} };
const WallsMesh = React.memo(WallsMeshComponent);
export default WallsMesh; export default WallsMesh;

View File

@ -1002,8 +1002,7 @@
height: 100%; height: 100%;
width: 1px; width: 1px;
position: absolute; position: absolute;
color: var(--text-color); color: var(--accent-color);
opacity: 0.4;
font-size: var(--font-size-regular); font-size: var(--font-size-regular);
outline-offset: -1px; outline-offset: -1px;
top: 0; top: 0;

View File

@ -326,7 +326,7 @@ export const lineConfig: LineConfig = {
}; };
export const wallConfig: WallConfig = { export const wallConfig: WallConfig = {
defaultColor: "white", // Default color of the walls defaultColor: "#f2f2f2", // Default color of the walls
height: 7, // Height of the walls height: 7, // Height of the walls
width: 0.05, // Width of the walls width: 0.05, // Width of the walls
}; };
@ -334,7 +334,7 @@ export const wallConfig: WallConfig = {
export const floorConfig: FloorConfig = { export const floorConfig: FloorConfig = {
defaultColor: "grey", // Default color of the floors defaultColor: "grey", // Default color of the floors
height: 0.1, // Height of the floors height: 0.1, // Height of the floors
textureScale: 0.1, // Scale of the floor texture textureScale: 1, // Scale of the floor texture
}; };
export const roofConfig: RoofConfig = { export const roofConfig: RoofConfig = {
@ -345,7 +345,7 @@ export const roofConfig: RoofConfig = {
export const aisleConfig: AisleConfig = { export const aisleConfig: AisleConfig = {
width: 0.1, // Width of the aisles width: 0.1, // Width of the aisles
height: 0.01, // Height of the aisles height: 0.01, // Height of the aisles
defaultColor: 0xffff00, // Default color of the aisles defaultColor: 0xE2AC09, // Default color of the aisles
}; };
export const zoneConfig: ZoneConfig = { export const zoneConfig: ZoneConfig = {

View File

@ -4,7 +4,7 @@ export { };
function setTheme() { function setTheme() {
const savedTheme: string | null = localStorage.getItem('theme'); const savedTheme: string | null = localStorage.getItem('theme');
const systemPrefersDark: boolean = window.matchMedia('(prefers-color-scheme: dark)').matches; const systemPrefersDark: boolean = window.matchMedia('(prefers-color-scheme: dark)').matches;
const defaultTheme: string = savedTheme || (systemPrefersDark ? 'dark' : 'light'); const defaultTheme: string = savedTheme ?? (systemPrefersDark ? 'dark' : 'light');
document.documentElement.setAttribute('data-theme', defaultTheme); document.documentElement.setAttribute('data-theme', defaultTheme);
localStorage.setItem('theme', defaultTheme); localStorage.setItem('theme', defaultTheme);
} }